比方说,我有一个函数(或函数),它需要很长时间(墙壁时间)来执行,例如:
#include "stdafx.h"
#include <math.h>
#include <windows.h>
void fun()
{
long sum = 0L;
for (long long i = 1; i < 10000000; i++){
sum += log((double)i);
}
}
double cputimer()
{
FILETIME createTime;
FILETIME exitTime;
FILETIME kernelTime;
FILETIME userTime;
if ( GetProcessTimes( GetCurrentProcess( ),
&createTime, &exitTime, &kernelTime, &userTime ) != -1 )
{
SYSTEMTIME userSystemTime;
if ( FileTimeToSystemTime( &userTime, &userSystemTime ) != -1 )
return (double)userSystemTime.wHour * 3600.0 +
(double)userSystemTime.wMinute * 60.0 +
(double)userSystemTime.wSecond +
(double)userSystemTime.wMilliseconds / 1000.0;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
double start, stop;
start = cputimer();
fun();
stop = cputimer();
printf("Time taken: %f [seconds]\n", stop - start);
return 0;
}我想测量这个函数的CPU负载和这个函数调用所使用的RAM使用情况。这有可能吗?我该怎么做?我对Windows和Linux解决方案感兴趣。
发布于 2013-10-19 19:14:12
在POSIX上,您可以尝试使用类似于检查墙壁时间的方式使用getrusage。不确定窗户的事。
发布于 2013-10-19 19:14:08
https://stackoverflow.com/questions/19469962
复制相似问题