On Fri, Nov 12, 2010 at 02:11, Steven D'Aprano <st...@pearwood.info> wrote: > Richard D. Moores wrote: > >> I find using that at the interactive prompt a bit onerous -- lots of >> copy and pasting. And doubly so when comparing times for 2 or more >> functions. > > Does your Python not support readline? Normally, if you press UP ARROW or > DOWN ARROW, Python will cycle through the previous interpreter lines.
Do you mean my IDE? IDLE does that with Alt+P and Alt+N. I'm dealing with multi-line functions, not single lines. If I paste a function at the prompt, IDLE will bring the whole thing back with Alt+P. Problem is, IDLE isn't my IDE -- Wing is. > Another approach is to write helper functions, or use string interpolation, > to make it easy to re-use code: > > setup = "from __main__ import %s as func" > test = "func(1000)" > t1 = Timer(test, setup % "my_func") > t1 = Timer(test, setup % "your_func") > > A third approach might be to treat your testing as a script. Put all your > test code in a module, and then run it: > > python time_test.py OK, but why can't I do what the timeit doc suggests, only put 2 or more functions in the file, as I do here: <http://tutoree7.pastebin.com/84u1fkgA> def test(): "Stupid test function" L = [] for i in range(100): L.append(i) if __name__=='__main__': from timeit import Timer t = Timer("test()", "from __main__ import test") print t.timeit() >> The timeit doc gave me the obvious idea of how to avoid the prompt and >> also easily compare the times of 2 or more functions. I'd like to know >> if doing it this way is correct: Please see >> <http://tutoree7.pastebin.com/84u1fkgA> > > You're vulnerable to statistical outliers (which are remarkably common on > multi-tasking operating systems!) cause by the OS calling some other program > in the middle of yours. Call each time test three or five times, and use the > smallest. Sure, I do that with the for loops, don't I? >> Huh. Just realized that this timing method doesn't include the 5 >> repeats called for by Steven's method. So how about using a for loop? >> As in <http://tutoree7.pastebin.com/J8bPKUqC>. > > You're still re-inventing the wheel. timeit already includes a method for > doing exactly that: repeat. From the documentation: > > def repeat(self, repeat=default_repeat, number=default_number): > """Call timeit() a few times. > > This is a convenience function that calls the timeit() > repeatedly, returning a list of results. ... I'm sorry, Steven, but I could you revise this code to use repeat=5 instead of the for loop? I can't see how to do it. if __name__=='__main__': from timeit import Timer for y in range(5): t = Timer("proper_divisors_sum1(500000)", "from __main__ import proper_divisors_sum") print round(t.timeit(number=10000),3) Thanks, Dick _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor