Created
December 12, 2019 12:01
-
-
Save pfreixes/e5417deba3d4235a39b93db5e0e83f50 to your computer and use it in GitHub Desktop.
Compare performance conditionals between if None and if []
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
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