• window下tqdm进度条


    原代码是linux下运行,修改后可在window下运行。

    1. #ifndef TQDM_H
    2. #define TQDM_H
    3. #include <chrono>
    4. #include <ctime>
    5. #include <numeric>
    6. #include <ios>
    7. #include <string>
    8. #include <cstdlib>
    9. #include <iostream>
    10. #include <vector>
    11. #include <math.h>
    12. #include <algorithm>
    13. #include <io.h>
    14. class tqdm {
    15. private:
    16. // time, iteration counters and deques for rate calculations
    17. std::chrono::time_point<std::chrono::system_clock> t_first = std::chrono::system_clock::now();
    18. std::chrono::time_point<std::chrono::system_clock> t_old = std::chrono::system_clock::now();
    19. int n_old = 0;
    20. std::vector<double> deq_t;
    21. std::vector<int> deq_n;
    22. int nupdates = 0;
    23. int total_ = 0;
    24. int period = 1;
    25. unsigned int smoothing = 50;
    26. bool use_ema = true;
    27. float alpha_ema = 0.1;
    28. std::vector<const char*> bars = { " ", "▏", "▎", "▍", "▌", "▋", "▊", "▉", "█" };
    29. bool in_screen = (system("test $STY") == 0);
    30. bool in_tmux = (system("test $TMUX") == 0);
    31. bool is_tty = _isatty(1);
    32. bool use_colors = true;
    33. bool color_transition = true;
    34. int width = 40;
    35. std::string right_pad = "▏";
    36. std::string label = "";
    37. void hsv_to_rgb(float h, float s, float v, int& r, int& g, int& b) {
    38. if (s < 1e-6) {
    39. v *= 255.;
    40. r = v; g = v; b = v;
    41. }
    42. int i = (int)(h*6.0);
    43. float f = (h*6.) - i;
    44. int p = (int)(255.0*(v*(1. - s)));
    45. int q = (int)(255.0*(v*(1. - s * f)));
    46. int t = (int)(255.0*(v*(1. - s * (1. - f))));
    47. v *= 255;
    48. i %= 6;
    49. int vi = (int)v;
    50. if (i == 0) { r = vi; g = t; b = p; }
    51. else if (i == 1) { r = q; g = vi; b = p; }
    52. else if (i == 2) { r = p; g = vi; b = t; }
    53. else if (i == 3) { r = p; g = q; b = vi; }
    54. else if (i == 4) { r = t; g = p; b = vi; }
    55. else if (i == 5) { r = vi; g = p; b = q; }
    56. }
    57. public:
    58. tqdm() {
    59. if (in_screen) {
    60. set_theme_basic();
    61. color_transition = false;
    62. }
    63. else if (in_tmux) {
    64. color_transition = false;
    65. }
    66. }
    67. void reset() {
    68. t_first = std::chrono::system_clock::now();
    69. t_old = std::chrono::system_clock::now();
    70. n_old = 0;
    71. deq_t.clear();
    72. deq_n.clear();
    73. period = 1;
    74. nupdates = 0;
    75. total_ = 0;
    76. label = "";
    77. }
    78. void set_theme_line() { bars = { "─", "─", "─", "╾", "╾", "╾", "╾", "━", "═" }; }
    79. void set_theme_circle() { bars = { " ", "◓", "◑", "◒", "◐", "◓", "◑", "◒", "#" }; }
    80. void set_theme_braille() { bars = { " ", "⡀", "⡄", "⡆", "⡇", "⡏", "⡟", "⡿", "⣿" }; }
    81. void set_theme_braille_spin() { bars = { " ", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠇", "⠿" }; }
    82. void set_theme_vertical() { bars = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█", "█" }; }
    83. void set_theme_basic() {
    84. bars = { " ", " ", " ", " ", " ", " ", " ", " ", "#" };
    85. right_pad = "|";
    86. }
    87. void set_label(std::string label_) { label = label_; }
    88. void disable_colors() {
    89. color_transition = false;
    90. use_colors = false;
    91. }
    92. void finish() {
    93. progress(total_, total_);
    94. printf("\n");
    95. fflush(stdout);
    96. }
    97. void progress(int curr, int tot) {
    98. if (is_tty && (curr%period == 0)) {
    99. total_ = tot;
    100. nupdates++;
    101. auto now = std::chrono::system_clock::now();
    102. double dt = ((std::chrono::duration<double>)(now - t_old)).count();
    103. double dt_tot = ((std::chrono::duration<double>)(now - t_first)).count();
    104. int dn = curr - n_old;
    105. n_old = curr;
    106. t_old = now;
    107. if (deq_n.size() >= smoothing) deq_n.erase(deq_n.begin());
    108. if (deq_t.size() >= smoothing) deq_t.erase(deq_t.begin());
    109. deq_t.push_back(dt);
    110. deq_n.push_back(dn);
    111. double avgrate = 0.;
    112. if (use_ema) {
    113. avgrate = deq_n[0] / deq_t[0];
    114. for (unsigned int i = 1; i < deq_t.size(); i++) {
    115. double r = 1.0*deq_n[i] / deq_t[i];
    116. avgrate = alpha_ema * r + (1.0 - alpha_ema)*avgrate;
    117. }
    118. }
    119. else {
    120. double dtsum = std::accumulate(deq_t.begin(), deq_t.end(), 0.);
    121. int dnsum = std::accumulate(deq_n.begin(), deq_n.end(), 0.);
    122. avgrate = dnsum / dtsum;
    123. }
    124. // learn an appropriate period length to avoid spamming stdout
    125. // and slowing down the loop, shoot for ~25Hz and smooth over 3 seconds
    126. if (nupdates > 10) {
    127. period = (int)(std::min(std::max((1.0 / 25)*curr / dt_tot, 1.0), 5e5));
    128. smoothing = 25 * 3;
    129. }
    130. double peta = (tot - curr) / avgrate;
    131. double pct = (double)curr / (tot*0.01);
    132. if ((tot - curr) <= period) {
    133. pct = 100.0;
    134. avgrate = tot / dt_tot;
    135. curr = tot;
    136. peta = 0;
    137. }
    138. double fills = ((double)curr / tot * width);
    139. int ifills = (int)fills;
    140. printf("\015 ");
    141. if (use_colors) {
    142. if (color_transition) {
    143. // red (hue=0) to green (hue=1/3)
    144. int r = 255, g = 255, b = 255;
    145. hsv_to_rgb(0.0 + 0.01*pct / 3, 0.65, 1.0, r, g, b);
    146. printf("\033[38;2;%d;%d;%dm ", r, g, b);
    147. }
    148. else {
    149. printf("\033[32m ");
    150. }
    151. }
    152. for (int i = 0; i < ifills; i++) std::cout << bars[8];
    153. if (!in_screen && (curr != tot)) printf("%s", bars[(int)(8.0*(fills - ifills))]);
    154. for (int i = 0; i < width - ifills - 1; i++) std::cout << bars[0];
    155. printf("%s ", right_pad.c_str());
    156. if (use_colors) printf("\033[1m\033[31m");
    157. printf("%4.1f%% ", pct);
    158. if (use_colors) printf("\033[34m");
    159. std::string unit = "Hz";
    160. double div = 1.;
    161. if (avgrate > 1e6) {
    162. unit = "MHz"; div = 1.0e6;
    163. }
    164. else if (avgrate > 1e3) {
    165. unit = "kHz"; div = 1.0e3;
    166. }
    167. printf("[%4d/%4d | %3.1f %s | %.0fs<%.0fs] ", curr, tot, avgrate / div, unit.c_str(), dt_tot, peta);
    168. printf("%s ", label.c_str());
    169. if (use_colors) printf("\033[0m\033[32m\033[0m\015 ");
    170. if ((tot - curr) > period) fflush(stdout);
    171. }
    172. }
    173. };
    174. #endif

    主函数: 

    1. #include "MyClass.h"
    2. #include <windows.h>
    3. int main() {
    4. int N = 2000;
    5. tqdm bar;
    6. std::cout << "Overhead of loop only:" << std::endl;
    7. for (int i = 0; i < 100000000; i++) {
    8. bar.progress(i, 100000000);
    9. }
    10. bar.finish();
    11. std::cout << "Basic:" << std::endl;
    12. bar.reset();
    13. bar.set_theme_basic();
    14. for (int i = 0; i < N; i++) {
    15. bar.progress(i, N);
    16. Sleep(1000);
    17. }
    18. bar.finish();
    19. std::cout << "Braille:" << std::endl;
    20. bar.reset();
    21. bar.set_theme_braille();
    22. for (int i = 0; i < N; i++) {
    23. bar.progress(i, N);
    24. Sleep(300);
    25. }
    26. bar.finish();
    27. std::cout << "Line:" << std::endl;
    28. bar.reset();
    29. bar.set_theme_line();
    30. for (int i = 0; i < N; i++) {
    31. bar.progress(i, N);
    32. Sleep(300);
    33. }
    34. bar.finish();
    35. std::cout << "Circles:" << std::endl;
    36. bar.reset();
    37. bar.set_theme_circle();
    38. for (int i = 0; i < N; i++) {
    39. bar.progress(i, N);
    40. Sleep(300);
    41. }
    42. bar.finish();
    43. bar.reset();
    44. std::cout << "Vertical bars:" << std::endl;
    45. bar.reset();
    46. bar.set_theme_vertical();
    47. for (int i = 0; i < N; i++) {
    48. bar.progress(i, N);
    49. Sleep(3000);
    50. }
    51. bar.finish();
    52. return 0;
    53. }

    源码 

  • 相关阅读:
    Himall验证帮助类判断当前时间是否在指定的时间段内、是否是数值(包括整数和小数)
    c实现mp4解封装
    lua 判空的坑
    ELK 与 EFK的介绍和对比
    美国IP代理如何获取?适用于哪些场景?
    MySQL备份与恢复
    [激光原理与应用-20]:《激光原理与技术》-6- 谐振腔的结构、作用、工作原理
    Java基于SSM框架的教室预约申请管理系统 毕业设计
    HTML5:七天学会基础动画网页6
    自己动手从零写桌面操作系统GrapeOS系列教程——11.MBR介绍
  • 原文地址:https://blog.csdn.net/qq_35054151/article/details/140377766