Skip to content

Instantly share code, notes, and snippets.

@mocquin
Last active January 25, 2019 22:28
Show Gist options
  • Save mocquin/8f61f628118ae60341baa11a8246d970 to your computer and use it in GitHub Desktop.
Save mocquin/8f61f628118ae60341baa11a8246d970 to your computer and use it in GitHub Desktop.
Element-wise data structure computation time
# Please note the results for larger list can be different
import numpy as np
# making a list, a dict, and an array
list_a = list(np.asarray(np.linspace(0,9,10)).astype(int)) # working with int
array_a = np.array(list_a)
dict_a = {x: x for x in list_a}
from operator import add as opadd
%timeit -r5 list_c = list(map(opadd, list_a, list_a))
%timeit -r5 list_cc = [x+y for x,y in zip(list_a,list_a)]
%timeit -r5 array_c = array_a + array_a
%timeit -r5 dict_c = {dict_a[key] + dict_a[key] for key in dict_a.keys()}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment