前文 使用 uv 管理 Python 项目 介绍了 uv 的主要使用过程. 相较于传统的 pip
不仅是功能更丰富, 速度也是嘎嘎快. 本文就来做一个实际速度测试对比. 以下测试均使用清华大学 pypi 镜像, 在 Docker 环境中分别启动新的容器进行无本地缓存的冷安装. 使用 Docker 镜像 public.ecr.aws/amazonlinux/amazonlinux:2023
, 内置 Python 版本 3.9.20, 记录创建虚拟环境以及安装 pandas, boto3, flask 的用时.
pip 环境
docker run -it --rm public.ecr.aws/amazonlinux/amazonlinux:2023# 环境准备
dnf install python3-pip python3-virtualenv -y
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simpletime virtualenv .venv
real 0m0.314s
user 0m0.158s
sys 0m0.210ssource .venv/bin/activate
time pip install pandas
real 0m6.347s
user 0m4.838s
sys 0m0.973stime pip install boto3
real 0m3.080s
user 0m2.205s
sys 0m0.547stime pip install flask
real 0m1.944s
user 0m0.892s
sys 0m0.117s
uv 环境
docker run -it --rm public.ecr.aws/amazonlinux/amazonlinux:2023# 环境准备
dnf install python3-pip -y
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
pip install uvmkdir ~/test && cd ~/testtime uv venv
real 0m0.050s
user 0m0.030s
sys 0m0.012suv initcat <<EOF >> pyproject.toml[pip]
index-url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"[tool.uv]
index-url = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple"
EOFtime uv pip install pandas
real 0m0.903s
user 0m0.237s
sys 0m0.839stime uv pip install boto3
real 0m1.060s
user 0m0.154s
sys 0m0.751stime uv pip install flask
real 0m0.276s
user 0m0.047s
sys 0m0.117s
结果对照表格, real 时长 (ms):
操作 | pip | uv | 差距 |
---|---|---|---|
创建虚拟环境 | 314 | 50 | 6.3x |
安装 pandas | 6347 | 930 | 6.8x |
安装 boto3 | 3080 | 1060 | 2.9x |
安装 flask | 1944 | 276 | 7.0x |
在相同网络环境中, uv 确实比 pip 的速度要明显快出好几倍. 好评!