查找代码所花费的时间

问题描述 投票:0回答:2

我正在尝试使用 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));
}
c profiling time.h memmove
2个回答
1
投票

正如我在评论中所写,移动 5000 字节的速度太快,无法用

clock
来测量。如果你执行 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.
 }

0
投票

如果您想知道 Beagle 骨或其他带有 GIPO 的设备所花费的时间,您可以在例程之前和之后切换 GPIO。您必须连接示波器或类似的设备来快速采样电压。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.