如何衡量在Linux / Windows的CPU时间和挂钟时间? [英] How can I measure CPU time and wall clock time on both Linux/Windows?
问题描述
我的意思是:我怎么能测量时间我的CPU功能上执行,需要运行我的功能挂钟时间用在何处? (IM感兴趣的Linux / Windows与x86和x86_64的)。看看我想做的事(IM使用C ++在这里,但我会preFER℃溶液):
I mean: how can I measure time my CPU spent on function execution and wall clock time it takes to run my function? (Im interested in Linux/Windows and both x86 and x86_64). See what I want to do (Im using C++ here but I would prefer C solution):
int startcputime, endcputime, wcts, wcte;
startcputime = cputime();
function(args);
endcputime = cputime();
std::cout << "it took " << endcputime - startcputime << " s of CPU to execute this\n";
wcts = wallclocktime();
function(args);
wcte = wallclocktime();
std::cout << "it took " << wcte - wcts << " s of real time to execute this\n";
另一个重要的问题:就是这个类型的时间测量体系结构无关的或不
推荐答案
下面是一个复制粘贴的解决方案,以及在Windows和Linux可以作为C和C ++。
Here's a copy-paste solution that works on both Windows and Linux as well as C and C++.
正如评论中提到,有,这是否提振库。但是,如果你不能使用boost,这应该工作:
As mentioned in the comments, there's a boost library that does this. But if you can't use boost, this should work:
// Windows
#ifdef _WIN32
#include <Windows.h>
double get_wall_time(){
LARGE_INTEGER time,freq;
if (!QueryPerformanceFrequency(&freq)){
// Handle error
return 0;
}
if (!QueryPerformanceCounter(&time)){
// Handle error
return 0;
}
return (double)time.QuadPart / freq.QuadPart;
}
double get_cpu_time(){
FILETIME a,b,c,d;
if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0){
// Returns total user time.
// Can be tweaked to include kernel times as well.
return
(double)(d.dwLowDateTime |
((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
}else{
// Handle error
return 0;
}
}
// Posix/Linux
#else
#include <time.h>
#include <sys/time.h>
double get_wall_time(){
struct timeval time;
if (gettimeofday(&time,NULL)){
// Handle error
return 0;
}
return (double)time.tv_sec + (double)time.tv_usec * .000001;
}
double get_cpu_time(){
return (double)clock() / CLOCKS_PER_SEC;
}
#endif
有一堆方法来实现这些时钟。但这里是上面的代码片段使用:
There's a bunch of ways to implement these clocks. But here's what the above snippet uses:
对于Windows:
- 挂钟时间:<一href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/aa373083%28v=vs.85%29.aspx\">Performance计数器
- CPU时间:<一href=\"http://msdn.microsoft.com/en-us/library/windows/desktop/ms683223%28v=vs.85%29.aspx\"><$c$c>GetProcessTimes()$c$c>
对于Linux:
- 挂钟时间:
函数gettimeofday()
- CPU时间:
时钟()
这是一个小的演示:
#include <math.h>
#include <iostream>
using namespace std;
int main(){
// Start Timers
double wall0 = get_wall_time();
double cpu0 = get_cpu_time();
// Perform some computation.
double sum = 0;
#pragma omp parallel for reduction(+ : sum)
for (long long i = 1; i < 10000000000; i++){
sum += log((double)i);
}
// Stop timers
double wall1 = get_wall_time();
double cpu1 = get_cpu_time();
cout << "Wall Time = " << wall1 - wall0 << endl;
cout << "CPU Time = " << cpu1 - cpu0 << endl;
// Prevent Code Elimination
cout << endl;
cout << "Sum = " << sum << endl;
}
输出(12个线程):
Output (12 threads):
Wall Time = 15.7586
CPU Time = 178.719
Sum = 2.20259e+011
这篇关于如何衡量在Linux / Windows的CPU时间和挂钟时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!