如何计算C+中的时间差+; 计算C++中时差的最好方法是什么?我对程序的执行速度进行计时,所以我对毫秒感兴趣。更好的是,秒。毫秒

如何计算C+中的时间差+; 计算C++中时差的最好方法是什么?我对程序的执行速度进行计时,所以我对毫秒感兴趣。更好的是,秒。毫秒,c++,C++,被接受的答案有效,但需要包括ctime或time.h,如评论中所述 参见函数 const clock_t begin_time = clock(); // do something std::cout << float( clock () - begin_time ) / CLOCKS_PER_SEC; const clock\u t begin\u time=clock(); //做点什么 std::cout只是一个旁注:如果您在Windows上运行,并且确实需要精确性,那么您

被接受的答案有效,但需要包括ctime或time.h,如评论中所述

参见函数

const clock_t begin_time = clock();
// do something
std::cout << float( clock () - begin_time ) /  CLOCKS_PER_SEC;
const clock\u t begin\u time=clock();
//做点什么

std::cout只是一个旁注:如果您在Windows上运行,并且确实需要精确性,那么您可以使用。它以纳秒为单位提供时间。

在开始时以毫秒为单位获取系统时间,在结束时再次获取系统时间,然后减去

要在POSIX中获取自1970年以来的毫秒数,您需要编写:

struct timeval tv;

gettimeofday(&tv, NULL);
return ((((unsigned long long)tv.tv_sec) * 1000) +
        (((unsigned long long)tv.tv_usec) / 1000));
SYSTEMTIME systime;
FILETIME filetime;

GetSystemTime(&systime);
if (!SystemTimeToFileTime(&systime, &filetime))
    return 0;

unsigned long long ns_since_1601;
ULARGE_INTEGER* ptr = (ULARGE_INTEGER*)&ns_since_1601;

// copy the result into the ULARGE_INTEGER; this is actually
// copying the result into the ns_since_1601 unsigned long long.
ptr->u.LowPart = filetime.dwLowDateTime;
ptr->u.HighPart = filetime.dwHighDateTime;

// Compute the number of milliseconds since 1601; we have to
// divide by 10,000, since the current value is the number of 100ns
// intervals since 1601, not ms.
return (ns_since_1601 / 10000);
要获取Windows上自1601起的毫秒数,请编写:

struct timeval tv;

gettimeofday(&tv, NULL);
return ((((unsigned long long)tv.tv_sec) * 1000) +
        (((unsigned long long)tv.tv_usec) / 1000));
SYSTEMTIME systime;
FILETIME filetime;

GetSystemTime(&systime);
if (!SystemTimeToFileTime(&systime, &filetime))
    return 0;

unsigned long long ns_since_1601;
ULARGE_INTEGER* ptr = (ULARGE_INTEGER*)&ns_since_1601;

// copy the result into the ULARGE_INTEGER; this is actually
// copying the result into the ns_since_1601 unsigned long long.
ptr->u.LowPart = filetime.dwLowDateTime;
ptr->u.HighPart = filetime.dwHighDateTime;

// Compute the number of milliseconds since 1601; we have to
// divide by 10,000, since the current value is the number of 100ns
// intervals since 1601, not ms.
return (ns_since_1601 / 10000);

如果您想规范化Windows答案,使其也返回自1970年以来的毫秒数,那么您必须将答案调整1164443600000毫秒。但是,如果你关心的只是经过的时间,那不是必需的。

< P>我会认真考虑使用Boost,特别是Booo::Posix:Time::PoTy:Booo::Posix:Time::TimeION(AT).< /P> 它是跨平台的,易于使用,以我的经验,它提供了操作系统提供的最高级别的时间分辨率。可能也很重要,;它提供了一些非常好的IO操作符

要使用它计算程序执行中的差异(微秒;可能是过度杀戮),它看起来像这样[浏览器编写,未测试]:

ptime time_start(microsec_clock::local_time());
//... execution goes here ...
ptime time_end(microsec_clock::local_time());
time_duration duration(time_end - time_start);
cout << duration << '\n';
ptime time_start(microsec_clock::local_time());
//... 这里执行死刑。。。
ptime time_end(微秒时钟::本地时间());
时间持续时间(时间结束-时间开始);

cout如果您在Unix上,您可以使用
time
获取执行时间:

$ g++ myprog.cpp -o myprog
$ time ./myprog
在Windows中:使用

//GetTickCount定义
#包括
int main()
{
DWORD dw1=GetTickCount();
//做点什么
DWORD dw2=GetTickCount();
cout您也可以使用。此方法可用于测量:

  • 全系统实时时钟
  • 全系统单调时钟
  • 每进程CPU时间
  • 每进程线程CPU时间
  • 代码如下:

    #include < time.h >
    #include <iostream>
    int main(){
      timespec ts_beg, ts_end;
      clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_beg);
      clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts_end);
      std::cout << (ts_end.tv_sec - ts_beg.tv_sec) + (ts_end.tv_nsec - ts_beg.tv_nsec) / 1e9 << " sec";
    }
    
    #包括
    #包括
    int main(){
    timespec ts_beg,ts_end;
    时钟获取时间(时钟进程、CPU使用时间ID和ts请求);
    时钟获取时间(时钟进程、CPU使用时间ID和ts结束);
    
    标准::coutboost 1.46.0及以上版本包括库:

    thread_clock类提供对真实线程挂钟的访问,即。 调用线程的实时CPU时间时钟。线程相对 当前时间可以通过调用线程_clock::now()获得

    #包括
    {
    ...
    使用名称空间boost::chrono;
    线程时钟::时间点开始=线程时钟::现在();
    ...
    线程时钟::时间点停止=线程时钟::现在();
    
    std::cout这似乎适用于英特尔Mac 10.7:

    #include <time.h>
    
    time_t start = time(NULL);
    
    
        //Do your work
    
    
    time_t end = time(NULL);
    std::cout<<"Execution Time: "<< (double)(end-start)<<" Seconds"<<std::endl;
    
    #包括
    时间\u t开始=时间(空);
    //做你的工作
    时间\u t结束=时间(空);
    
    如果您使用的是c++11,这里有一个简单的包装器(请参见此):

    #包括
    #包括
    班级计时器
    {
    公众:
    计时器():beg_(时钟::now()){}
    void reset(){beg_uu=clock_uu::now();}
    双经过()常量{
    返回std::chrono::duration\u cast
    (时钟::now()-beg..count();}
    私人:
    typedef std::chrono::高分辨率时钟时钟;
    typedef std::chrono::持续时间秒;
    标准:时钟:时间点;
    };
    
    对于*nix上的c++03:

    #include <iostream>
    #include <ctime>
    
    class Timer
    {
    public:
        Timer() { clock_gettime(CLOCK_REALTIME, &beg_); }
    
        double elapsed() {
            clock_gettime(CLOCK_REALTIME, &end_);
            return end_.tv_sec - beg_.tv_sec +
                (end_.tv_nsec - beg_.tv_nsec) / 1000000000.;
        }
    
        void reset() { clock_gettime(CLOCK_REALTIME, &beg_); }
    
    private:
        timespec beg_, end_;
    };
    
    #包括
    #包括
    班级计时器
    {
    公众:
    计时器(){clock_gettime(clock_REALTIME,&beg)}
    双时间(){
    时钟获取时间(时钟实时和结束时间);
    返回end_uu.tv_usec-beg_uu.tv_usec+
    (完tv-beg-tv-nsec)/100000000。;
    }
    void reset(){clock_gettime(clock_REALTIME,&beg)}
    私人:
    timespec beg_uu,end_uu;
    };
    
    用法示例:

    int main()
    {
        Timer tmr;
        double t = tmr.elapsed();
        std::cout << t << std::endl;
    
        tmr.reset();
        t = tmr.elapsed();
        std::cout << t << std::endl;
        return 0;
    }
    
    intmain()
    {
    定时器tmr;
    双t=tmr.appeased();
    std::cout如果您正在使用:

    tstart = clock();
    
    // ...do something...
    
    tend = clock();
    
    然后,您将需要以下内容以秒为单位获取时间:

    time = (tend - tstart) / (double) CLOCKS_PER_SEC;
    

    对我来说,最简单的方法是:

    #include <boost/timer.hpp>
    
    boost::timer t;
    double duration;
    
    t.restart();
    /* DO SOMETHING HERE... */
    duration = t.elapsed();
    
    t.restart();
    /* DO OTHER STUFF HERE... */
    duration = t.elapsed();
    
    #包括
    boost::定时器t;
    双倍持续时间;
    t、 重启();
    /*在这里做点什么*/
    持续时间=t.经过();
    t、 重启();
    /*在这里做其他事情*/
    持续时间=t.经过();
    
    使用这段代码,您不必执行经典的
    end-start


    享受你最喜欢的方法。

    我添加此答案是为了澄清,接受的显示CPU时间可能不是你想要的时间。因为根据,有CPU时间和挂钟时间。挂钟时间是显示实际运行时间的时间,而不考虑其他进程共享的CPU等任何其他条件例如,我使用多个处理器来执行某项任务,CPU时间很长18s,而实际的挂钟时间实际上需要2s

    要得到你实际做的时间

    #include <chrono>
    
    auto t_start = std::chrono::high_resolution_clock::now();
    // the work...
    auto t_end = std::chrono::high_resolution_clock::now();
    
    double elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end-t_start).count();
    
    #包括
    自动启动=标准::时钟::高分辨率时钟::现在();
    //工作。。。
    自动t_end=std::chrono::高分辨率时钟::now();
    双运行时间\u ms=std::chrono::duration(t_end-t_start).count();
    
    Dupe,但现在无法链接投票结果太少,太晚了。我得到了一个可行的答案。尽管尝试得不错。顺便说一句,我也找不到链接。这是针对windows的?然后尝试GetTickCount(windows API)罗伯特:幸运的是,因为新的帖子提供了更多的答案,我选择了其中一个。说真的,我质疑关闭dup帖子的价值。如果在第一篇帖子中没有提到一些解决方案呢?开发了新技术?因为标题不同而找不到?@JackBeNimble一直是几个“dup”的接收端这并不完全是骗人的(可能是那些可能很快读到问题并做了标记的人,因为它听起来与另一个问题相似),我非常同意你的观点……可能是
    #include <chrono>
    
    auto t_start = std::chrono::high_resolution_clock::now();
    // the work...
    auto t_end = std::chrono::high_resolution_clock::now();
    
    double elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end-t_start).count();