[issue6422] timeit called from within Python should allow autoranging

Steven D'Aprano report at bugs.python.org
Thu May 12 12:49:28 EDT 2016


Steven D'Aprano added the comment:

On Thu, May 12, 2016 at 04:49:59AM +0000, Nick Coghlan wrote:
> The embedded side-effects were my main concern with Scott's original 
> patch, so Steven's callback-based approach strikes me as a definite 
> improvement. However, the awkwardness of the revised calling code in 
> main does make me wonder whether or not this might be better 
> implemented as a generator rather than as a function accepting a 
> callback:

I thought about a generator too, but then I thought about the *non* 
verbose case, where you don't care about the intermediate results, only 
the final (number, time_taken) pair.

# function with callback:
number, time_taken = t.autorange()

# generator
number, time_taken = list(t.autorange())[-1]

Which hints that your code snippet is buggy, or at least incomplete:

>     try:
>         results = list(t.autorange())
>     except:
>         t.print_exc()
>         return 1
>     if verbose:
>         for number, time_taken in results:
>             msg = "{} loops -> {:.{}g} secs"
>             print(msg.format(number, time_taken, precision))

If verbose is False, you never set number and time_taken. So you need an 
else clause:

    else:
        number, time_taken = results[-1]

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue6422>
_______________________________________


More information about the Python-bugs-list mailing list