2

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.

Community
  • 1
  • 1
Arun Suryan
  • 1,615
  • 1
  • 9
  • 27
  • 1
    What kind of slow down were you expecting? Double, triple? It's all relative. You might be getting the slowdown you think, but your measuring includes the workings of the actual algorithm. – DeiDei Apr 06 '20 at 18:00
  • 2
    Your time is being dominated by calls to `num_of_factors`. Look at a trace of your program under `perf` and you'll see it. – user14717 Apr 06 '20 at 21:58
  • you should also turn off syncing with printf with `std::ios::sync_with_stdio(false)` because that's also one of the reasons cout is slow. See [why is cin/cout slower than scanf/ printf](https://stackoverflow.com/a/41715739/995714) – phuclv Apr 07 '20 at 05:32

2 Answers2

2

As posted by @Blastfurnace in the other question referring to this. The reason is that Here I am comparing an empty loop and a loop that prints something. No work versus some work. My previous codes compare a loop that does a lot of work and a loop that does the same work but adds a single print statement. My running time in other code is dominated by the computation, not the single additional print.

Arun Suryan
  • 1,615
  • 1
  • 9
  • 27
1

There may not be a "significant difference" in the execution time of each of the code examples. However, there is a difference. In the examples the difference is trivial. However, if the print statement was more actively called in the program the difference in execution time would be extremely significant.