If that's the one you entered (the one in the count object, not the count2 object), then doing a similar thing in C++ results in times under the resolution of my timer, for all lengths. :think:
Ok so if you compiler does any optimization you will see your times drop to 0ms if you never use the variable you assign to out of the loop.
If you do use it out side of the loop you get times exactly matching Trace's times (at least on my laptop). What that means is that the Java compiler Trace used did not optimize as it should have (his should have resulted in 0ms for any input number).
The Code I used to test this:
Code:#include <iostream>
#include <ctime>
using namespace std;
int main()
{
clock_t start, end, elapsed;
unsigned char foo;
long count(0), temp(0), temp2;
do
{
cout << "How many iterations do you want to do?\n";
cin >> temp;
count = ++temp;
start = clock();
while(count)
{
temp2 = count--;
}
end = clock();
elapsed = end - start;
cout << "For " << temp2 << " loops (count from 0 to " << temp - 1 << ")\n We took: " << elapsed << " ms\n"
<< "Would you like to run again?(y/n)\n";
cin >> foo;
}
while(foo == 'y');
return 0;
}