Skip to content

Instantly share code, notes, and snippets.

@eddjberry
Last active January 17, 2022 16:48
Show Gist options
  • Save eddjberry/a6c8c957609e5eb344b5252c10f79cef to your computer and use it in GitHub Desktop.
Save eddjberry/a6c8c957609e5eb344b5252c10f79cef to your computer and use it in GitHub Desktop.
import timeit
import numpy as np
from faker import Faker
# create the faker object
fake = Faker()
# np.random_choice function
def np_choice(N=1000):
np.random.choice(N+1, N, replace = False)
# fake.random_sample function
def fake_random_sample(N=1000):
fake.random_sample(range(1, N+1))
# timings
reps = 10000
np_avg_time = timeit.timeit(np_choice, number=reps) / reps
fake_avg_time = timeit.timeit(fake_random_sample, number=reps) / reps
print(f"""
np.random.choice(): {(np_avg_time * 1e06):.0f}µs
fake.random_sample(): {(fake_avg_time * 1e06):.0f}µs
numpy is {fake_avg_time / np_avg_time:.0f}x faster
""")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment