Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import numpy as np
- import time
- n_choices = 18
- def switch_fun(x):
- if x == 0:
- return 0
- elif x == 1:
- return 1
- elif x == 2:
- return 2
- elif x == 3:
- return 3
- elif x == 4:
- return 4
- elif x == 5:
- return 5
- elif x == 6:
- return 6
- elif x == 7:
- return 7
- elif x == 8:
- return 8
- elif x == 9:
- return 9
- elif x == 10:
- return 10
- elif x == 11:
- return 11
- elif x == 12:
- return 12
- elif x == 13:
- return 13
- elif x == 14:
- return 14
- elif x == 15:
- return 15
- elif x == 16:
- return 16
- elif x == 17:
- return 17
- _switch_dict = dict((ii, ii) for ii in range(n_choices))
- def dict_fun(x):
- return _switch_dict.get(x)
- n_repeat = 1000
- tags = np.arange(500) % n_choices
- times = np.zeros((2, n_repeat))
- for ii, fun in enumerate((switch_fun, dict_fun)):
- for jj in range(n_repeat):
- t0 = time.time()
- for tag in tags:
- fun(tag)
- times[ii, jj] = time.time() - t0
- times = np.median(times, axis=1)
- print('%0.1fx speedup for dict access' % (times[0] / times[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement