从 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,爱上了编程! 接下来,当然是又一些有趣的 ...
随机推荐
- c++ 判断数组元素是否有负数(any_of)
#include <iostream> // std::cout #include <algorithm> // std::any_of #include <array& ...
- Hadoop 部分截图
- Jmeter 测试API接口 查看接口的幂等问题
背景介绍: 比如一个注册接口,要求填入的手机号与DB中已有的不能重复, 如果手机号码重复,则此次注册失败,不会新增会员数据: 如果不重复,则注册成功(忽略其他因素). 但是用20个并发,同样的请求,请 ...
- django模型和字段
一个模型(model)就是一个单独的.确定的数据的信息源,包含了数据的字段和操作方法.通常,每个模型映射为一张数据库中的表. 基本的原则如下: 每个模型在Django中的存在形式为一个Python类 ...
- C++中int、string等常见类型转换
1.int型与string型的互相转换 最佳实践: int型转string型 void int2str(const int &int_temp,string &string_temp) ...
- U3D协程yield的使用和理解
部分内容参考网址:http://blog.csdn.net/huang9012/article/details/29595747 Win7+U3D 4.6.7 1.在c#中使用①首选需要定义一个返回值 ...
- Shell需注意的语法问题
1.文件头声明别漏掉#和! #!/bin/bash 2.赋值语句①=号两端不能有空格(判断语句=号两端必须有空格)②使用变量必须使用符号$ var1=valecho $var1 3.if语句写错下面任 ...
- secs/gem协议
介绍SECS/GEM SEMI SECS/GEM标准概述 SECS/GEM是由国际半导体设备与材料协会(SEMI)制定的连接性标准.此连接性标准用于在设备和工厂的资讯和控制系统间建立通讯. SECS是 ...
- 雷林鹏分享:C# 索引器(Indexer)
C# 索引器(Indexer) 索引器(Indexer) 允许一个对象可以像数组一样被索引.当您为类定义一个索引器时,该类的行为就会像一个 虚拟数组(virtual array) 一样.您可以使用数组 ...
- C++中的初始化参数列表
c++中以下几种情况的变量的初始化不可以写在构造函数里,而是要写在初始化参数列表中 1.const常量 class AA { public : const int num; public : AA() ...