如何在 C++ 中使用时钟()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3220477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use clock() in C++
提问by dato datuashvili
How do I call clock()
in C++
?
我怎么叫clock()
的C++
?
For example, I want to test how much time a linear search takes to find a given element in an array.
例如,我想测试线性搜索在数组中查找给定元素所需的时间。
回答by Dolph
#include <iostream>
#include <cstdio>
#include <ctime>
int main() {
std::clock_t start;
double duration;
start = std::clock();
/* Your algorithm here */
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
std::cout<<"printf: "<< duration <<'\n';
}
回答by Martin G
An alternative solution, which is portable and with higher precision, available since C++11, is to use std::chrono
.
从 C++11 开始可用的另一种解决方案是使用std::chrono
.
Here is an example:
下面是一个例子:
#include <iostream>
#include <chrono>
typedef std::chrono::high_resolution_clock Clock;
int main()
{
auto t1 = Clock::now();
auto t2 = Clock::now();
std::cout << "Delta t2-t1: "
<< std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count()
<< " nanoseconds" << std::endl;
}
Running this on ideone.com gave me:
在 ideone.com 上运行它给了我:
Delta t2-t1: 282 nanoseconds
回答by Shirik
clock()
returns the number of clock ticks since your program started. There is a related constant, CLOCKS_PER_SEC
, which tells you how many clock ticks occur in one second. Thus, you can test any operation like this:
clock()
返回自程序启动以来的时钟滴答数。有一个相关的常数 ,CLOCKS_PER_SEC
它告诉您一秒内发生了多少个时钟滴答。因此,您可以像这样测试任何操作:
clock_t startTime = clock();
doSomeOperation();
clock_t endTime = clock();
clock_t clockTicksTaken = endTime - startTime;
double timeInSeconds = clockTicksTaken / (double) CLOCKS_PER_SEC;
回答by SonarJetLens
On Windows at least, the onlypractically accurate measurement mechanism is QueryPerformanceCounter (QPC). std::chrono is implemented using it (since VS2015, if you use that), but it is notaccurate to the same degree as using QueryPerformanceCounter directly. In particular it's claim to report at 1 nanosecond granularity is absolutely not correct. So, if you're measuring something that takes a very short amount of time (and your case might just be such a case), then you should use QPC, or the equivalent for your OS. I came up against this when measuring cache latencies, and I jotted down some notes that you might find useful, here; https://github.com/jarlostensen/notesandcomments/blob/master/stdchronovsqcp.md
至少在 Windows 上,唯一实际准确的测量机制是 QueryPerformanceCounter (QPC)。的std ::时辰使用它(因为VS2015,如果您使用的),但它是实现不准确的相同程度直接使用QueryPerformanceCounter的。特别是它声称以 1 纳秒粒度报告是绝对不正确的。因此,如果您正在测量需要很短时间的东西(而您的情况可能就是这样),那么您应该使用 QPC,或者您的操作系统的等效产品。我在测量缓存延迟时遇到了这个问题,我在这里记下了一些你可能会觉得有用的笔记; https://github.com/jarlostensen/notesandcomments/blob/master/stdchronovsqcp.md
回答by Muhamed Youssry
you can measure how long your program works. The following functions help measure the CPU time since the start of the program:
你可以衡量你的程序运行了多长时间。以下函数有助于测量自程序启动以来的 CPU 时间:
- C++ (double)clock() / CLOCKS PER SEC with ctime included.
- python time.clock() returns floating-point value in seconds.
- Java System.nanoTime() returns long value in nanoseconds.
- C++ (double)clock() / CLOCKS PER SEC,包括 ctime。
- python time.clock() 以秒为单位返回浮点值。
- Java System.nanoTime() 以纳秒为单位返回长值。
my reference: Algorithms toolbox week 1 course part of data structures and algorithms specialization by University of California San Diego & National Research University Higher School of Economics
我的参考资料:加州大学圣地亚哥分校和国立研究大学高等经济学院数据结构和算法专业的算法工具箱第 1 周课程部分
so you can add this line of code after your algorithm
所以你可以在你的算法之后添加这行代码
cout << (double)clock() / CLOCKS_PER_SEC ;
Expected Output: the output representing the number of clock ticks per second
预期输出:代表数量的输出 clock ticks per second
回答by F?rid Alijani
Probably you might be interested in timer like this : H : M : S . Msec.
可能您可能对这样的计时器感兴趣:H:M:S。毫秒
the code in Linux OS:
Linux操作系统中的代码:
#include <iostream>
#include <unistd.h>
using namespace std;
void newline();
int main() {
int msec = 0;
int sec = 0;
int min = 0;
int hr = 0;
//cout << "Press any key to start:";
//char start = _gtech();
for (;;)
{
newline();
if(msec == 1000)
{
++sec;
msec = 0;
}
if(sec == 60)
{
++min;
sec = 0;
}
if(min == 60)
{
++hr;
min = 0;
}
cout << hr << " : " << min << " : " << sec << " . " << msec << endl;
++msec;
usleep(100000);
}
return 0;
}
void newline()
{
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
}
回答by Garrett
#include <iostream>
#include <ctime>
#include <cstdlib> //_sleep() --- just a function that waits a certain amount of milliseconds
using namespace std;
int main()
{
clock_t cl; //initializing a clock type
cl = clock(); //starting time of clock
_sleep(5167); //insert code here
cl = clock() - cl; //end point of clock
_sleep(1000); //testing to see if it actually stops at the end point
cout << cl/(double)CLOCKS_PER_SEC << endl; //prints the determined ticks per second (seconds passed)
return 0;
}
//outputs "5.17"