On 02/11/2010 03:38, Steven D'Aprano wrote:
Alexander Belopolsky wrote:
On Mon, Nov 1, 2010 at 9:50 PM, Steven D'Aprano email@example.com wrote: ..
The timeit module includes:
if sys.platform == "win32": # On Windows, the best timer is time.clock() default_timer = time.clock else: # On most other platforms the best timer is time.time() default_timer = time.time
Doesn't this mean that the requested functionality is already available as
from timeit import default_timer
Well, yes, it does, but:
(1) How many people would think to look in timeit for this function? Both you and Kristján were apparently surprised that it already exists, and I'd bet money that you weren't the only ones. timeit seems to be more of an application than a library (although I acknowledge the border between the two is fuzzy), and people don't think about using it as a library.
(2) The time module is the natural place to look for time-related functions. It would be nicer if timeit could import the default timer from time:
from time import timer as default_timer # or wall_clock if you prefer
(3) The default_timer is an implementation detail of timeit. It could change, or disappear, or be renamed. You'll note it isn't documented:
(4) I started off thinking that we should just document timer (wall_clock) as an alias to one of time() or clock(), but I changed my mind. That's an implementation detail that we should be free to change in the future. For the sake of information hiding, we shouldn't promise that the timer will be one of time.clock() or time.time(). Who knows, maybe there are (or will be someday) supported platforms where neither choice is appropriate, and the timer needs to be something else?