Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python dictionary creation time. Different techniques
#!/usr/bin/env python3
# coding=utf-8
import timer
import itertools
def naive_dict_create(keys, values):
return {k: v for k, v in
zip(keys, values + [None] * (len(keys) - len(values)))}
def for_loop_dict_create(keys, values):
d = {}
for i, k in enumerate(keys):
try:
d[k] = values[i]
except IndexError:
d[k] = None
return d
def itertools_dict_create(keys, values):
return {k: v for k, v in itertools.zip_longest(keys, values)}
def zip_dict_create(keys, values):
return dict(zip(keys, values + [None] * (len(keys) - len(values))))
def from_keys_dict_create(keys, values):
d = dict.fromkeys(keys, None)
d.update(zip(keys, values))
return d
def main():
TEST_Q = 100
KEYS_SIZE = 300000
VALUES_SIZE = 500000
print("Results for case len(keys) > len(values):")
for f in (naive_dict_create, itertools_dict_create, zip_dict_create,
from_keys_dict_create, for_loop_dict_create):
print("{:<25s}: {:.5f} sec".format(f.__name__, timer.bestof(TEST_Q,
f, list(range(KEYS_SIZE)),
list(range(VALUES_SIZE)))[0]))
print("Results for case len(keys) < len(values):")
for f in (naive_dict_create, itertools_dict_create, zip_dict_create,
from_keys_dict_create, for_loop_dict_create):
print("{:<25s}: {:.5f} sec".format(f.__name__, timer.bestof(TEST_Q,
f, list(range(VALUES_SIZE)),
list(range(KEYS_SIZE)))[0]))
print("Total time for case len(keys) < len(values):")
for f in (naive_dict_create, itertools_dict_create, zip_dict_create,
from_keys_dict_create, for_loop_dict_create):
print("{:<25s}: {:.5f} sec".format(f.__name__, timer.total(TEST_Q,
f, list(range(KEYS_SIZE)),
list(range(VALUES_SIZE)))[0]))
if __name__ == "__main__":
main()
# File timer.py
"""
Homegrown timing tools for function calls.
Does total time, best-of time, and best-of-totals time
"""
import time
import sys
timer = time.clock if sys.platform[:3] == 'win' else time.time
def total(reps, func, *pargs, **kargs):
"""
Total time to run func() reps times.
Returns (total time, last result)
"""
# Hoist out, equalize 2.x, 3.x
repslist = list(range(reps))
# Or perf_counter/other in 3.3+
start = timer()
for i in repslist:
ret = func(*pargs, **kargs)
elapsed = timer() - start
return (elapsed, ret)
def bestof(reps, func, *pargs, **kargs):
"""
Quickest func() among reps runs.
Returns (best time, last result)
"""
best = 2 ** 32 # 136 years seems large enough
for i in range(reps): # range usage not timed here
start = timer()
ret = func(*pargs, **kargs)
elapsed = timer() - start # Or call total() with reps=1
if elapsed < best:
best = elapsed # Or add to list and take min()
return (best, ret)
def bestoftotal(reps1, reps2, func, *pargs, **kargs):
"""
Best of totals:
(best of reps1 runs of (total of reps2 runs of func))
"""
return bestof(reps1, total, reps2, func, *pargs, **kargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.