Execution time of program is "5E-006"?
double SumOfSquare()()
{
int i;
double T3,total=0;
for(i=0;i<200;i++) {
clock_t st开发者_如何转开发art = clock();
int n=100,sum=0;
for(int i =1;i<=n;i++) {
sum=sum+i*i;
}
clock_t end = clock();
T3=double(end-start)/(double) CLOCKS_PER_SEC;
total=total+T3;
}
T3=total/200;
return T3;
}
int main()
{
double T3=SumOfSquare();
cout<<T3<<endl;
return 0;
}
This code is supposed to return a value for execution time of that code instead it returns some weird out put such as "5e-006" instead of the execution time. Why?
5e-006
is the same thing as 5 * 10^-6
, or 0.000005
. What makes you think that isn't the execution time?
(5e-006
is a number written in E notation.)
T3 is a double and its value is 5 microseconds, so nothing is wrong.
Have a look at manipulators. They are used to format the output stream so you may get a more sensible looking result.
5e-006 is simply the standard exponential notation for 5 * 10-6, i.e. 0.000005. This is the same as 6 µs.
As others have already pointed out, you're getting 5 microseconds as a result, which seems at least reasonable as the time.
I would, however, compute the time a bit differently. I'd accumulate the number of "ticks" for the loop, then convert the total to seconds:
static const int iterations = 200;
clock_t total=0;
double seconds;
for(i=0;i<iterations;i++) {
clock_t start = clock();
int n=100,sum=0;
for(int i =1;i<=n;i++) {
sum=sum+i*i;
}
total += clock() - start;
}
return total/double(CLOCKS_PER_SEC*iterations);
Instead of a floating point division and floating point addition every iteration, this does an integer addition each iteration, and a single floating point division at the very end. On low-end hardware, this is likely to be faster. More importantly, it's likely to be more accurate, almost regardless of hardware -- adding a long list of small numbers in floating point is one of the cases that frequently leads to a substantial loss of precision.
The obvious answer is that your code only takes 5 microseconds
to execute. Probably because you never use sum
, so the
compiler will eliminate any code used to modify its value (and
thus the inner loop). More than anything else, your measurement
is probably determined by the granularity of clock
. You also
want to measure over as large a period as possible: I'd be
suspicious of the measurements if the two calls to clock
are
less than about 5 minutes a part (but of course, I'll use a much
shorter interval when debugging the program:-)). My solution
(and I'm not saying it's perfect) has generally been to put the
code to be measured in a virtual function, in a derived class,
with the function in the base class doing nothing, something
like:
class Base
{
static int ourCount;
static double ourTare;
virtual void doRun();
public:
double run();
static void setCount( int count );
};
int Base::ourCount = 0;
double Base::ourTare = 0.0;
void Base::doRun() {}
double Base::run()
{
clock_t start = clock();
for ( int count = ourCount; count > 0; -- count )
doRun();
clock_t end = clock();
return (static_cast<double>(end - start) / CLOCKS_PER_SEC - ourTare;
}
void Base::setCount( int count )
{
ourCount = count;
ourTare = 0.0;
// The following has been sufficient in the past. If the
// compiler inlines Base::run, however, it could be
// insufficent. (In my own code, Base::run is in a
// separate translation unit.)`
ourTare = Base().run();
}
class Derived
{
int d;
virtual void doRun();
public:
};
void Derived::doRun()
{
int sum = 0;
for ( int i = 1; i <= 100; ++ i ) {
sum += i * i;
}
d = sum;
}
You then call Base::setCount
with the count (for something
simple like this, anything less than around a million is
useless), create an instance of Derived, and call run
on it to
obtain the total time in seconds. You can divide it by the
count if you want the time per iteration.
(A more flippant answer would be that the program outputs
"5e-006"
because the compiler is broken. That's not a legal
output for any floating point value in C++.)
精彩评论