【问题标题】:C++ print progress with minimal impact on runtimeC++ 打印进度,对运行时影响最小
【发布时间】:2015-08-12 13:26:04
【问题描述】:

我有一个运行很长的循环的 C++ 程序。它有大约 500000 次迭代。 我想每 5% 打印一次进度。

到目前为止,我所拥有的如下。 这样做的问题是它会一遍又一遍地写同一行(0%,%5等)。这是因为在将百分比截断为整数后,有 1000 次迭代将达到 5 的倍数。这使程序明显变慢。

相反,如果我不截断为整数,那么percent % 5.0 的结果不太可能正好是 0。

如何打印对运行时影响最小的进度指示器?

// Counters for progress indicator
int i = 0;
float totalWindows = (float)(win.nX * win.nY);
int percent;

while (win.next() == 0)
{
    // Read the data

    // Nicely formatted progress indicator
    i++;
    percent = (i / totalWindows) * 100;
    if (percent % 5 == 0)
    {
        std::cout << "\r" << std::string(percent/5, '|') << percent << "%";
        std::cout.flush();
    }



}

编辑: 感谢您的回答。我选择了 christophes',它的指令数量最少。 它减少了 25% 的运行时间,非常显着!

【问题讨论】:

    标签: c++ console progress


    【解决方案1】:

    考虑到totalWindows 似乎保持不变,而integer 的增量/减量可能会比许多转换为int 的double 操作更快,我建议:

    // Counters for progress indicator
    int i = 0;
    float totalWindows = (float)(win.nX * win.nY);
    int increment5 = 0.05 * totalWindows; // how many iterations does 5% represent ? 
    int countdown = increment5;   // decrement countdown instead of modulo
    int percent5 = 0;  // number of elements in the progress bar (1 means 5%)
    
    while (win.next() == 0)
    {
        // Read the data
    
        // Nicely formatted progress indicator
        i++;
        if (--countdown == 0)
        {
                percent5++;
                std::cout << "\r" << std::string(percent5, '|') << percent5*5 << "%";
                countdown = increment5;  
                std::cout.flush();
        }
    
    }
    

    如果您担心累积舍入对于显示进度来说是不可接受的,您总是可以选择在if-block 中计算精确值:计算将仅每 5% 执行一次,而不是在每次迭代时执行.

    【讨论】:

    • totalWindows 保持不变,是的。聪明的解决方案,我喜欢使用倒计时
    • @Christophe - 我认为您的代码中有一个小问题。 cout 使用变量 percent 的字符串填充构造函数,这不是代码的一部分。也许你想要percent5*5。
    • @StillLearning 准确!谢谢,我已经编辑了答案:-)。顺便说一句,为了避免临时字符串的alloc/dealloc,可以考虑在循环外创建一个字符串变量,为它预留足够的容量,每次交互添加一个'|'在字符串的末尾。但这种改进是次要的,因为这段代码只执行了 20 次。
    【解决方案2】:

    怎么样:

    int step = 5;
    int nextPrint = step;
    
    while (win.next() == 0)
    {
        // Read the data
    
        // Nicely formatted progress indicator
        i++;
        percent = (100 * i) / totalWindows;
        if (percent >= nextPrint)
        {
            std::cout << "\r" << std::string(percent/5, '|') << percent << "%";
            std::cout.flush();
            nextPrint += step;
        }
    }
    

    顺便说一句: 为什么将 totalWindows 作为浮点数?这也会损害性能。如果迭代次数为 500000,则 32 位 int 就足够了。

    另一种更好的方法,因为不是在每个循环中都计算百分比:

    const int percentPrint = 5;
    int step = totalWindows / (100/percentPrint);
    int nextPrint = step;
    
    while (win.next() == 0)
    {
        // Read the data
    
        // Nicely formatted progress indicator
        i++;
        if (i >= nextPrint)
        {
            percent = (100 * i) / totalWindows;
            std::cout << "\r" << std::string(percent/percentPrint , '|') << percent << "%";
            std::cout.flush();
            nextPrint += step;
        }
    }
    std::cout << "\r" << std::string(100/percentPrint , '|') << percent << "%";
    std::cout.flush();
    

    【讨论】:

    • 是的,这很好,比我的方法好得多,同时保持清晰和简单
    【解决方案3】:

    不用每次都计算百分比,你可以计算总数的5%是多少,然后直接使用。

    考虑这个简化的例子:

    int main(int argc, char * argv) {
      int total = 50000;
      int total_percent_stop = total * 5 / 100; //Set the percentage here.
    
      for (int i = 0; i < total; i++) {
        if (i % total_percent_stop == 0) {
          printf("%d percent done\n", (i / total_percent_stop) * 5);
        }
      }
    }
    

    如果此特定代码的性能对您非常重要,您可以通过执行此类操作来避免相对昂贵的除法操作(以牺牲一些可读性为代价)。

    int main(int argc, char * argv) {
      int total = 50000;
      int total_percent_stop = total * 5 / 100; //Set the percentage here
    
      for (int i = 0, percent_counter = 0, n_percent = 0;
           i < total;
           i++, percent_counter++) {
        if (percent_counter == total_percent_stop) {
          percent_counter = 0;
          n_percent++;
          printf("%d percent done\n", n_percent * 5);
        }
      }
    }
    

    在我的机器上,对于足够大的 total 值,第二个明显更快。当我将所有内容更改为 unsigned long longs 并将总数设置为 50 亿时,第二个用时 9.336 秒,第一个用时 40.159 秒

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      相关资源
      最近更新 更多