2

I have the same single thread problem, which is something like a simple

#include<stdlib.h>
#include<iostream>
#include<stdio.h>
#include<time.h>
//#include<ctime.h>

int main(){

clock_t start=clock();
for (int i=0;i<100 000 000; i++){
//do something here
}

double duration = (clock()-start)/(double)CLOCKS_PER_SEC;
cout<<endl<<"Total consumption time is : "<<duration<<"seconds "<<endl;
// or use 
// printf("\n Total consumption time is : %lf seconds",duration);
system("pause");

return 0;
}

Occasionally when I was running them on different computer: older one with XP OS and Pentium V CPU, newer one with Win7 OS x86 and i7 CPU.

It is very strange that I found the older computer runs the same program significantly faster. Then I found it is general for other similar programs.

Why I got such result? Is it because XP OS is faster than Win7?

LCFactorization
  • 729
  • 1
  • 7
  • 15

2 Answers2

6

There are so many variables that play into the speed with which a program runs that it is impossible to tell from just your description. For example:

  • Did other programs run at the same time?
  • What is the clock speed of your processor?
  • How was the program compiled and which processor was it optimized for?
  • How long does the program run to begin with -- for example, is it within the accuracy of the timer?
  • Is the loop simple enough for the compiler to optimize it away?
  • Are you timing the entire program execution including loading it, or only the actual run time of the loop?
  • If you time the entire program execution including loading, how does the set of shared libraries that the program links to differ?

There are many other questions that make it impossible to answer your question in this generality.

Wolfgang Bangerth
  • 55,373
  • 59
  • 119
  • So virus should also be potential reasons? How can I identify the root causes? – LCFactorization Dec 04 '13 at 15:13
  • 1
    Sure, maybe a virus too. As for how to find the cause -- go down the list above and see how your systems differ. – Wolfgang Bangerth Dec 04 '13 at 15:44
  • Is it possible to compare the CPU clock of different computers? Do you have any idea how to implement it via C++? – LCFactorization Dec 05 '13 at 12:19
  • 1
    I'm not sure what you mean by compare the "CPU clock". I assume you mean "clock frequency", not "clock time". In that case, you can go to the Windows System Information and see what the CPUs name, generation number, clock frequency, etc is. I'm sure there is a way to query the current frequency from the power management system, but I don't know how to do that on Windows. – Wolfgang Bangerth Dec 05 '13 at 12:36
  • actually, the cpu clock frequency may be higher on the older processor – Andre Holzner Dec 05 '13 at 16:54
  • Yes. There are many factors, and some of them may cancel each other. – Wolfgang Bangerth Dec 06 '13 at 03:27
1

The faster CPU may be more than made up for by the heavier operating system. If you wanted a true comparison, you'd run the test with the same operating system on both computers.

Moreover, depending on what you're actually doing inside that loop, the program may spend more time on memory reads/writes than floating point operations, in which case the performance is more dependent on the cache than the processor clock speed.

Daniel Shapero
  • 10,263
  • 1
  • 28
  • 59