GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • All gists
  • Back to GitHub
  • Sign in
  • Sign up
  • Code
  • Revisions 1
  • Terms
  • Privacy
  • Security
  • Status
  • Community
  • Docs
  • Contact

This view

  • Original page
  • New address

GeezerVison is showing gist.github.com — bench.py

Instantly share code, notes, and snippets.

@colesbury

colesbury/bench.py Secret

Created December 22, 2025 16:11
Show Gist options
  • Download ZIP
  • Star 0 (0) You must be signed in to star a gist
  • Fork 0 (0) You must be signed in to fork a gist
  • Clone this repository at <script src="https://gist.github.com/colesbury/b675c95d35f29ca9d5723b95396d48a8.js"></script>
  • Save colesbury/b675c95d35f29ca9d5723b95396d48a8 to your computer and use it in GitHub Desktop.
Clone this repository at <script src="https://gist.github.com/colesbury/b675c95d35f29ca9d5723b95396d48a8.js"></script>
Save colesbury/b675c95d35f29ca9d5723b95396d48a8 to your computer and use it in GitHub Desktop.
Download ZIP
Raw
bench.py
import timeit
def bench(n=100_000, reps=1_000):
setups = {
"begin": "lst = list(range(n))",
"middle": "lst = list(range(n))",
"end": "lst = list(range(n))",
}
stmts = {
"begin": "lst.insert(0, -1)",
"middle": "lst.insert(n//2, -1)",
"end": "lst.insert(n, -1)", # same as append for a valid index at the end
}
for name in ("begin", "middle", "end"):
t = timeit.timeit(
stmt=stmts[name],
setup=setups[name],
number=reps,
globals={"n": n},
)
print(f"{name:6} n={n:<7} reps={reps:<7} total={t:.6f}s avg={t/reps*1e6:.2f}µs/op")
if __name__ == "__main__":
bench()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You are visitor 01402622 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.