Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import time
- from numpy.random import random
- from collections import OrderedDict, defaultdict
- def howlong(f):
- def tmp(*args, **kwargs):
- t = time.time()
- res = f(*args, **kwargs)
- need_time = time.time()-t
- tmp.__name__ = f.__name__
- #print "%s time: %f" % ((f.__doc__), need_time)
- #print f.__doc__
- print ".",
- return need_time
- return tmp
- def test(tests):
- results = {}
- for test in tests:
- results[test.__name__] = test()
- print "\nRESULTS"
- sorted_results = sorted(results.items(), key=lambda x:x[1] )
- best_name, best_result = sorted_results[0]
- for name, result in sorted_results:
- if name == best_name:
- description = "100%"
- else:
- description = "%s%%" % (int(100 + 100 * (result - best_result) / best_result))
- print "%70s - %20s - %10s" % (name, result, description)
- #print results
- lst = []
- dic = {}
- odic = OrderedDict()
- ddic = defaultdict(int)
- for i in range(1, 500000):
- val = random() + i
- lst.append(val)
- dic[i] = val
- odic[i] = val
- ddic[i] = val
- tupl = tuple(lst)
- NEED_INDEX = (1, 15, 190, 299, 410, 810, 1399, 2200, 2400, 2999, 3100, 3300, 3800, 3900, 4110, 4200, 4500, 4899, 4998, 450000)
- NUM_TESTS = 100000
- @howlong
- def test_list_access():
- result = []
- for i in range(NUM_TESTS):
- for j in NEED_INDEX:
- result.append(lst[j])
- return result
- @howlong
- def test_tuple_access():
- result = []
- for i in range(NUM_TESTS):
- for j in NEED_INDEX:
- result.append(tupl[j])
- return result
- @howlong
- def test_dic_access():
- result = []
- for i in range(NUM_TESTS):
- for j in NEED_INDEX:
- result.append(dic[j])
- return result
- @howlong
- def test_odic_access():
- result = []
- for i in range(NUM_TESTS):
- for j in NEED_INDEX:
- result.append(dic[j])
- return result
- @howlong
- def test_ddic_access():
- result = []
- for i in range(NUM_TESTS):
- for j in NEED_INDEX:
- result.append(dic[j])
- return result
- tests = [
- test_list_access,
- test_tuple_access,
- test_dic_access,
- test_odic_access,
- test_ddic_access,
- ]
- test(tests)
Advertisement
Add Comment
Please, Sign In to add comment