我正在尝试使用 time.h 库查找 c 中 memmove 函数所花费的时间。但是,当我执行代码时,我得到的值为零。有什么可能的解决方案来查找 memmove 函数所花费的时间吗?
void main(){
uint64_t start,end;
uint8_t a,b;
char source[5000];
char dest[5000];
uint64_t j=0;
for(j=0;j<5000;j++){
source[j]=j;
}
start=clock();
memmove(dest,source,5000);
end=clock();
printf("%f",((double)end-start));
}
最佳答案
正如我在评论中所写,内存移动 5000 字节的速度太快,无法用时钟
来测量。如果你执行 memmove 100000 次,那么它就会变得可测量。
下面的代码在我的计算机上给出了 12
的输出。但这取决于平台,您在计算机上获得的数字可能会完全不同。
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
int main(void) {
uint64_t start, end;
char source[5000];
char dest[5000];
uint64_t j = 0;
for (j = 0; j < 5000; j++) {
source[j] = j;
}
start = clock();
for (int i = 0; i < 100000; i++)
{
memmove(dest, source, 5000);
}
end = clock();
printf("%lld", (end - start)); // no need to convert to double, (end - start)
// is an uint64_t.
}
关于c - 查找代码所花费的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47717767/