I read at a programming blog that a program with a large number of print
statements takes more time to finish it's execution as it has to send the data to output buffer continuously. I am solving the ProjectEuler problem #12. I have solved it successfully. Following are the codes
#include <iostream>
#include <ctime>
#include <boost/multiprecision/cpp_int.hpp>
using big_int = boost:: multiprecision:: cpp_int;
using namespace std;
clock_t start = clock();
big_int get_num(big_int num) {
return num*(num + 1) / 2;
}
big_int num_of_factors(big_int num) {
big_int count = 0;
for(big_int i = 1; i <= sqrt(num); ++i) {
if(num % i == 0) {
if(num / i == i)
count += 1;
else
count += 2;
}
}
return count;
}
int main() {
big_int num = 1;
while(true) {
if(num_of_factors(get_num(num)) >= 500) {
cout << get_num(num);
break;
}
++num;
}
double elapsedTime = static_cast<double>(clock() - start) / CLOCKS_PER_SEC;
return 0;
}
Time Elapsed: /home/arun/CLionProjects/DebugTutorial/cmake-build-debug/DebugTutorial 76576500 Time is 106.029 Seconds Process finished with exit code 0
Here is the second snippet. Notice the cout
statement in main()
after ++num
#include <iostream>
#include <ctime>
#include <boost/multiprecision/cpp_int.hpp>
using big_int = boost:: multiprecision:: cpp_int;
using namespace std;
clock_t start = clock();
big_int get_num(big_int num) {
return num*(num + 1) / 2;
}
big_int num_of_factors(big_int num) {
big_int count = 0;
for(big_int i = 1; i <= sqrt(num); ++i) {
if(num % i == 0) {
if(num / i == i)
count += 1;
else
count += 2;
}
}
return count;
}
int main() {
big_int num = 1;
while(true) {
if(num_of_factors(get_num(num)) >= 500) {
cout << get_num(num);
break;
}
++num;
cout << get_num(num) << endl; //Notice this
}
double elapsedTime = static_cast<double>(clock() - start) / CLOCKS_PER_SEC;
cout << endl << "Time is " << elapsedTime << " Seconds";
return 0;
}
Time Elapsed: Time is 110.946 Seconds Process finished with exit code 0
What exactly I want to know is why there is not a significant difference between the execution time in these two codes. While there is a print
statement in another version.
For example, Look at these codes:
1) Without print
statement:
#include <iostream>
#include <ctime>
#include <boost/multiprecision/cpp_int.hpp>
using big_int = boost:: multiprecision:: cpp_int;
using namespace std;
clock_t start = clock();
int main() {
for(big_int i = 0; i < 10000000; ++i) {
}
double elapsedTime = static_cast<double>(clock() - start) / CLOCKS_PER_SEC;
cout << endl << "Time is " << elapsedTime << " Seconds";
return 0;
}
Execution Time:
Time is 0.370125 Seconds
2) With print
statement
#include <iostream>
#include <ctime>
#include <boost/multiprecision/cpp_int.hpp>
using big_int = boost:: multiprecision:: cpp_int;
using namespace std;
clock_t start = clock();
int main() {
for(big_int i = 0; i < 10000000; ++i) {
cout << i << endl;
}
double elapsedTime = static_cast<double>(clock() - start) / CLOCKS_PER_SEC;
cout << endl << "Time is " << elapsedTime << " Seconds";
return 0;
}
Execution Time:
Time is 26.8947 Seconds
I want to know that like these two codes why there is not a significant difference in the execution time of the codes mentioned in ProjectEuler solution codes.