Created
June 7, 2023 01:54
-
-
Save elliotwutingfeng/0387782a0b601e1fe66fb9a828c3dc29 to your computer and use it in GitHub Desktop.
Fastest way to pick either 1 or 3 at random in IPython (IronPython). Inspired by Christopher's (twitter handle: @shrydar) suggestion featured in https://youtube.com/watch?v=VC-lbd8mTOs&t=1702s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import choice, random, randrange | |
# Fastest way to pick either 1 or 3 at random in IPython (IronPython). | |
# Inspired by Christopher's (twitter handle: @shrydar) suggestion | |
# featured in https://youtube.com/watch?v=VC-lbd8mTOs&t=1702s | |
# Tested on Python 3.11.3, Linux x64, AMD Ryzen 7 5800X | |
t = (1, 3) | |
%timeit int(random() * 4) | 1 | |
# 63.3 ns ± 0.112 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each) | |
%timeit choice(t) | |
# 147 ns ± 0.377 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each) | |
%timeit randrange(1, 4, 2) | |
# 237 ns ± 0.276 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment