<div dir="ltr"><br><div class="gmail_quote"><div dir="ltr">On Tue, Jun 23, 2015 at 1:25 PM Paul Sokolovsky <<a href="mailto:pmiscml@gmail.com">pmiscml@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
On Tue, 23 Jun 2015 00:03:14 +0000<br>
"Gregory P. Smith" <<a href="mailto:greg@krypto.org" target="_blank">greg@krypto.org</a>> wrote:<br>
<br>
[]<br>
<br>
> > sleep_ms()<br>
> > sleep_us()<br>
> > monotonic_ms()<br>
> > monotonic_us()<br>
> ><br>
><br>
> If you're going to add new function names, going with the _unit suffix<br>
> seems best.<br>
><br>
> Another option to consider: keyword only arguments.<br>
><br>
> time.sleep(ms=31416)<br>
> time.sleep(us=31415927)<br>
> time.sleep(ns=31415296536)<br>
<br>
That doesn't immediately map to usage for monotonic(), as you mention<br>
below.<br>
<br>
Another issue is that keywords arguments on average (and for<br>
MicroPython all the time) are less efficient than positional. Put it<br>
other way,<br>
<br>
t = monotonic_ns()<br>
t = monotonic_ns() - t<br>
<br>
is going to give lower number than<br>
<br>
t = monotonic(ns=True)<br>
t = monotonic(ns=True) - t<br>
<br>
, and the closer it to 0, the better.<br>
<br>
> # We could use the long form names milliseconds, microseconds and<br>
> nanoseconds but i worry with those that people would inevitably<br>
> confuse ms with microseconds as times and APIs usually given the<br>
> standard abbreviations rather than spelled out.<br>
<br>
Another issue is that full spellings are rather long. Logistically,<br>
while function names can be expected to have autocompletion support,<br>
keyword arguments not necessarily.<br>
<br>
> time.monotonic(return_int_ns=True) ?<br>
> # This seems ugly.  time.monotonic_ns() seems better.<br>
><br>
> These should be acceptable to add to Python 3.6 for consistency.<br>
<br>
Well, as I mentioned, I'm personally not looking for this to be<br>
implemented in CPython right away. Ideally, this should be tested by >1<br>
independent "embedded" Python implementation first, and only then, based<br>
on the actual experience, submitted as a PEP. That's rather better than<br>
"desktop" CPython, which doesn't care about all the subtle "embedded"<br>
aspects "forced" a way to implement it.<br>
<br>
> I do not think we should have functions for each ms/us/ns unit if<br>
> adding functions.  Just choose the most useful high precision unit<br>
> and let people do the math as needed for the others.<br>
<br>
Well, that's one of examples of that "desktop" thinking ;-).<br>
Consider for example that 2^32 microseconds is just over an hour, so<br>
expressing everything in microseconds would require arbitrary-precision<br>
integers, which may be just the same kind of burden for an embedded<br>
system as floats.<br>
<br></blockquote><div><br></div><div>I know. I was actually hoping you'd respond on that point because I haven't used micropython yet. I assumed it had bignum, or at least fixed "big" 64-bit number, support. But if it does not, having specific functions for the needed resolutions makes a lot of sense.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> > Point 3 above isn't currently addressed by time module at all.<br>
> > <a href="https://www.python.org/dev/peps/pep-0418/" rel="noreferrer" target="_blank">https://www.python.org/dev/peps/pep-0418/</a> mentions some internal<br>
<br>
[]<br>
<br>
> Reading the PEP my takeaway is that wrap-around of underlying<br>
> deficient system APIs should be handled by the Python VM for the<br>
> user. It sounds like we should explicitly spell this out though.<br>
<br>
This is another point which is overlooked by "desktop" programmers -<br>
time counters can, will, and do wrap around. Big OSes try hard to to<br>
hide this fact, and indeed succeed well enough, so in cases when they<br>
do fail, it has shattering effect (at least PR-wise) - Y2K, Y2K38<br>
problems. For an embedded programmer wrapping counters is objective<br>
reality, and we wouldn't like to hide that fact in MicroPython<br>
(of course, only for these, newly introduced real-time precision time<br>
functions).<br></blockquote><div><br></div><div>I still don't see how an elapsed() function taking two arbitrary integer arguments could work in a meaningful manner.  Even if you assume they are the same units, the only assumption that can be made is that if the second int is lower than the first, at least one wraparound occurred.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> I don't think time.elapsed() could ever provide any utility in either<br>
> case, just use subtraction.<br>
<br>
Can't work. Previous value of monotonic_us() is 65530, next value is<br>
10, what does it tell you?<br></blockquote><div><br></div><div>At least one wrap around occurred. without more information you cannot know how many.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> time.elapsed() wouldn't know when and<br>
> where the time values came from and magically be able to apply wrap<br>
> around or not to them.<br>
<br>
Well, as I mentioned, it's an API contract that elapsed() takes values<br>
of monotonic_ms(), monotonic_us(), etc. functions, and knows law how<br>
their values change (likely, apply unsigned power-of-2 modular<br>
arithmetics). There's additional restriction that this change law for<br>
all of monotonic_ms(), monotonic_us() is the same, but I personally<br>
find this an acceptable restriction to not bloat API even further. (But<br>
it is a restriction, for example, if nano/microsecond time source is<br>
24-bit counter, than millisecond time is limited to 24 bits too).<br></blockquote><div><br></div><div>I guess what I'm missing is how you intend to tell elapsed() which of the _ms vs _us vs _ns functions the values came from. I'm assuming that all functions are likely to exist at once rather than there being only one high resolution integer time function.</div><div><br></div><div>Given that, yes, you can make elapsed() do what you want.  But I really think you should call it something more specific than elapsed if the function is serving as a common source of information on how a particular type of timer on the system works.  monotonic_elapsed() perhaps?  etc..</div><div><br></div><div>Also, agreed, we don't need these in 3.6.  I'm not seeing anything really objectionable for inclusion in a future 3.x which is all I'm really looking out for. It sounds like avoiding keyword arguments and adding _ms _us and _ns variants of functions is the practical solution for micropython.</div><div><br></div><div>-gps  (awaiting his WiPys :)</div></div></div>