Re: [clickbait] Slowest Python program?

new topic     » goto parent     » topic index » view thread      » older message » newer message
euphoric said...

Why is Python so slow?! Yikes!

The python code is recursively taking smaller and smaller slices, which is a fair bit of copying, whereas the phix routine iteratively reduces hi/lo indexes, no copying.

If would probably be fairer to compare against a couple of entries from https://rosettacode.org/wiki/Binary_search#Python :

Closest match to the Phix code

def binary_search(l, value): 
    low = 0 
    high = len(l)-1 
    while low <= high:  
        mid = (low+high)//2 
        if l[mid] > value: high = mid-1 
        elif l[mid] < value: low = mid+1 
        else: return mid 
    return -1 

Using Python builtin methods:

from bisect import bisect_left 
  
def binary_search(a, x, lo=0, hi=None):   # can't use a to specify default for hi 
    hi = hi if hi is not None else len(a) # hi defaults to len(a)    
    pos = bisect_left(a,x,lo,hi)          # find insertion position 
    return (pos if pos != hi and a[pos] == x else -1) # don't walk off the end 

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu