time.wallclock() or other similar stuff

Bringing this in from python-dev, and http://bugs.python.org/issue10278 Summary: time.clock() means two different things on Windows and Unix. So, if you need to measure time across a blocking call in a portable way, you need to use time.time() (this fact is not documented). Time.time has its own problems though, such as low resolution (compared to time.clock on windows) and being susceptible to being adjusted by the user. I proposed adding a time.wallclock() to address this issue, which would be the most suitable function for the job on any platform. The patch is an example implementation. Any thoughts? And yes, I acknowledge that the time module is complicated enough as it is :) K

Kristján Valur Jónsson wrote:
I'm not particularly keen on "wall_clock" as a name. The name suggests it should return something like "12:37:15". I prefer a name that tells you what you would use the function for: this is the right function to use for timers, so call it "timer". 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 (Aside: what about WinCE? Shouldn't it also use time.clock?) and the timeit.Timer class takes an argument "timer" which defaults to default_timer, so there is precedent on the name. +1 on a platform-dependent alias to time() or clock() +1 on calling it "timer" -0 on calling it "wall_clock" -- Steven

Well, maybe all we need is the timeit module, then? I didn't realize that we have default_timer there. Otherwise, I'm not sure about "timer". To me that word implies some sort of context to be timed, or action to be taken, but I'm +0 on it. K -----Original Message----- From: python-ideas-bounces+kristjan=ccpgames.com@python.org [mailto:python-ideas-bounces+kristjan=ccpgames.com@python.org] On Behalf Of Steven D'Aprano Sent: Tuesday, November 02, 2010 9:51 To: python-ideas@python.org Subject: Re: [Python-ideas] time.wallclock() or other similar stuff 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 +1 on a platform-dependent alias to time() or clock() +1 on calling it "timer" -0 on calling it "wall_clock" -- Steven _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas

Alexander Belopolsky wrote:
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: http://docs.python.org/library/timeit.html (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? -- Steven

If you're looking for new function names, I'd suggest a pair of functions called real_time() and cpu_time(). -- Greg

On 2 November 2010 11:35, Antoine Pitrou <solipsis@pitrou.net> wrote:
I'd agree with this. I'm actually +1 on the original time.wallclock(), wall time is a common concept, so having the word "wall" in it's api seems very natural to me. .walltime() would probably work as well. Regards Floris -- Debian GNU/Linux -- The Power of Freedom www.debian.org | www.gnu.org | www.kernel.org

On 02/11/2010 13:21, Floris Bruynooghe wrote:
I beg to differ. Seeing "wall clock" just confused me - my reaction was what the ..... is he talking about? Even when I understood it, it didn't seem a very appropriate name - I would have found something like "system clock" or "system time" more intelligible. Whereas "timer" is a very familiar concept (in computing, and in life in general). And I think most people are familiar with time.time(), so will recognize that time.timer() is something different. Though I agree it would be better if the names were more different. Best wishes Rob Cliffe

On Tue, 02 Nov 2010 13:43:04 +0000 Rob Cliffe <rob.cliffe@btinternet.com> wrote:
I share this point of view. "wallclock" is really misleading, it simply does not tell what the object is, let's one guess something totally different; and what about its purpose? (also think at non-english speakers). Imo, "timer" is exactly the proper term -- the issue due to proximity with "time" is real, but far less important. Denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.com

On 2010-11-02 16:17, James Noble wrote:
With that name, I would expect a "proper" stopwatch object with "get", "start", "stop" and "reset" methods. Not just an alias for time.time or time.clock. I wouldn't mind seeing such an object in the stdlib, but it is easy enough to define in terms of the function that is actually proposed. For example: from time import timer # the new function class stopwatch(object): def __init__(self, t=0.0): self._value = t self._started = None def get(self): s = self._started if s is not None: return self._value + timer() - s return self._value def reset(self, t=0.0): self._value = t if self._started is not None: self._started = timer() def is_running(self): return self._started is not None def start(self): if self._started is None: self._started = timer() def stop(self): s = self._started if s is not None: self._value += timer() - s self._started = None value = property(get, reset, reset) running = property(is_running, None, stop) @running.setter def running(self, value): if value: if self._started is None: self._started = timer() else: s = self._started if s is not None: self._value += timer() - s self._started = None Oh, and +1 for the proposed function - I don't really care what it's called. - Jacob

On Tue, 2 Nov 2010 16:02:47 +0100 spir <denis.spir@gmail.com> wrote:
Imo, "timer" is exactly the proper term
I'm not sure it's the proper term. A timer is often a countdown with a callback. See for example threading.Timer and signal.setitimer.
the issue due to proximity with "time" is real, but far less important.
I think it would be too easy to use one instead of the other, and get strange semantics (both count in seconds but with slightly different characteristic). Regards Antoine.

Kristján Valur Jónsson wrote:
You might want to look at the systimes.py module that comes with pybench (see Tools/pybench). This already provides a cross-platform way of accessing high accuracy timers for benchmarking and the like. For obvious reasons this tries to measure run-time, though, and not elapsed time. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Nov 02 2010)
::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/

On 2 November 2010 22:17, Nick Coghlan <ncoghlan@gmail.com> wrote:
I'm +1 on the functionality and either elapsed_time or wallclock is fine with me. Perhaps the patch author should decide... Michael

Kristján Valur Jónsson wrote:
I'm not particularly keen on "wall_clock" as a name. The name suggests it should return something like "12:37:15". I prefer a name that tells you what you would use the function for: this is the right function to use for timers, so call it "timer". 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 (Aside: what about WinCE? Shouldn't it also use time.clock?) and the timeit.Timer class takes an argument "timer" which defaults to default_timer, so there is precedent on the name. +1 on a platform-dependent alias to time() or clock() +1 on calling it "timer" -0 on calling it "wall_clock" -- Steven

Well, maybe all we need is the timeit module, then? I didn't realize that we have default_timer there. Otherwise, I'm not sure about "timer". To me that word implies some sort of context to be timed, or action to be taken, but I'm +0 on it. K -----Original Message----- From: python-ideas-bounces+kristjan=ccpgames.com@python.org [mailto:python-ideas-bounces+kristjan=ccpgames.com@python.org] On Behalf Of Steven D'Aprano Sent: Tuesday, November 02, 2010 9:51 To: python-ideas@python.org Subject: Re: [Python-ideas] time.wallclock() or other similar stuff 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 +1 on a platform-dependent alias to time() or clock() +1 on calling it "timer" -0 on calling it "wall_clock" -- Steven _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas

Alexander Belopolsky wrote:
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: http://docs.python.org/library/timeit.html (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? -- Steven

If you're looking for new function names, I'd suggest a pair of functions called real_time() and cpu_time(). -- Greg

On 2 November 2010 11:35, Antoine Pitrou <solipsis@pitrou.net> wrote:
I'd agree with this. I'm actually +1 on the original time.wallclock(), wall time is a common concept, so having the word "wall" in it's api seems very natural to me. .walltime() would probably work as well. Regards Floris -- Debian GNU/Linux -- The Power of Freedom www.debian.org | www.gnu.org | www.kernel.org

On 02/11/2010 13:21, Floris Bruynooghe wrote:
I beg to differ. Seeing "wall clock" just confused me - my reaction was what the ..... is he talking about? Even when I understood it, it didn't seem a very appropriate name - I would have found something like "system clock" or "system time" more intelligible. Whereas "timer" is a very familiar concept (in computing, and in life in general). And I think most people are familiar with time.time(), so will recognize that time.timer() is something different. Though I agree it would be better if the names were more different. Best wishes Rob Cliffe

On Tue, 02 Nov 2010 13:43:04 +0000 Rob Cliffe <rob.cliffe@btinternet.com> wrote:
I share this point of view. "wallclock" is really misleading, it simply does not tell what the object is, let's one guess something totally different; and what about its purpose? (also think at non-english speakers). Imo, "timer" is exactly the proper term -- the issue due to proximity with "time" is real, but far less important. Denis -- -- -- -- -- -- -- vit esse estrany ☣ spir.wikidot.com

On 2010-11-02 16:17, James Noble wrote:
With that name, I would expect a "proper" stopwatch object with "get", "start", "stop" and "reset" methods. Not just an alias for time.time or time.clock. I wouldn't mind seeing such an object in the stdlib, but it is easy enough to define in terms of the function that is actually proposed. For example: from time import timer # the new function class stopwatch(object): def __init__(self, t=0.0): self._value = t self._started = None def get(self): s = self._started if s is not None: return self._value + timer() - s return self._value def reset(self, t=0.0): self._value = t if self._started is not None: self._started = timer() def is_running(self): return self._started is not None def start(self): if self._started is None: self._started = timer() def stop(self): s = self._started if s is not None: self._value += timer() - s self._started = None value = property(get, reset, reset) running = property(is_running, None, stop) @running.setter def running(self, value): if value: if self._started is None: self._started = timer() else: s = self._started if s is not None: self._value += timer() - s self._started = None Oh, and +1 for the proposed function - I don't really care what it's called. - Jacob

On Tue, 2 Nov 2010 16:02:47 +0100 spir <denis.spir@gmail.com> wrote:
Imo, "timer" is exactly the proper term
I'm not sure it's the proper term. A timer is often a countdown with a callback. See for example threading.Timer and signal.setitimer.
the issue due to proximity with "time" is real, but far less important.
I think it would be too easy to use one instead of the other, and get strange semantics (both count in seconds but with slightly different characteristic). Regards Antoine.

Kristján Valur Jónsson wrote:
You might want to look at the systimes.py module that comes with pybench (see Tools/pybench). This already provides a cross-platform way of accessing high accuracy timers for benchmarking and the like. For obvious reasons this tries to measure run-time, though, and not elapsed time. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Nov 02 2010)
::: Try our new mxODBC.Connect Python Database Interface for free ! :::: eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/

On 2 November 2010 22:17, Nick Coghlan <ncoghlan@gmail.com> wrote:
I'm +1 on the functionality and either elapsed_time or wallclock is fine with me. Perhaps the patch author should decide... Michael
participants (17)
-
Alexander Belopolsky
-
Antoine Pitrou
-
Brian Curtin
-
Floris Bruynooghe
-
geremy condra
-
Greg Ewing
-
Jacob Holm
-
James Noble
-
Kristján Valur Jónsson
-
M.-A. Lemburg
-
Michael Foord
-
MRAB
-
Nick Coghlan
-
Rob Cliffe
-
spir
-
Steven D'Aprano
-
Terry Reedy