0

我正在尝试使用 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));
}
4

2 回答 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.
 }
于 2017-12-08T16:10:33.417 回答
0

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

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

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

于 2017-12-08T19:17:22.300 回答