c - 查找代码所花费的时间

标签 c profiling time.h memmove

我正在尝试使用 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/

相关文章:

Python line_profiler 代码示例

c - POSIX 数据类型的格式说明符

objective-c - 一个 unsigned unsigned long 的两倍总是一个 double 吗?

c - visual studio 2013 截断 x64 中返回的指针值

JavaScript 性能分析

node.js - 使用 --inspect 分析 NodeJS 时, "(program)"是什么意思

C 编程 通过<time.h>的time(&start)函数改变程序结果

C mktime() 打印问题

clockid_t(clock_gettime 第一个参数)可移植性

c - 大型数据集的高效输出格式?