Skip to content

Instantly share code, notes, and snippets.

@pfreixes
Created December 12, 2019 12:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pfreixes/e5417deba3d4235a39b93db5e0e83f50 to your computer and use it in GitHub Desktop.
Save pfreixes/e5417deba3d4235a39b93db5e0e83f50 to your computer and use it in GitHub Desktop.
Compare performance conditionals between if None and if []
import time
N = 1000000
def benchmark():
l = None
start = time.time()
for i in range(N):
if l:
pass
elapsed = time.time() - start
print("Time op if None: ~ {} nanosecond".format((elapsed/N)*1e9))
l = []
start = time.time()
for i in range(N):
if l:
pass
elapsed = time.time() - start
print("Time op if []: ~ {} nanosecond".format((elapsed/N)*1e9))
benchmark()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment