performance - Is it faster to add a key to a dictionary or append a value to a list in Python? -
i have application need build list or dictionary , speed important. declare list of zeros of appropriate length , assign values 1 @ time need able check length , have still meaningful.
would faster add key value pair dictionary or append value list? length of lists , dictionary small (less 100) isn't true , in worst case larger.
i have variable keep track of in list if both of these operations slow.
best way use time()
check execution time.
in following example dict faster.
from time import time st_time = time() b = dict() in range(1, 10000000): b[i] = print (time() - st_time) st_time = time() = [] in range(1, 10000000): a.append(i) print (time() - st_time) 1.45600008965 1.52499985695
Comments
Post a Comment