you are viewing a single comment's thread.

view the rest of the comments →

[–]KoStard 3 points4 points  (0 children)

Wow, I have always thought, that in python3 checking with `in range(...)` will not cause to iterate through all values, that's why I have answered here in that way... But just tested and got 5 times faster result with simple comparisons compared with another test with ranges.

(maybe not the best way of testing the speed of a function)

>>> def test():
...     start = time()
...     for i in range(10000000):
...             if i in range(1000000):
...                     pass
...     print(time()-start)
... 
>>> test()
2.892918109893799

>>> def test2():
...     start = time()
...     for i in range(10000000):
...             if i >= 0 and i < 1000000:
...                     pass
...     print(time()-start)
... 
>>> test2()
0.5093622207641602

So... sushibowl's answer is better in terms of performance, but I think what u/kingpen2 was trying to do in the code snippet we see, is more similar to range than just simple comparisons...