c++代码示例
#include <windows.h>
#include <iostream>
void main()
{
LARGE_INTEGER t1,t2,tc;
QueryPerformanceFrequency(&tc);
QueryPerformanceCounter(&t1);
func(); // 需要计时的操作
QueryPerformanceCounter(&t2);
double wasteTime = (double)(t2.QuadPart - t1.QuadPart) / (double)tc.QuadPart;
std::cout << "waste time: " << time << std::endl; // 输出时间(单位:s)
}
// 其它方法:
// clock() // 精确到秒
// GetTickCount() // 精确到10ms
// linux - gettimeofday()
qt代码示例
#include <QTime>
void main()
{
QTime time;
time.start();
// time.restart(); 重新开始计时
// do something
//while(true){}
//....
time.elapsed(); // 返回值单位毫秒(milliseconds)
}
c#代码示例
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
stopWatch.Start();
// stopWatch.Restart(); 重新开始计时
// do some waste time thing
// while(true){}
stopWatch.Stop();
TimeSpan timeSpan = stopWatch.Elapsed;
double elapsedSecs = timeSpan.TotalSeconds; // 输出耗时单位为秒(seconds)
}
}
}
|