利用gettimeofday简单测试程序用时
2016-02-24 15:41
561 查看
centos$ man 2 gettimeofday
#include <sys/time.h> int gettimeofday(struct timeval *tv, struct timezone *tz); struct timeval { time_t tv_sec; // seconds suseconds_t tv_usec; //microseconds }; // it gives the number of seconds and microseconds since the Epoch (see time(2)) struct timezone { int tz_minuteswest; //minutes west of Greenwich int tz_dsttime; //type of DST correction }; // if tv or tz is NULL, the corresponding structure is no set or returned.
简单测试用例
程序中添加两个struct timeval 来记录开始和完成的时间就可以了。#include <stdio.h> #include <stdlib.h> #include <sys/time.h> int main() { struct timeval beginTime, endTime; if (gettimeofday(&beginTime, NULL) == -1) return -1; for (int i = 1; i <= 10000; i++) for (int j = 1; j <= 10000; j++) ; if (gettimeofday(&endTime, NULL) == -1) return -1; double seconds; seconds = 1000000*(endTime.tv_sec - beginTime.tv_sec) + (endTime.tv_usec - beginTime.tv_usec); seconds /= 1000000; printf("using time : %f\n", seconds); return 0; }
运行结果
相关文章推荐
- fft
- vi/vim多行注释和取消注释(转载)
- init.rc
- 利用图层制作选项卡
- 重复数据删除:基于相同数据的检测
- Android 依据变量来获得资源R中的id值
- HDU 2141 哈希表或者二分
- 【CSS3】变形--缩放 scale()
- WeX5学习笔记(一):打包app时出错
- 关于LINUX中的GDB调试 与 LINUX一些命令
- 泛型
- js常见函数汇总
- 林达华推荐的几本数学书
- 网络学习笔记2
- php 把数据导成excel格式
- 如何在windows2008/2012上安装启明星系统。
- activiti 引擎 数据库设计说明书
- Mysql----浅入浅出之视图、存储过程、触发器
- for循环与while循环的区别和联系
- android触摸事件冲突