计时函数总结
来源:互联网 发布:seo大牛张辉 编辑:程序博客网 时间:2024/05/14 01:29
1.标准C/C++的二个计时函数time()及clock()
time_t time(time_t *timer);
返回以格林尼治时间(GMT)为标准,从1970年1月1日00:00:00到现在的此时此刻所经过的秒数。
time_t实际是个long长整型typedef long time_t;
头文件:#include <time.h>
#include <stdio.h> #include <windows.h> #include <time.h> //time_t time() clock_t clock() int main() { //用time()来计时 秒 time_t begin, end; begin = time(NULL); Sleep(1000); end = time(NULL); printf("%d\n", end - begin); return 0;}
2.clock_t clock(void);
返回进程启动到调用函数时所经过的CPU时钟计时单元(clock tick)数,在MSDN中称之为挂钟时间(wal-clock),以毫秒为单位。
clock_t实际是个long长整型typedef long clock_t;
头文件:#include <time.h>
#include <stdio.h> #include <windows.h> #include <time.h> //time_t time() clock_t clock()int main(){ //用clock()来计时 毫秒 clock_t begin, end; begin = clock(); Sleep(1000); end = clock(); printf("%d\n", end - begin); return 0;}
3.Windows系统API函数
timeGetTime()、GetTickCount()及QueryPerformanceCounter()
DWORD timeGetTime(VOID);
返回系统时间,以毫秒为单位。系统时间是从系统启动到调用函数时所经过的毫秒数。注意,这个值是32位的,会在0到2^32之间循环,约49.71天。
头文件:#include <Mmsystem.h>
引用库:#pragma comment(lib, "Winmm.lib")
#include <stdio.h> #include <windows.h> #include <time.h> //time_t time() clock_t clock()int main(){ //用clock()来计时 毫秒 DWORD begin, end; begin = timeGetTime(); Sleep(1000); end = timeGetTime(); printf("%d\n", end - begin); return 0;}
DWORD WINAPI GetTickCount(void);
这个函数和timeGetTime()一样也是返回系统时间,以毫秒为单位。
头文件:直接使用#include <windows.h>就可以了。
#include <stdio.h> #include <windows.h> int main(){ //用GetTickCount()来计时 毫秒 DWORD dwGTCBegin, dwGTCEnd; dwGTCBegin = GetTickCount(); Sleep(1000); dwGTCEnd = GetTickCount(); printf("%d\n", dwGTCEnd - dwGTCBegin); return 0;}
高精度计时,以微秒为单位(1毫秒=1000微秒)。
BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);
得到高精度计时器的值(如果存在这样的计时器)。
BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);
返回硬件支持的高精度计数器的频率(次每秒),返回0表示失败。
LARGE_INTEGER其实是一个联合体,可以得到__int64 QuadPart;也可以分别得到低32位DWORD LowPart和高32位的值LONG HighPart。
在使用时,先使用QueryPerformanceFrequency()得到计数器的频率,再计算二次调用QueryPerformanceCounter()所得的计时器值之差,用差去除以频率就得到精确的计时了。
头文件:直接使用#include <windows.h>就可以了。
#include <stdio.h> #include <windows.h> int main(){ //用QueryPerformanceCounter()来计时 微秒 LARGE_INTEGER large_interger; double dff; __int64 c1, c2; QueryPerformanceFrequency(&large_interger); dff = large_interger.QuadPart; QueryPerformanceCounter(&large_interger); c1 = large_interger.QuadPart; Sleep(800); QueryPerformanceCounter(&large_interger); c2 = large_interger.QuadPart; printf("本机高精度计时器频率%lf\n", dff); printf("第一次计时器值%I64d 第二次计时器值%I64d 计时器差%I64d\n", c1, c2, c2 - c1); printf("计时%lf毫秒\n", (c2 - c1) * 1000 / dff); return 0;}
4.cuda API计时
<pre name="code" class="cpp">#include <stdio.h>#include <Windows.h>#include <cuda_runtime.h> int main(){ //毫秒float elapseTime; cudaEvent_t begin, end;cudaEventCreate(&begin);//创建eventcudaEventCreate(&end);cudaEventRecord(begin, 0);//记录当前时间kernel<<<...>>>();cudaThreadSynchronize();cudaEventRecord(end, 0);cudaEventSynchronize(end);cudaEventElapsedTime(&elapseTime, begin, end);printf("Processing time:%f(ms)\n", elapseTime);return 0;}
- 计时函数总结
- Matlab计时函数总结
- 计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- windows各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- Windows 各种计时函数总结
- sdk、jdk、jre的区别
- 动态规划的理论知识
- Python 获取Facebook用户的Friends的爱好
- week 1 Union-Find(并查集)
- android文本文字自动向下移动
- 计时函数总结
- 提高编程能力的10种方法
- poj1001
- 海量数据处理问题
- HDU-1517-A Multiplication Game
- poj 2607 Fire Station(floyd)
- 重载操作符的设计
- 如何提高编程能力
- 2.1.3 Search in Rotated Sorted Array