从 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,爱上了编程! 接下来,当然是又一些有趣的 ...
随机推荐
- Spring AOP之使用注解创建切面
上节中我们已经定义了Performance接口,他是切面中的切点的一个目标对象.那么现在就让我们使用AspectJ注解来定义切面吧. 1.定义切面 下面我们就来定义一场舞台剧中观众的切面类Audien ...
- EM算法及其推广
概述 EM算法是一种迭代算法,用于含有隐变量(hidden variable)的概率模型参数的极大似然估计,或极大后验概率估计. EM算法的每次迭代由两步组成:E步,求期望(expectation): ...
- Android JNI学习(四)——JNI的常用方法的中文API
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
- [转]TOMCAT启动提示NB: JAVA_HOME should point to a JDK not a JRE解决
来源:http://blog.csdn.net/caozhongyan/article/details/6602759 本人使用的Tomcat版本为apache-tomcat-6.0.18(用的是解压 ...
- django-celery定时任务以及异步任务and服务器部署并且运行全部过程
Celery 应用Celery之前,我想大家都已经了解了,什么是Celery,Celery可以做什么,等等一些关于Celery的问题,在这里我就不一一解释了. 应用之前,要确保环境中添加了Celery ...
- CentOS7 Install Docker(转)
https://linux.cn/article-4340-1.html CentOS 7 中 Docker 的安装 Docker 软件包已经包括在默认的 CentOS-Extras 软件源里.因此想 ...
- 利用nodeJs anywhere搭建本地服务器环境
1.npm install anywhere -g 如果是mac系统会提示你权限不够,需要在代码前加上 sudo获取管理员权限.即sudo npm install anywhere -g. 2.安装完 ...
- OAF中下载附件之后页面失效,报过时的数据异常,浏览器后退异常
我在使用了下载功能之后,再往页面添加行或进行保存,页面老是报浏览器后退的异常. 猜测是因为我的下载按钮使用的submitButton,它隐式的包含了一个submit动作,且我在代码中有一个Commit ...
- XML Publisher Using API’s(转)
原文地址:XML Publisher Using API’s Applications Layer APIsThe applications layer of XML Publisher allows ...
- Tomcat类加载器破坏双亲委派
转载:https://blog.csdn.net/qq_38182963/article/details/78660779 http://www.cnblogs.com/aspirant/p/8991 ...