Dear tutors<div><br></div><div>My function below simply populates a large dict. When measured by timeit populating 10 million items once, versus populating 1 million items ten times, the times are noticeably different:</div>


<div><br></div><div><div>---</div><div>import timeit</div><div><br></div><div>N = 10000000 # This constant's value is either 10 million or 1 million</div><div>testDict = {}</div><div>def writeDict(N):</div><div>    for i in xrange(N):</div>


<div>        testDict[i] = [i, [i + 1, i + 2], i + 3]</div><div>print timeit.Timer('f(N)', 'from __main__ import N, writeDict as f').timeit(1) # the 'number' parameter is either 1 or 10</div></div>


<div><br></div><div>---</div><div>Result from 3 runs of 10 million x 1 time: 12.7655465891, 13.1248426525, 12.1611512459</div><div><div><br></div><div><div>Result from 3 runs of 1 million x 10 times: 14.3727692498, 14.3825673988, 14.4390314636</div>

</div><div><br></div></div><div>I ran Python 2.7 on Pycharm on Windows 7.</div><div><br></div><div>My guess is that this discrepancy is a result of either how some sort of overhead in timeit, or of Python having to allocate memory space for a dict 10 times. What do you think, and how to find out for sure?</div>


<div><br></div><div>Second (for me, this question is more important), how to improve performance? (I tried a tuple rather than a list for the dict values, it was slightly faster, but I need dict items to be mutable)</div>


<div><br></div><div>Thanks</div><div><br></div><div>Trung Doan</div>