引言
在项目中或者研究中,经常需要对系统性能进行测试,今天将对我在研究生期间项目中用到的性能测试的有关东西进行简单总结。
程序运行时间
使用linux 命令time
进行运行整个程序
time main_exe
使用在测试程序中打印系统时间的方法
- 获取最佳性能
#include <time.h>
#include <sys/time.h>
//
static long get_current_time()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000 + tv.tv_usec;
}
long perf_test()
{
long start = get_current_time();
// execute the program
long end = get_current_time();
return end - start;
}
void test()
{
long totle = 0;
perf_test();
for(size_t i = 0; i < 10 ;++i)
totle += per_test();
printf("The execute time: %lfus", (double)totle/10.0);
}
值得注意的是,我们首先运行了一边被测函数,测试后面执行的效果。这样测试的性能比平均好一点。
- 调用次数很频繁的功能时间测试可以使用上述方法,但是有时