title: python speed calculation optimisation
body: <p>I have a script with two lists. One list has measured distances and the other has spend time. I then calculate max speed ether at  certain distances or for certain time window. For example I calculate max speed in two seconds period, 10s period.... and max speed at 100 meters, 1 km....</p>
  <p>So i first do for loop for each measurement and in that loop for x i find end point which is y meters or y seconds away from point x and then calculate traveled distance and spend time and then calculate speed.</p>
  <p>This works fine but when there is a lot of data in lists it takes quite some time for calculations to complete. When i try the same in some closed sourced applications calculations are done in matter of seconds while my script sometimes takes minutes to  calculate. Any idea how i could speed up things ?</p>
  <pre><code>distance=[1,2,6,8,13,15,18,24,27,34,37,38,44,47,48,49]
  time=[1,2,3,4,5,7,8,9,10,11,12,13,15,16,17,18]
  
  
  def calculate(arr,unit,type):
      pointer=-1
      max=0
      for xy in arr:
          pointer+=1
          try:        
              if type=='distance':
                   nxt=next(x[0] for x in enumerate(distance) if x[1] &gt; (distance[pointer]+unit))
              else:
                   nxt=next(x[0] for x in enumerate(time) if x[1] &gt; (time[pointer]+unit))
  
              vald=distance[nxt]-distance[pointer]
              valc=time[nxt]-time[pointer]
              value=round((vald/valc)*1.94384449,2)
  
              if value &gt; max and value &lt; 45:
                   max=value
          except:
              pass
  
          return(max)
  
  print(calculate(distance,2,&quot;time&quot;))
  print(calculate(distance,10,&quot;time&quot;))
  print(calculate(distance,10,&quot;distance&quot;)) 
  </code></pre>