清除一个小整数数组:循环memset的对比 [英] clearing a small integer array: memset vs. for loop

查看:143
本文介绍了清除一个小整数数组:循环memset的对比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种方法可以零出一个整数/浮点数组:

  memset的(数组,0,sizeof的(INT)* ARRAYSIZE);

 的for(int i = 0; I< ARRAYSIZE ++ I)
    数组[我] = 0;

显然,memset的是大快 ARRAYSIZE 。但是,在什么点是memset的开销实际上比for循环开销更大?例如,对于大小为5的阵列 - 这将是最好的?第一,第二,或者甚至未轧版本:

 数组[0] = 0;
阵列[1] = 0;
阵列[2] = 0;
阵列[3] = 0;
阵列[4] = 0;


解决方案

在所有的可能性,memset的()将被编译器内联(大多数编译器把它当作一个'内在',这基本上意味着它内联,也许除了在最低优化或除非明确禁用)。

例如,这里有从GCC 4.3 一些发行说明:


  

code一代块移动
  (的memcpy )和块集( memset的
  被改写。 GCC现在可以挑
  最好的算法(循环,展开的循环,
  有代表preFIX或指令
  库调用)的基础上的大小
  块被复制和CPU被
  优化。一个新的选项
   -minline-stringops,动态
  被添加。使用此选项字符串
  未知大小的操作
  膨胀使得小块是
  通过在线code复制,而
  大盖帽库调用使用。
  这将导致更快的code比
   -minline-全stringops
  库的实现能够
  使用缓存层次结构的提示。该
  启发式选择特定的
  算法可以通过覆盖
   -mstringop策略。新近还
   memset的 0值不同的是
  内联。


这是可能的编译器做你给替代的例子类似的东西,但我敢打赌这是不太可能。

和它的的grep -able,更显而易见,一目了然什么意图是引导(而不是循环是特别困难的要么神交)。

There are two ways to zero out an integer/float array:

memset(array, 0, sizeof(int)*arraysize);

or:

for (int i=0; i <arraysize; ++i)
    array[i]=0;

obviously, memset is faster for large arraysize. However, at what point is the overhead of memset actually larger than the overhead of the for loop? For example, for an array of size 5 - which would be best? The first, the 2nd, or maybe even the un-rolled version:

array[0] = 0;
array[1] = 0;
array[2] = 0;
array[3] = 0;
array[4] = 0;

解决方案

In all likelihood, memset() will be inlined by your compiler (most compilers treat it as an 'intrinsic', which basically means it's inlined, except maybe at the lowest optimizations or unless explicitly disabled).

For example, here are some release notes from GCC 4.3:

Code generation of block move (memcpy) and block set (memset) was rewritten. GCC can now pick the best algorithm (loop, unrolled loop, instruction with rep prefix or a library call) based on the size of the block being copied and the CPU being optimized for. A new option -minline-stringops-dynamically has been added. With this option string operations of unknown size are expanded such that small blocks are copied by in-line code, while for large blocks a library call is used. This results in faster code than -minline-all-stringops when the library implementation is capable of using cache hierarchy hints. The heuristic choosing the particular algorithm can be overwritten via -mstringop-strategy. Newly also memset of values different from 0 is inlined.

It might be possible for the compiler to do something similar with the alternative examples you gave, but I'd bet it's less likely to.

And it's grep-able and more immediately obvious at a glance what the intent is to boot (not that the loop is particularly difficult to grok either).

这篇关于清除一个小整数数组:循环memset的对比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆