Skip to content

Instantly share code, notes, and snippets.

@pfmoore
Created March 6, 2024 16:56
Show Gist options
  • Save pfmoore/a5406e0901609ed08cab8a0719d4e866 to your computer and use it in GitHub Desktop.
Save pfmoore/a5406e0901609ed08cab8a0719d4e866 to your computer and use it in GitHub Desktop.
from tempfile import TemporaryDirectory
import os
import time
import datetime
import venv
import subprocess
import sys
def mkvenvs(venv_type="dummy", N=100):
def do_nothing(target):
pass
def with_venv(target):
builder = venv.EnvBuilder()
builder.create(target)
def with_venv_sp(target):
subprocess.run([sys.executable, "-m", "venv", "--without-pip", target])
def with_uv(target):
subprocess.run(["uv", "venv", "--quiet", target])
if venv_type == "dummy":
mk_venv = do_nothing
elif venv_type == "venv":
mk_venv = with_venv
elif venv_type == "venv_sp":
mk_venv = with_venv_sp
elif venv_type == "uv":
mk_venv = with_uv
with TemporaryDirectory() as tmp:
total_duration = datetime.timedelta(0)
for i in range(N):
target = os.path.join(tmp, f"venv_{i}")
start = datetime.datetime.now()
mk_venv(target)
end = datetime.datetime.now()
total_duration += end - start
print(f"Average creation time ({venv_type}): {total_duration / N}")
mkvenvs("dummy")
mkvenvs("venv")
mkvenvs("venv_sp")
mkvenvs("uv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment