之前有写文章介绍了 mbw(https://blog.csdn.net/engrossment/article/details/88314722)以及 lmbench(https://blog.csdn.net/engrossment/article/details/83830363)。其中 mbw 的只是简单使用了 memcpy 系统调用进行测试,而 lmbench 里面的 bw_mem、stream 则给出了多种模式的带宽测试方式。查看其代码实现,比较复杂,而且加入了一些运算,使得在嵌入式的测试场景中,处理器成为了测试的瓶颈,无法测出更好的峰值。
于是参考 stream 的实现,简化了数据的读写,而且是读、写各自独立统计。代码如下。注意编译使用 -O3 优化级别编译。
#include <stdio.h>#include <stdint.h>#include <stdlib.h>#include <unistd.h>#include <sys/time.h>//NOTE, you should compile this program with gcc -O3#define N 10000000 // Count of array elements.// Return seconds since Epoch.double mysecond() { struct timeval tp; struct timezone tzp; gettimeofday(&tp, &tzp)