C++ 如何在没有boost::timer的情况下以毫秒为单位对函数计时
我使用的是boost 1.46,它不包括boost::timer,还有什么其他方法可以计时我的函数 我目前正在这样做:C++ 如何在没有boost::timer的情况下以毫秒为单位对函数计时,c++,boost,timer,std,timing,C++,Boost,Timer,Std,Timing,我使用的是boost 1.46,它不包括boost::timer,还有什么其他方法可以计时我的函数 我目前正在这样做: time_t now = time(0); <some stuff> time_t after = time(0); cout << after - now << endl; time\u t now=time(0); 时间=时间(0); 在linux或Windows中不能: #include <ctime> #includ
time_t now = time(0);
<some stuff>
time_t after = time(0);
cout << after - now << endl;
time\u t now=time(0);
时间=时间(0);
在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;
}
#包括
#包括
int
主(整型,常量字符**)
{
std::时钟未启动;
开始=标准::时钟();
//你的测试
std::cout证明Boost1.46中有一个版本的时间(只是在不同的位置)
@感谢乔加潘指出这一点
可以这样做:
#include <boost/timer.hpp>
timer t;
<some stuff>
std::cout << t.elapsed() << std::endl;
#包括
定时器t;
std::cout您可以使用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.
}
}
#包括
#包括
#包括
#包括
#包括
int main()
{
结构_timebtstruct;
这次加倍;
bool done=false;
长启动时间;
结构时间b
{
int dstflag;//如果夏令时生效,则保留非零值
long millitm;//上次一秒钟黑客攻击后的时间(毫秒)
长时间;//自1970年1月1日00:00:00以来的时间(秒)
长时区;//从UTC向西移动的分钟差
};
_ftime(&tStruct);//获取开始时间
thistTime=tStruct.time+((双精度)(tStruct.millitm))/1000.0;//转换为双精度
startTime=ThistTime;//设置开始时间(函数开始时)
while(!done)//启动一个永久循环
{
系统(“cls”);//清除屏幕
_ftime(&tStruct);//获取当前时间
thistTime=tStruct.time+((双精度)(tStruct.millitm))/1000.0;//转换为双精度
//检查5秒间隔以将状态打印到屏幕
基于Quentin Perez的解决方案,您可以使用std::function和lambda将任意函数传递给时间
#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;
}
#包括
#包括
#包括
void timeit(std::function func){
std::clock_t start=std::clock();
func();
int-ms=(std::clock()-start)/(double)(每秒时钟数/1000);
std::cout使用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::time_点;
使用std::chrono::duration\u cast;
使用std::chrono::毫秒;
使用名称空间std::literals::chrono_literals;
使用std::this_线程::sleep_for;
int main()
{
时间点开始=时钟::现在();
睡眠时间(500ms);
时间点结束=时钟::现在();
毫秒差=持续时间(结束-开始);
std::coutstd::chrono
??@Pubby是对的,如果你可以访问C++11,std::chrono
是一条路。你的目标平台是什么?Win32
?Linux
?我使用的是Ubuntu,G++编译器boost 1.46不包括boost::timer
。你确定吗?我还没有测试,但有文档n对于1.46.0中的计时器:请注意,这测量的是CPU时间,而不是经过的时间。例如,如果函数休眠,则不会测量这一时间。这是CPU时间,在休眠或多线程时会出现问题如果确实需要CPU时间,则应使用clock\u gettime
并指定是需要进程时间还是线程时间,或者使用getrusage
并指定是否要包含子项。问题标题为“无boost::timer
”@Progo更具体地说,问题说使用boost 1.46是可以的。(请注意,此答案由原始问题的作者给出)这很有帮助,谢谢。我用高分辨率时钟
代替时钟
,用纳秒
代替毫秒
,为一些关键部分函数调用计时,每个调用只需几百纳秒。@offerit32高分辨率时钟
完全错误,因为它不能保证d表示稳定;在Linux上,它是系统时钟的别名。但是稳定时钟也有纳秒分辨率,我只是将持续时间
转换为任意值以用于打印目的。