Page 1 of 1

Python 3 && pi 3 time.time() how to translate to int?

Posted: Tue May 02, 2017 3:21 am
by RinksCustoms
New to python (from Parallax's STAMP's & Propeller, and recently Arduino and C++) I'm not a pro coder, this is not my profession, just my passion and hobby.

[b]According to Python 3.3+ documentation[/b] the time.get_clock_info(name) returns a namespace object.
When I query ipython3 with : [code]time.get_clock_info('perf_counter')[/code] , it returns with the namespace object printed out.
I noticed a difference with calling 'perf_counter' vs 'clock' in that the reported resolution from 'perf_counter' is 1e-09 and the resolution from 'clock' is 1e-06..
And while running this quick, tight iter:
[code]for t in range(10):
iv[t] = time.time()
for y in range(10):
print(iv[y])[/code]
I get numbers as :
1493761419.8588312
1493761419.8588355
1493761419.8588371
1493761419.8588393
1493761419.8588412
1493761419.8588430
1493761419.8588452

Similarly, the time.clock() prints out as a float (0.700483)

[b]So, how would I be able to convert these floats to usable int values like a long or double representing uS or nS "ticks"?[/b]
I will try to "&"-off the larger significant number and just multiply the fractal part by 1e06 and change it to an int as the fractal part which seems fairly accurate to 1e-06 when using time.time()

Edit: Title and some code and output display for clarity.

Re: Python 3 && pi 3 time.time() || time.clockl() resolution

Posted: Tue May 02, 2017 5:33 am
by gkreidl
Why don't you just look into the Python (3) documentation?
time.time()
Return the time in seconds since the epoch as a floating point number.

Re: Python 3 && pi 3 time.time() || time.clockl() resolution

Posted: Tue May 02, 2017 9:40 pm
by RinksCustoms
hello, yes, that is exactly what the python documentation says. You are correct. However that wasn't my question, my question was pertaining to how to convert those floats to an int so that I can time my code without time.sleep(0.1) and i can be doing other useful things with my code like generating the GUI.
floats do not help, ints as nS/uS/mS do help however.

Re: Python 3 && pi 3 time.time() || time.clockl() resolution

Posted: Tue May 02, 2017 10:18 pm
by Paeryn
RinksCustoms wrote:hello, yes, that is exactly what the python documentation says. You are correct. However that wasn't my question, my question was pertaining to how to convert those floats to an int so that I can time my code without time.sleep(0.1) and i can be doing other useful things with my code like generating the GUI.
floats do not help, ints as nS/uS/mS do help however.
If you've got the time as a float number of seconds and you want an integer number of microseconds then just multiply it by a million and convert it to an int (1e6 is shorthand for 1000000).

Code: Select all

>>> time_in_s = time.time()
>>> time_in_us = int(time_in_s * 1e6)
>>> print("Time in seconds = ", time_in_s)
Time in seconds =  1493762792.195797
>>> print("Time in us = ", time_in_us)
Time in us =  1493762792195797
Similarly if you want it in nanoseconds change the 1e6 into 1e9.

<Edit> I incorrectly used ms instead of us to mean microseconds...

Re: Python 3 && pi 3 time.time() how to translate to int?

Posted: Tue May 02, 2017 11:52 pm
by RinksCustoms
Thank you Paeryn for the very clear answer. I had a thought of doing it that way just now. Just found a post on stack exchange that explained why the samples were not 1e-6 alligned (overhead with python) and why the results were more like 1e-3 and 1e-5 accurate. The post on Stack exchange leads one to believe that if your loops need to be super tight (greater than 1e-6 accurate), then those loops should be implemented in C as an add-on module (with python API wrapper I think).

I got thrown by the fact time.time() or .clock() returns floats and not a huge long representing a counting register of MCU clock ticks since power-on. I get it now, 0.001 = is literally 1mS.
I also realize pythons limitations now if your performance loop really needs to be performance. (greater than 10KHz)
the for iter loop does nothing more than grab 10 samples fast as possible, the accuracy seems to be limited to about 1e-5 before the numbers go from orderly to jumping, its a consistent jump like 1,3,5,7,9 though at the 1e-6 level if you need to do something in your loops that do something on 0.000002s after doing something at 0.000000s you'll miss the compare.
I just didn't know about this limitation from my understanding of python in general and any interpreted language.

And while the time.get_clock_info('name') works great in showing the different clocks available and their resolutions like the 'perf_counter' having 1e-9 resolution, python cannot take advantage of the performance counter, where a tight C loop could.

Good to know.