寻找基准代码片段 (c++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/483164/
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
Looking for benchmarking code snippet (c++)
提问by Mizipzor
Some loading routines in my program takes to long to complete. I want a quick small snippet for checking how long a function took to execute. By small I mean "preferably without 3rd party libraries".
我的程序中的一些加载例程需要很长时间才能完成。我想要一个快速的小片段来检查函数执行的时间。小我的意思是“最好没有第 3 方库”。
Maybe something as simple as taking the system time?
也许像占用系统时间一样简单?
start = current_system_time()
load_something()
delta = current_system_time()-start
log_debug("load took "+delta)
Edit:Target OS in question is Windows.
编辑:有问题的目标操作系统是 Windows。
采纳答案by Robert Gould
Your answer: Yes
你的回答:是的
Caveat: That WON'Twork in multihtreaded code or multiple core machines, you need a robust wall-clock timer. So I recommend you use omp's wallclock. OMP is included with VC and GCC, and most compilers and its a standard you don't need to worry about disappearing
警告:这不会在multihtreaded代码或多个核心机的工作,你需要一个强大的挂钟计时器。所以我建议你使用 omp 的挂钟。OMP 包含在 VC 和 GCC 中,大多数编译器及其标准您不必担心会消失
#include <omp.h>
// Starting the time measurement
double start = omp_get_wtime();
// Computations to be measured
...
// Measuring the elapsed time
double end = omp_get_wtime();
// Time calculation (in seconds)
回答by Vadim Ferderer
#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
namespace win32 {
#include <windows.h>
}
class timer
{
win32::LARGE_INTEGER start_time_;
public:
timer() { QueryPerformanceCounter( &start_time_ ); }
void restart() { QueryPerformanceCounter( &start_time_ ); }
double elapsed() const
{
win32::LARGE_INTEGER end_time, frequency;
QueryPerformanceCounter( &end_time );
QueryPerformanceFrequency( &frequency );
return double( end_time.QuadPart - start_time_.QuadPart )
/ frequency.QuadPart;
}
};
#else
#include <ctime>
class timer
{
clock_t _start_time;
public:
timer() { _start_time = clock(); }
void restart() { _start_time = clock(); }
double elapsed() const
{
return double(clock() - _start_time) / CLOCKS_PER_SEC;
}
};
#endif
template< typename Func >
double measure_time( Func f )
{
timer t;
f();
return t.elapsed();
}
回答by jvasak
This is a quick and dirty way to time a block of C/C++ code. You need to #include <sys/time.h>
, which should be a standard header...
这是一种对 C/C++ 代码块计时的快速而肮脏的方法。你需要#include <sys/time.h>
,这应该是一个标准的标题......
struct timeval start, end;
gettimeofday(&start, NULL);
// benchmark code
gettimeofday(&end, NULL);
long long time = (end.tv_sec * (unsigned int)1e6 + end.tv_usec) -
(start.tv_sec * (unsigned int)1e6 + start.tv_usec);
This should give 1-2μs resolution on modern Linux systems (what OS are you using?), which means that it's not well suited to learning much for items taking of <10μs. However, you don't seem to be in that situation.
这应该在现代 Linux 系统上提供 1-2μs 的分辨率(您使用的是什么操作系统?),这意味着它不太适合学习 <10μs 的项目。但是,您似乎并不处于这种情况。
Update:Based on specified OS... Windows implementation of gettimeofday()
更新:基于指定的操作系统... gettimeofday() 的 Windows 实现
回答by jheriko
I use a class for this, its designed to measure the time taken to execute a function and write that to a uth-16le text file (I need to update this to use a new class i made for this... but nm).
我为此使用了一个类,它旨在测量执行函数所需的时间并将其写入 uth-16le 文本文件(我需要更新它以使用我为此创建的新类......但是 nm)。
Create a new instance at the top of a function, e.g. jProfiler(L"myFunction") and the cleanup at the end of the function will do the rest, if you want to be sure though new and delete it yourself. Its a bit overkill for a small test, but might help:
在函数顶部创建一个新实例,例如 jProfiler(L"myFunction") 并且函数末尾的清理将完成其余的工作,如果您想确定虽然是新的并自己删除它。对于小型测试来说有点矫枉过正,但可能会有所帮助:
// start header
/* jProfiler class by Semi Essessi
*
* (class description goes here)
*
*/
#ifndef __JPROFILER_H
#define __JPROFILER_H
#include <stdio.h>
#include <windows.h>
class jProfiler
{
private:
wchar_t* str;
LARGE_INTEGER start;
LARGE_INTEGER tps;
LARGE_INTEGER buf;
static FILE* f;
static int instCount;
static void Initialise();
static void Shutdown();
public:
jProfiler(const wchar_t* msg);
~jProfiler();
};
#endif
// - end header
/* jProfiler class by Semi Essessi
*
* (class description goes here)
*
*/
#include "jProfiler.h"
#include <windows.h>
FILE* jProfiler::f = 0;
int jProfiler::instCount = 0;
jProfiler::jProfiler(const wchar_t* msg)
{
// constructor code for menuVar
int i = (int)wcslen(msg)+1;
str = new wchar_t[i];
memcpy(str, msg, sizeof(wchar_t)*i);
str[i-1] = 0;
QueryPerformanceFrequency(&tps);
QueryPerformanceCounter(&start);
instCount++;
Initialise();
}
jProfiler::~jProfiler()
{
// destructor code for menuVar
QueryPerformanceCounter(&buf);
// work out change in time
double dt=((float)buf.QuadPart - (float)start.QuadPart)/(float)tps.QuadPart;
fwprintf(f, L"%s : %.20f\r\n", str, dt);
if(str) delete[] str;
instCount--;
Shutdown();
}
void jProfiler::Initialise()
{
if(!f)
{
f = _wfopen(L"profilerlog.txt", L"wb");
unsigned short a = 0xFEFF;
fwrite(&a, sizeof(unsigned short), 1, f);
}
}
void jProfiler::Shutdown()
{
if(instCount==0) if(f) fclose(f);
}
回答by burner
I have a benchmark.hppheader in my sweet.hpplibrary. It has two benchmark tools. The first is a simple manual start stop time taker
我的sweet.hpp库中有一个benchmark.hpp标头。它有两个基准测试工具。第一个是简单的手动开始停止计时
Bench b;
...
b.stop();
b.milli(); // returns an uint with passed millisec. Also has sec and micro sec
The other one is a bit more sophisticated. You write functions or block statements like this.
另一种更复杂一些。您可以像这样编写函数或块语句。
void myFunc() {
BENCH(someName);
...
}
And in the end call sweet::Benchmark::printResults();
to have the time spend and the number of calls printed.
最后sweet::Benchmark::printResults();
调用打印花费的时间和调用次数。
Edit: I added a function so you can call it like this.
编辑:我添加了一个函数,所以你可以这样调用它。
double c = BENCHMARK_CNT(25, yourFunctionCallHere());