Page 1 of 1

utime.ticks_diff() between pycom and upython

Posted: Thu May 23, 2019 6:13 pm
by Frida
In upython, utime.ticks_diff (new, old) gives positive results.
In pycom, utime.ticks_diff (old, new) gives positive results.
Old and new have been switched, I wonder why, there is no
change in the underlying code. It must be somewhere,
between the call and the underlying code.

ps. I prefer the pycom way.

Code: Select all

import time
time.time()
start = time.ticks_ms()

while 1:
	#if time.ticks_diff(time.ticks_ms(), start) > 10000: # esp32 pico upython
	if time.ticks_diff(start, time.ticks_ms()) > 10000: # pycom wipy
		break        
time.time()

deadline = time.ticks_add(time.ticks_ms(), 100)

#while time.ticks_diff(deadline, time.ticks_ms()) > 0:	# esp32 pico upython
#	print(time.ticks_diff(deadline, time.ticks_ms()))	# esp32 pico upython
while time.ticks_diff(time.ticks_ms(), deadline) > 0:	# pycom wipy
	print(time.ticks_diff(time.ticks_ms(), deadline))	# pycom wipy


Re: utime.ticks_diff() between pycom and upython

Posted: Thu May 23, 2019 7:08 pm
by Christian Walther
It was changed at some point after a lengthy discussion: https://github.com/micropython/micropython/issues/2217

Re: utime.ticks_diff() between pycom and upython

Posted: Fri May 24, 2019 5:44 am
by Roberthh
Obviously there is a difference in the underlying code, and yes, this change by Pycom was definitely not necessary, and they may switch back. Besides that, you can make you code for that, by introducing a factor:

diff = ticks_diff(old, new) * ticks_diff(1, 2)

That will give you the same result on both systems. If you pre-calculate the factor, you'll save the second call.

Re: utime.ticks_diff() between pycom and upython

Posted: Fri May 24, 2019 1:41 pm
by dhylands
Presumably, the second call should be ticks_diff(1, 2)

Re: utime.ticks_diff() between pycom and upython

Posted: Fri May 24, 2019 1:50 pm
by Roberthh
Sure