C++ 如何在没有 boost::timer 的情况下以毫秒为单位计时函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15092504/
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 time a function in milliseconds without boost::timer
提问by Aly
I am using boost 1.46 which does not include boost::timer, What other way can I time my functions.
我正在使用不包含 boost::timer 的 boost 1.46,还有什么其他方法可以为我的函数计时。
I am currently doing this:
我目前正在这样做:
time_t now = time(0);
<some stuff>
time_t after = time(0);
cout << after - now << endl;
but it just gives the answer in seconds, so if the function takes < 1s it displays 0.
但它只是在几秒钟内给出答案,所以如果函数需要 < 1 秒,它会显示 0。
Thanks
谢谢
回答by Quentin Perez
In linux or Windows:
在 linux 或 Windows 中:
#include <ctime>
#include <iostream>
int
main(int, const char**)
{
std::clock_t start;
start = std::clock();
// your test
std::cout << "Time: " << (std::clock() - start) / (double)(CLOCKS_PER_SEC / 1000) << " ms" << std::endl;
return 0;
}
Good Luck ;)
祝你好运 ;)
回答by o11c
Using std::chrono
:
使用std::chrono
:
#include <chrono>
#include <thread>
#include <iostream>
// There are other clocks, but this is usually the one you want.
// It corresponds to CLOCK_MONOTONIC at the syscall level.
using Clock = std::chrono::steady_clock;
using std::chrono::time_point;
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using namespace std::literals::chrono_literals;
using std::this_thread::sleep_for;
int main()
{
time_point<Clock> start = Clock::now();
sleep_for(500ms);
time_point<Clock> end = Clock::now();
milliseconds diff = duration_cast<milliseconds>(end - start);
std::cout << diff.count() << "ms" << std::endl;
}
std::chrono
is C++11, std::literals
is C++14 (otherwise you need milliseconds(500)
).
std::chrono
是 C++11,std::literals
是 C++14(否则你需要milliseconds(500)
)。
回答by Aly
Turns out there is a version of time in boost 1.46 (just in different location). Thanks to @jogojapan for pointing it out.
原来在 boost 1.46 中有一个时间版本(只是在不同的位置)。感谢@jogojapan 指出。
It can be done like this:
可以这样做:
#include <boost/timer.hpp>
timer t;
<some stuff>
std::cout << t.elapsed() << std::endl;
Or alternatively using std libs as @Quentin Perez has pointed out (and I will accept as is what was originally asked)
或者也可以像@Quentin Perez 指出的那样使用 std libs(我会接受最初的要求)
回答by Aly
Building on Quentin Perez's solution, you can pass an arbitrary function to time using std::function and a lambda.
基于 Quentin Perez 的解决方案,您可以使用 std::function 和 lambda 将任意函数传递给 time。
#include <ctime>
#include <iostream>
#include <functional>
void timeit(std::function<void()> func) {
std::clock_t start = std::clock();
func();
int ms = (std::clock() - start) / (double) (CLOCKS_PER_SEC / 1000);
std::cout << "Finished in " << ms << "ms" << std::endl;
}
int main() {
timeit([] {
for (int i = 0; i < 10; ++i) {
std::cout << "i = " << i << std::endl;
}
});
return 0;
}
回答by valkn0t
You can use a long to hold the current time value as a start value, and then convert the current time to a double. here is some snippet code to use as an example.
您可以使用 long 来保存当前时间值作为起始值,然后将当前时间转换为 double。这是一些用作示例的片段代码。
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/timeb.h>
int main()
{
struct _timeb tStruct;
double thisTime;
bool done = false;
long startTime;
struct _timeb
{
int dstflag; // holds a non-zero value if daylight saving time is in effect
long millitm; // time in milliseconds since the last one-second hack
long time; // time in seconds since 00:00:00 1/1/1970
long timezone; // difference in minutes moving west from UTC
};
_ftime(&tStruct); // Get start time
thisTime = tStruct.time + (((double)(tStruct.millitm)) / 1000.0); // Convert to double
startTime = thisTime; // Set the starting time (when the function begins)
while(!done) // Start an eternal loop
{
system("cls"); // Clear the screen
_ftime(&tStruct); // Get the current time
thisTime = tStruct.time + (((double)(tStruct.millitm)) / 1000.0); // Convert to double
// Check for 5 second interval to print status to screen
cout << thisTime-startTime; // Print it.
}
}