Correct import method when using timeit?

I tested the following code from one of my previous questions (turning a list into a dictionary) :

single = ['key1', 'value1', 'key2', 'value2', 'key3', 'value3']

if __name__ == '__main__':
    from timeit import Timer
    print Timer("dict(zip(single[::2], single[1::2]))",
        "from __main__ import single").timeit()
    print Timer("si = iter(single); dict(izip(si, si))",
        "from __main__ import single; from itertools import izip").timeit()

And I'm not sure what is best used timeitto import izipinto Timeror install (I assume the installation, but the end result of the synchronization differs depending on what I do).

Anyway, I was just hoping for any further ideas from you guys when you define your code, etc. (Also, I'm just trying to learn - I don't suffer from premature optimization or anything else.)

Thank.

+5
source share
1 answer

. , , dict - . .

+5

All Articles