Time difference with cout and without cout in C++

2020-04-18 09:16发布

问题:

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.

回答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.



回答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.



标签: c++ boost