Ask Your Question

Record output of timeit in variable

asked 2012-04-03 07:49:33 +0100

chaesloc2 gravatar image

updated 2012-04-03 09:59:37 +0100

Jason Grout gravatar image

How can I record in a variable the amount of time taken by a function? I read somewhere that timeit is the good way to time stuff, but it seems to print its output instead of returning it.

If possible, I would prefer not to redirect streams or redefine print, etc.

EDIT: got it, leaving the question for reference.


returns a number.

This should probably get a mention in 'timeit?'.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2012-04-03 14:08:14 +0100

When I execute timeit?, I see


This method prints the timing information and does not return
anything, except if the option "seconds=True" was passed, in which
case the wall time in seconds is returned.

I suppose it could be more prominent, but it is there.

edit flag offensive delete link more


That's not what I see; I don't get an OUTPUT section. I suppose my version is too old (4.7.2). Anyway, thanks!

chaesloc2 gravatar imagechaesloc2 ( 2012-04-03 15:15:35 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2012-04-03 07:49:33 +0100

Seen: 277 times

Last updated: Apr 03 '12