Skip to content

Instantly share code, notes, and snippets.

@nschloe
Created November 13, 2021 12:05
Show Gist options
  • Save nschloe/d790a873081dc504193c99d3758755b4 to your computer and use it in GitHub Desktop.
Save nschloe/d790a873081dc504193c99d3758755b4 to your computer and use it in GitHub Desktop.
Python string lookup vs list lookup speed
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()
@nschloe
Copy link
Author

nschloe commented Nov 13, 2021

out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment