[issue35904] Add statistics.fmean(seq)

Steven D'Aprano report at bugs.python.org
Fri Feb 8 00:28:53 EST 2019


Steven D'Aprano <steve+python at pearwood.info> added the comment:

> On my current 3.8 build, this code given an approx 500x speed-up

On my system, I only get a 30x speed-up using your timeit code. Using 
ints instead of random floats, I only get a 9x speed-up.

This just goes to show how sensitive these timing results are on 
platform and hardware.

What do you think of this implementation?

def floatmean(data:Iterable) -> Float:
    try:
        n = len(data)
    except TypeError:
        # Handle iterators with no len.
        n = 0
        def count(x):
            nonlocal n
            n += 1
            return x
        total = math.fsum(map(count, data))
        return total/n
    else:
        return math.fsum(data)/n

Compared to the "no frills" fsum()/len() version:

- I see no visible slowdown on lists of floats;
- it handles iterators as well.

On my computer, the difference between the sequence path and the 
iterator path is just a factor of 3.5. How does it compare on other 
machines?

As for the name, I think we have three reasonable candidates:

float_mean
fast_mean
fmean

(with or without underscores for the first two). Do people have a 
preference?

----------
title: Add statistics.fastmean(seq) -> Add statistics.fmean(seq)

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35904>
_______________________________________


More information about the Python-bugs-list mailing list