Created
November 13, 2021 12:05
-
-
Save nschloe/d790a873081dc504193c99d3758755b4 to your computer and use it in GitHub Desktop.
Python string lookup vs list lookup speed
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 random | |
import perfplot | |
lst = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"] | |
string = "abcdefghijkl" | |
def list_lookup(data): | |
return [lst[n] for n in data] | |
def string_lookup(data): | |
return [string[n] for n in data] | |
b = perfplot.bench( | |
setup=lambda n: [random.randint(0, 11) for _ in range(n)], | |
kernels=[list_lookup, string_lookup], | |
n_range=[2 ** k for k in range(20)], | |
equality_check=None, | |
) | |
b.save("out.png") | |
b.show() |
Author
nschloe
commented
Nov 13, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment