[英]c++ measuring execution time
EDIT: the code was modified following the advices givven to me. 编辑:按照给我的建议修改了代码。
The purpose of the following code is to measure the execution time of a simple operation (1+1) and of a call to a function who does nothing (foo). 以下代码的目的是测量简单操作(1 + 1)和不执行任何操作的函数(foo)的执行时间。
The code compiles and seeme to work properly, but the results i am getting are weird - it seems the basic operation requires about the same time as the function call, and most times it take even a little bit more time. 代码可以编译并且可以正常工作,但是我得到的结果很奇怪-似乎基本操作所需的时间与函数调用大约相同,而且大多数情况下甚至需要更多时间。
Another issue is that the execution time do not seem to be affected by the number of itterrations - it could be 100K or 100M but the times are basically the same. 另一个问题是执行时间似乎不受传输速率的影响-可能是100K或100M,但是时间基本上是相同的。 Also, if I pick a number over one billion, it seeme the execuition time decreases. 另外,如果我选择一个超过十亿的数字,似乎执行时间会减少。
i could not find on google what i need - i must time a simple opertaion and an empty function, which should be in the same file as measureTimes() is, or at least - each measuring function should be contained wholy in a single file (and besides, moving foo() to another file actually reduced times so far 我在Google上找不到我需要的东西-我必须计时一个简单的操作符和一个空函数,该函数应该与measureTimes()放在同一文件中,或者至少-每个测量函数都应该单独包含在一个文件中(此外,到目前为止,将foo()移至另一个文件实际上减少了时间
right now, this is the output of my program: 现在,这是我程序的输出:
instructionTimeNanoSecond: 1.9 structionTimeNanoSecond:1.9
functionTimeNanoSecond: 1.627 functionTimeNanoSecond:1.627
#include <iostream>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <math.h>
#include "osm.h"
#define INVALID_ITERATIONS 0
#define DEFAULT_ITERATIONS 1000
#define HOST_NAME_LEN 100
#define TO_NANO 1000
#define TO_MICRO 1000000
#define ROLL 10
using namespace std;
int main()
{
unsigned int iterations = (unsigned int) pow( 10, 9);
measureTimes( iterations, iterations, iterations, iterations );
return 0;
}
void foo();
timeMeasurmentStructure measureTimes (unsigned int operation_iterations,
unsigned int function_iterations,
)
{
double functionTimeNanoSecond;
functionTimeNanoSecond = osm_function_time( function_iterations);
cout << "functionTimeNanoSecond: " << functionTimeNanoSecond << "\n";;
double instructionTimeNanoSecond;
instructionTimeNanoSecond = osm_operation_time( operation_iterations);
cout << "instructionTimeNanoSecond: " << instructionTimeNanoSecond << "\n";
}
double osm_operation_time(unsigned int iterations)
{
timeval start;
gettimeofday(&start, NULL);
int x=0;
for( int i = 0; i < iterations/ROLL; i++ )
{
x=x+1;
x=x+1;
x=x+1;
x=x+1;
x=x+1;
x=x+1;
x=x+1;
x=x+1;
x=x+1;
x=x+1;
}
timeval end;
gettimeofday(&end, NULL);
timeval diff;
timersub(&end, &start, &diff);
// double micro_seconds =(double) (end.tv_usec - start.tv_usec);
double ret =((double) diff.tv_sec*TO_MICRO + diff.tv_usec) / ((double) iterations);
return ret * TO_NANO;
}
double osm_function_time(unsigned int iterations)
{
timeval start;
gettimeofday(&start, NULL);
for( int i = 0; i < iterations/ROLL; i++ )
{
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
foo();
}
timeval end;
gettimeofday(&end, NULL);
timeval diff;
timersub(&end, &start, &diff);
//double micro_seconds = (double)( (end.tv_sec - start.tv_sec)*TO_MICRO+(end.tv_usec - start.tv_usec));
double ret =((double) diff.tv_sec*TO_MICRO + diff.tv_usec) / ((double) iterations);
return ret * TO_NANO;
}
void foo()
{
return;
}
In this case compiler is optimizing your code and probably doesn't even execute the for loop because the code inside 1+1
is not affecting the rest of the program. 在这种情况下,编译器正在优化您的代码,甚至可能不会执行for循环,因为1+1
内的代码不会影响程序的其余部分。
If you do something like: 如果您执行以下操作:
int x = 0;
for(int i=0;i<iteration;i++)
x = x + 1;
cout << x;
you will get more real results 你会得到更多真实的结果
您总是在计算平均值..迭代次数应大致相同
double ret = diff.tv_usec / ((double) iterations);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.