Standard Forth versus Python: a case study

Ian McConnell ian at
Thu Oct 12 04:32:03 EDT 2006

bearophileHUGS at writes:

> John Doty:
>> Yes. The efficient exact algorithms for this problem use *partial*
>> sorts. The Forth one from the FSL is of this class (although I know of
>> two better ones for big arrays). But it's tough to beat the efficiency
>> of the approximate histogram-based method the Python stats module
>> implements.
> The usual way to compute a true median with Python may be:
> def median(inlist):
>     newlist = sorted(inlist)
>     index = len(newlist) // 2
>     if len(newlist) % 2:
>         return newlist[index]
>     else:
>         return (newlist[index] + newlist[index-1]) / 2.0
> If you can use Psyco and your FITS lines are really long (well, maybe
> too much, the treshold if about >~3000 in my PC) you can use something
> like this instead the builtin timsort:
> (To compute the median on a image line the median that uses sort is
> probably better in most cases, expecially if you use the built in sort
> of numerical libraries.)

sort() sorts all of the data, but you're only after one or two numbers, so
the MODFIND method may be faster for the median:


More information about the Python-list mailing list