Skip to main content

Posts

Showing posts from February, 2015

Python using in vs equals or

Just a simple test to see which is faster... the winner using in!

%%timeit
x = 3
x in [1,2]

10000000 loops, best of 3: 53.6 ns per loop

%%timeit
x = 3
x==1 or x==2

10000000 loops, best of 3: 61 ns per loop

%%timeit
x = 3
x in [1,2]

10000000 loops, best of 3: 51.5 ns per loop

%%timeit
x = 3
x==1 or x==2

10000000 loops, best of 3: 56.2 ns per loop

%%timeit
x = 3
x==1 or x==2 or x==3

10000000 loops, best of 3: 76.2 ns per loop

%%timeit
x = 3
x in [1,2,3]

10000000 loops, best of 3: 52.2 ns per loop

%%timeit
x = 3
x==1 or x==2 or x==3 or x==4

10000000 loops, best of 3: 86 ns per loop

%%timeit
x = 3
x in [1,2,3,4]

10000000 loops, best of 3: 51.5 ns per loop


Clearly using the "x in list"format is faster.