这是一个简单的memset带宽基准:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main()
{
unsigned long n, r, i;
unsigned char *p;
clock_t c0, c1;
double elapsed;
n = 1000 * 1000 * 1000; /* GB */
r = 100; /* repeat */
p = calloc(n, 1);
c0 = clock();
for(i = 0; i < r; ++i) {
memset(p, (int)i, n);
printf("%4d/%4ld\r", p[0], r); /* "use" the result */
fflush(stdout);
}
c1 = clock();
elapsed = (c1 - c0) / …Run Code Online (Sandbox Code Playgroud) 灵感来自c/c ++中初始化和归零数组的问题?在我的例子中,我决定实际检查一下针对Windows Mobile Professional(ARM处理器,来自Microsoft Optimizing Compiler)的优化发布版本.我发现的有点令人惊讶,我想知道是否有人可以解释我的问题.
检查这两个例子:
byte a[10] = { 0 };
byte b[10];
memset(b, 0, sizeof(b));
Run Code Online (Sandbox Code Playgroud)
它们在同一个函数中使用,因此堆栈如下所示:
[ ] // padding byte to reach DWORD boundary
[ ] // padding byte to reach DWORD boundary
[ ] // b[9] (last element of b)
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ] // b[0] = sp + 12 (stack pointer + 12 bytes)
[ ] …Run Code Online (Sandbox Code Playgroud)