在程序执行中,在C ++中使用cout和不使用cout的时差 [英] Time difference with cout and without cout in C++ in program execution

查看:89
本文介绍了在程序执行中,在C ++中使用cout和不使用cout的时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个编程博客上读到,包含大量print语句的程序需要更多时间才能完成其执行,因为它必须将数据连续发送到输出缓冲区.我正在解决ProjectEuler问题#12.我已经成功解决了.以下是代码

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;
}

已用时间: /home/arun/CLionProjects/DebugTutorial/cmake-build-debug/DebugTutorial 76576500 时间是106.029秒 流程结束,退出代码为0

Time Elapsed: /home/arun/CLionProjects/DebugTutorial/cmake-build-debug/DebugTutorial 76576500 Time is 106.029 Seconds Process finished with exit code 0

这是第二个片段.注意++ num

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;
}

已用时间: 时间是110.946秒 流程结束,退出代码为0

Time Elapsed: Time is 110.946 Seconds Process finished with exit code 0

我确切想知道的是为什么这两个代码的执行时间之间没有显着差异.在另一个版本中有一个print语句.

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:

#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;
}

执行时间:

#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;
}

执行时间:

我想知道像这两个代码一样,为什么ProjectEuler解决方案代码中提到的代码的执行时间没有显着差异.

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.

推荐答案

@Blastfurnace在另一个与此有关的问题中发布了该解决方案. 原因是在这里我正在比较一个空循环和一个打印某些内容的循环.没有工作与一些工作.我之前的代码比较了执行大量工作的循环和执行相同工作但添加单个print语句的循环.我在其他代码中的运行时间主要取决于计算,而不是单张打印纸.

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 ++中使用cout和不使用cout的时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆