时间一个函数在C ++ [英] Time a function in C++

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

问题描述



这里是我有的:

 #include< iostream> 
#include< chrono>
using timepoint = std :: chrono :: steady_clock :: time_point;

float elapsed_time [100];

//运行函数和计数时间
for(int k = 0; k <100; k ++){

//开始计时器
const timepoint clock_start = chrono :: system_clock :: now();

//运行函数
Recursive_Foo();

//停止计时器
const timepoint clock_stop = chrono :: system_clock :: now();

//计算时间(以毫秒为单位)
chrono :: duration< double,std :: milli> timetaken = clock_stop - clock_start;
elapsed_time [k] = timetaken.count();
}

for(int l = 0; l< 100; l ++){
cout<<Array:< <<< elapsed_time [1]<<ms<< endl;
}

这是编译,但我认为多线程阻止它正常工作。输出产生不规则间隔的时间,例如:

 数组:0时间:0 ms 
数组: 0 ms
数组:2时间:15.6 ms
数组:3时间:0 ms
数组:4时间:0 ms
数组:5时间:0 ms
数组:6时间:15.6 ms
数组:7时间:0 ms
数组:8时间:0 ms

$ b b

我需要使用某种互斥锁吗?



EDIT



也许人们正在建议使用 high_resolution_clock steady_clock ,但所有三个产生相同的不规则结果。



此解决方案似乎产生了真正的结果:如何使用QueryPerformanceCounter?但是我不清楚为什么。此外, http://gamedev.stackexchange。 com / questions / 26759 / best-way-to-get-elapsed-time-in-miliseconds-in-windows 效果很好。似乎是一个Windows实现问题。

解决方案

微软有一个很好的,干净的解决方案微秒,通过: MSDN

  #include< windows.h> 

LONGLONG meas_activity_high_resolution_timing()
{
LARGE_INTEGER StartingTime,EndingTime,ElapsedMicroseconds;
LARGE_INTEGER频率;

QueryPerformanceFrequency(& Frequency);
QueryPerformanceCounter(& StartingTime);

//定时活动

QueryPerformanceCounter(& EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;


//
//我们现在已经过去的滴答数,以及
//每秒滴答数。我们使用这些值
//来转换已经过的微秒数。
//为了防止精度损失,我们将
//转换为微秒*之后再除以每秒的ticks。
//

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


I'd like to time how long a function takes in C++ in milliseconds.

Here's what I have:

#include<iostream>
#include<chrono>           
using timepoint = std::chrono::steady_clock::time_point;

float elapsed_time[100];

// Run function and count time
for(int k=0;k<100;k++) {

    // Start timer
    const timepoint clock_start = chrono::system_clock::now();

    // Run Function
    Recursive_Foo();

    // Stop timer
    const timepoint clock_stop = chrono::system_clock::now();

    // Calculate time in milliseconds
    chrono::duration<double,std::milli> timetaken = clock_stop - clock_start;
    elapsed_time[k] = timetaken.count();
}

for(int l=0;l<100;l++) {
    cout<<"Array: "<<l<<" Time: "<<elapsed_time[l]<<" ms"<<endl;
}

This compiles but I think multithreading is preventing it from working properly. The output produces times in irregular intervals, e.g.:

Array: 0 Time: 0 ms
Array: 1 Time: 0 ms
Array: 2 Time: 15.6 ms
Array: 3 Time: 0 ms
Array: 4 Time: 0 ms
Array: 5 Time: 0 ms
Array: 6 Time: 15.6 ms
Array: 7 Time: 0 ms
Array: 8 Time: 0 ms

Do I need to use some kind of mutex lock? Or is there an easier way to time how many milliseconds a function took to execute?

EDIT

Maybe people are suggesting using high_resolution_clock or steady_clock, but all three produce the same irregular results.

This solution seems to produce real results: How to use QueryPerformanceCounter? but it's not clear to me why. Also, http://gamedev.stackexchange.com/questions/26759/best-way-to-get-elapsed-time-in-miliseconds-in-windows works well. Seems to be a Windows implementation issue.

解决方案

Microsoft has a nice, clean solution in microseconds, via: MSDN

#include <windows.h>

LONGLONG measure_activity_high_resolution_timing()
{
    LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
    LARGE_INTEGER Frequency;

    QueryPerformanceFrequency(&Frequency); 
    QueryPerformanceCounter(&StartingTime);

    // Activity to be timed

    QueryPerformanceCounter(&EndingTime);
    ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;


    //
    // We now have the elapsed number of ticks, along with the
    // number of ticks-per-second. We use these values
    // to convert to the number of elapsed microseconds.
    // To guard against loss-of-precision, we convert
    // to microseconds *before* dividing by ticks-per-second.
    //

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

这篇关于时间一个函数在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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