简体   繁体   English

查找代码花费的时间

[英]Finding time taken by the code

I am trying to find the time taken by memmove function in c using time.h library. 我正在尝试使用time.h库查找c中的memmove函数花费的时间。 However, When i execute the code, I get the value as zero. 但是,当我执行代码时,我得到的值为零。 Any possible solution to find the time taken by the memmove function? 找到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));
}

As I write in my comment, memmoving 5000 bytes is far too fast to be mesurable with clock . 正如我在评论中所写,移动5000字节太快了,无法用clock来测量。 If you do your memmove 100000 times, then it will get mesurable. 如果您做100000次记忆,那么它将可以衡量。

This code below gives an output of 12 on my computer. 下面的代码在我的计算机上输出为12 But this is platform dependent, the number you get on your computer might be quite different. 但是,这是与平台相关的,你让你的计算机上的数字可能是完全不同的。

#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.
 }

If you want to know the time it takes on a beagle bone or another device with a GIPO you can toggle a GPIO before and after your routine. 如果您想知道在小猎犬骨头或带有GIPO的其他设备上花费的时间,可以在例行程序之前和之后切换GPIO。 You will have to hook up an oscilloscope or something similar that can sample voltage quickly. 您将必须连接示波器或可以快速采样电压的类似设备。

I don't know much about beagle bones but it seems the library libpruio allows fast gpio toggling. 我对小猎犬骨头了解不多,但似乎libpruio库允许快速gpio切换。

Also, what is your exact goal here? 另外,您的确切目标是什么? Compare speed on different hardware? 比较不同硬件上的速度? As someone suggests, you could increase the number of loops so it becomes more easily measurable with time.h. 就像有人建议的那样,您可以增加循环数,以便可以更容易地测量time.h。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM