鱼C论坛

 找回密码
 立即注册
查看: 3847|回复: 3

关于fill函数和memset函数的效率问题

[复制链接]
发表于 2019-1-27 23:41:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
在同样的环境下,用fill函数和memset函数填充同一个数组,哪个效率更高呢? fill函数和for循环比较呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-1-28 08:32:45 | 显示全部楼层
本帖最后由 行客 于 2019-1-28 10:50 编辑

memset>for>fill

memset约等于用{0}初始化数组

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-28 13:28:04 | 显示全部楼层
行客 发表于 2019-1-28 08:32
memset>for>fill

memset约等于用{0}初始化数组

为神马fill函数的效率会比for循环差呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-1-28 14:09:07 | 显示全部楼层
在同样的环境下,用fill函数和memset函数填充同一个数组,哪个效率更高呢? fill函数和for循环比较呢?


答案是不确定,不知道
完全取决于这个环境下的fill函数和memset函数的实现

下面贴两个memset函数的实现

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.

  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.

  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  10. Library General Public License for more details.

  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB.  If
  13. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  14. Cambridge, MA 02139, USA.  */

  15. #include <ansidecl.h>
  16. #include <string.h>
  17. #include <memcopy.h>

  18. PTR
  19. DEFUN(memset, (dstpp, c, len), PTR dstpp AND int c AND size_t len)
  20. {
  21.   long int dstp = (long int) dstpp;

  22.   if (len >= 8)
  23.     {
  24.       size_t xlen;
  25.       op_t cccc;

  26.       cccc = (unsigned char) c;
  27.       cccc |= cccc << 8;
  28.       cccc |= cccc << 16;
  29.       if (OPSIZ > 4)
  30.         cccc |= cccc << 32;

  31.       /* There are at least some bytes to set.
  32.          No need to test for LEN == 0 in this alignment loop.  */
  33.       while (dstp % OPSIZ != 0)
  34.         {
  35.           ((byte *) dstp)[0] = c;
  36.           dstp += 1;
  37.           len -= 1;
  38.         }

  39.       /* Write 8 `op_t' per iteration until less than 8 `op_t' remain.  */
  40.       xlen = len / (OPSIZ * 8);
  41.       while (xlen > 0)
  42.         {
  43.           ((op_t *) dstp)[0] = cccc;
  44.           ((op_t *) dstp)[1] = cccc;
  45.           ((op_t *) dstp)[2] = cccc;
  46.           ((op_t *) dstp)[3] = cccc;
  47.           ((op_t *) dstp)[4] = cccc;
  48.           ((op_t *) dstp)[5] = cccc;
  49.           ((op_t *) dstp)[6] = cccc;
  50.           ((op_t *) dstp)[7] = cccc;
  51.           dstp += 8 * OPSIZ;
  52.           xlen -= 1;
  53.         }
  54.       len %= OPSIZ * 8;

  55.       /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain.  */
  56.       xlen = len / OPSIZ;
  57.       while (xlen > 0)
  58.         {
  59.           ((op_t *) dstp)[0] = cccc;
  60.           dstp += OPSIZ;
  61.           xlen -= 1;
  62.         }
  63.       len %= OPSIZ;
  64.     }

  65.   /* Write the last few bytes.  */
  66.   while (len > 0)
  67.     {
  68.       ((byte *) dstp)[0] = c;
  69.       dstp += 1;
  70.       len -= 1;
  71.     }

  72.   return dstpp;
  73. }
复制代码

  1. static inline void * memset(void * s,char c,int count)
  2. {
  3. __asm__("cld\n\t"
  4.         "rep\n\t"
  5.         "stosb"
  6.         ::"a" (c),"D" (s),"c" (count)
  7.         );
  8. return s;
  9. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-15 22:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表