I was trying out the time -p command on Linux and I wrote some code to waste CPU cycles:
#include <iostream>
using namespace std;
int main() {
long int c;
long int ss;
for(c = 0;c < 10000000;c++) {
ss += c*c;
}
cout<<ss<<endl;
return 0;
}
I noticed something funny after running it a few times, however:
me@octopus:~/Desktop> ./test
1292030741067648912
me@octopus:~/Desktop> ./test
1292030742538841328
me@octopus:~/Desktop> ./test
1292030742228685600
me@octopus:~/Desktop> ./test
1292030740402651312
me@octopus:~/Desktop> ./test
1292030740207543344
me@octopus:~/Desktop> ./test
1292030740346553856
me@octopus:~/Desktop> ./test
1292030741629275040
me@octopus:~/Desktop> ./test
1292030740397307072
me@octopus:~/Desktop> ./test
1292030742928964784
me@octopus:~/Desktop> ./test
1292030741780094096
Not only did I not get the same number every time, as I would expect, I didn’t get the same number even once. What is going on here?
You have not initialized ss to zero therefore its initial value is undefined. You need: