kvesteri / intervals

Python tools for handling intervals (ranges of comparable objects).
BSD 3-Clause "New" or "Revised" License
107 stars 17 forks source link

checking 'in' interval performance #32

Open rotten opened 8 years ago

rotten commented 8 years ago

I don't know if this is an issue, or if it is expected, but I was surprised at the performance difference between these two loops (2 orders of magnitude), so I thought I'd share:

import time
from intervals import IntInterval

myInterval = IntInterval('[2000,2500]')

start = time.time()
for x in range(0,1000):
   if 2222 in myInterval:  pass
   if x in myInterval: pass
end = time.time()
print end - start

start = time.time()
for x in range(0,1000):
   if 2222 >= myInterval.lower and 2222 <= myInterval.upper:  pass
   if x >= myInterval.lower and t <= myInterval.upper: pass
end = time.time()
print end - start
$  ./test_intervals_timing.py
0.0624339580536
0.000767946243286
kvesteri commented 8 years ago

Thanks for sharing this. I think most of the overhead comes from decorators and the fact that they coerce compared values to interval objects.

edwardzhang0630 commented 5 years ago

I have meet the same problem. When i use 'in' operation in a loop, it takes more than 20 mintes to finish the loop, I hope you can impove this performance problem.

Thanks for sharing this. I think most of the overhead comes from decorators and the fact that they coerce compared values to interval objects.

I have met the same problem. When i use 'in' operation in a loop, it takes more than 20 mintes to finish the loop, I hope you can impove this performance problem to makes it better to use.