fork download
  1. import timeit
  2.  
  3. dct = dict.fromkeys(range(100000))
  4.  
  5. def bench_in(k):
  6. for _ in range(10):
  7. a = k in dct
  8.  
  9. def bench_in_keys(k):
  10. for _ in range(10):
  11. a = k in dct.keys()
  12.  
  13. N = 50
  14. for func in (bench_in, bench_in_keys):
  15. for k in (1, 1000, 80000):
  16. print("%-30s %.3f" % (func.__name__,
  17. timeit.timeit(lambda : func(k), number=N)))
  18.  
  19.  
Success #stdin #stdout 3.34s 10912KB
stdin
Standard input is empty
stdout
bench_in                       0.000
bench_in                       0.000
bench_in                       0.000
bench_in_keys                  0.783
bench_in_keys                  0.799
bench_in_keys                  1.651