从 Python 第三方进度条库 tqdm 谈起 (转载)
原文地址:
https://blog.ernest.me/post/python-progress-bar
tqdm
最近一款新的进度条 tqdm 库比较热门,声称比老版的 python-progressbar 库的单次响应时间提高了 10 倍以上。
Overhead is low -- about 60ns per iteration (80ns with gui=True). By comparison, the well established ProgressBar has an 800ns/iter overhead.

初读其源码,组织结构明显继承 python-progressbar,只是主代码行数从 357 提升到了 614。10 倍性能提升的奥妙在哪里呢?
在解答这个问题之前,我想先用这篇文章介绍下进度条的原理,然后,根据原理用几行代码实现一个简单的进度条。
progress bar 的原理
其实进度条的原理十分的简单,无非就是在 shell 中不断重写当前输出。
这时就不得不提到文本系统中的控制符。我们挑跟这次有关的看一下。
- \r = CR (Carriage Return) // moves the cursor to the beginning of the line without advancing to the next line(该控制符告诉输出端,将光标移到当前行的首位而不换行)
- \n = LF (Line Feed) // moves the cursor down to the next line without returning to the beginning of the line - *In a nix environment \n moves to the beginning of the line.(传统意义上的换行符,将光标移到下一行,但_并不移到首位_ )
- \r\n = CR + LF // a combi of \r and \n (换行并移动光标到行首)
这时,想要实现一个进度条,就十分简单,看下方代码。
Bash 实现
#/usr/bin/bash
for i in {..};
do
echo -ne "$i% \r"
sleep 0.01
done
echo -ne "\n"
但是,echo -n 存在明显的兼容性问题。
-n Do not print the trailing newline character. This may also be achieved by appending
\cto the end of the string, as is done by iBCS2 compatible systems. Note that this option as well as the effect of\care implementation-defined in IEEE Std 1003.1-2001 (POSIX.1) as amended by Cor. 1-2002. Applications aiming for maximum portability are strongly encouraged to useprintf(1)to suppress the newline character.Some shells may provide a builtin echo command which is similar or identical to this utility. Most notably, the builtin echo in sh(1) does not accept the -n option. Consult the builtin(1) manual page.
推荐使用 printf。
#/usr/bin/bash
for i in {..};
do
printf "%s%% \r" $i
sleep 0.01
done
printf "\n"
Python 实现
Python 主要使用系统库里的标准输出,sys.stdout 提供了便利的方法用于向 shell 打印输出。具体的方法介绍这里不赘述。
import sys
import time for i in range(100):
sys.stdout.write(' \r')
sys.stdout.flush()
sys.stdout.write('{}%\r'.format(i))
sys.stdout.flush()
time.sleep(0.01)
References
- tqdm repo
- progress bar repo
- NewLine Wikipedia
- Control Characters
- What's the difference between \r and \n
从 Python 第三方进度条库 tqdm 谈起 (转载)的更多相关文章
- Python - 进度条库 tqdm
前言 在写生成器的时候,网上看到一个进度条库,感觉蛮有意思,记录下 这个库感觉只有在调试的时候会用到,不做深入学习 内置库,不需要安装 示例代码 from tqdm import tqdm for i ...
- 一个简单、易用的Python命令行(terminal)进度条库
eprogress 是一个简单.易用的基于Python3的命令行(terminal)进度条库,可以自由选择使用单行显示.多行显示进度条或转圈加载方式,也可以混合使用. 示例 单行进度条 多行进度条 圆 ...
- Python进度条模块tqdm实现任务进度可视化
一.前言 tqdm 是一个易用性强.扩展性高的 Python 进度条库,可以在 Python 长循环中添加一个进度提示信息,我们只需要封装任意的迭代器 tqdm(iterator) 即可. 二.安装 ...
- python 中有趣的库tqdm
Tqdm 是 Python 进度条库,可以在 Python 长循环中添加一个进度提示信息用法:tqdm(iterator) # 方法1: import time from tqdm import tq ...
- Python实现进度条和时间预估的示例代码
一.前言 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做案例的人,却不知道如何去学习更加高深的知识.那么针对这三类人,我给大家 ...
- Python字符进度条
Python字符进度条 看看这个神奇的module from tqdm import trange from time import sleep for r in trange(10, 1, -1): ...
- Python之进度条及π的计算
Python之进度条及π的计算 文本进度条 1. 简单的开始 这是利用print()函数来实现简单的非刷新文本进度条.它的基本思想是按照任务执行百分比将整个任务划分为100个单位,每执行N%输出一次 ...
- Python实现进度条功能
Python实现进度条功能 import sys, time def progress(percent, width=50): # 设置进度条的宽度 if percent >= 100: # 当 ...
- 自主学习python文本进度条及π的计算
经过自己一段时间的学习,已经略有收获了!在整个过程的进行中,在我逐渐通过看书,看案例,做题积累了一些编程python的经验以后,我发现我渐渐爱上了python,爱上了编程! 接下来,当然是又一些有趣的 ...
随机推荐
- NIO 之 ByteBuffer
前言 对于刚接触ByteBuffer人来说,想要完全理解会稍微有点困难,正巧前几天有人问我,想到好久没写文章,就整理一下. 概念理解 对于ByteBuffer的一些概念不理解的情况下,如果直接打开源码 ...
- 常用 书签 mark
1.框架 jquery bootstrap 2.网站 w3cschool old 站长之家 ( site:www.zgacjx.com 直接看百度收录数 ) 博客园
- Java使用Log4记录日志
我们在系统使用中,为了方便查找问题,因此需要记录操作的日志,而目前比较成熟稳定的程序日志记录方式就是Log4,本人也是菜鸟,然后再学习研究中就记录一下使用方式,以方便今后查阅,同时本文章参考了博客园: ...
- UltraDropDown
private void FruitInit() { //Create some fruit fruits.Add(-1,"apple"); fruits.Add(-2," ...
- 清空mailq 队列里面的邮件
tmp_=`mailq | grep -E "root" | awk '{print $1}'` for i in $tmp_;do postsuper -d $i;done po ...
- 12月17日周日 form_for的部分理解。belongs_to的部分理解
1.lean guide:helper method query ,✅
- 使用python创建mxnet操作符(网络层)
对cuda了解不多,所以使用python创建新的操作层是个不错的选择,当然这个性能不如cuda编写的代码. 在MXNET源码的example/numpy-ops/下有官方提供的使用python编写新操 ...
- zzuli 1726 迷宫 BFS(题意)
1726: 迷宫 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 502 Solved: 80 SubmitStatusWeb Board Descri ...
- ioS UI-导航控制器(NavigationController)
#import "AppDelegate.h" #import "ViewController.h" @interface AppDelegate () @en ...
- POJ 2485 最小生成树
2333333333 又是水题.prim模板直接水过.求最小生成树里的最大的边的权值. 附代码:// 如果我木猜错的话.是要求最小生成树的最大边值. #include<stdio.h>#i ...