查找代码所花费的时间

时间:2017-12-08 15:50:44

标签: c profiling time.h memmove

我正在尝试使用time.h库在c中找到memmove函数所花费的时间。但是,当我执行代码时,我得到的值为零。找到memmove函数所花时间的任何可能的解决方案?

{"result":"200","log":"success","2017-11-01":{"unsub":"0","spam":"0","soft-bounce":"0"}}

2 个答案:

答案 0 :(得分:1)

正如我在评论中所写的那样,记忆5000字节的速度太快,无法与clock相提并论。如果你做了100000次memmove,那么它将变得可以理解。

以下代码在我的计算机上输出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.
 }

答案 1 :(得分:0)

如果您想知道使用GIPO的beagle bone或其他设备所花费的时间,您可以在例行程序之前和之后切换GPIO。您必须连接示波器或类似的可以快速采样电压的东西。

我对beagle骨骼了解不多,但似乎库libpruio允许快速gpio切换。

另外,你的确切目标是什么?比较不同硬件的速度?正如有人建议的那样,你可以增加循环次数,以便用time.h更容易衡量。

相关问题