算法运行的时间 [英] Time for an algorithm to run

查看:57
本文介绍了算法运行的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对算法计时,但时间始终显示为零。这是我的代码:

I'm trying to time my algorithm but the time always shows up as zero. Here is my code:

steady_clock::time_point start = steady_clock::now();
ReverseArray(list1, 1000000);
steady_clock::time_point end = steady_clock::now();
auto totalTime = end - start;
cout << duration_cast<microseconds>(totalTime).count()<< "\n";


推荐答案

如果您使用的是Windows,请使用此资源。

If you're on Windows, use this resource.

这对于亚微秒精度是有好处的。使用此命令:

It is good for sub-microsecond accuracy. Use this:

LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
LARGE_INTEGER Frequency;
QueryPerformanceFrequency(&Frequency); 
QueryPerformanceCounter(&StartingTime);
ReverseArray(list1, 1000000);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;

ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;

这篇关于算法运行的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆