ws2811/2812 Programs very slow

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
__deets__
Posts: 19
Joined: Sun Aug 20, 2017 4:50 pm

Re: ws2811/2812 Programs very slow

Post by __deets__ » Fri Sep 28, 2018 2:22 pm

jickster wrote:
Tue Sep 25, 2018 6:06 pm
Shouldn’t “b” point to the same int object in memory?

Same principle
c = (255,0,0)
d = (255,0,0)

Maybe my understanding is wrong. Idk. Send me a link to read if I’m wrong.
It is wrong. There are optimizations for smallish integers to point to the same ram, but generally speaking objects are not aggressively checked for equality, and then aliased. See below code for a small example to illustrate this. Also, doing so would need pretty nifty compile and runtime optimizations proving the properties of the elements of two tuples are in fact immutable and the same. This is not even done on "big" Python, let alone it's smaller cousin.

Code: Select all

>>> a = (255, 0, 0)
>>> b = (255, 0, 0)
>>> id(a)
1073674352
>>> id(b)
1073674464
>>> c = 255
>>> d = 255
>>> id(c)
511
>>> id(d)
511

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: ws2811/2812 Programs very slow

Post by jickster » Fri Sep 28, 2018 2:23 pm

__deets__ wrote:
jickster wrote:
Tue Sep 25, 2018 6:06 pm
Shouldn’t “b” point to the same int object in memory?

Same principle
c = (255,0,0)
d = (255,0,0)

Maybe my understanding is wrong. Idk. Send me a link to read if I’m wrong.
It is wrong. There are optimizations for smallish integers to point to the same ram, but generally speaking objects are not aggressively checked for equality, and then aliased. See below code for a small example to illustrate this. Also, doing so would need pretty nifty compile and runtime optimizations proving the properties of the elements of two tuples are in fact immutable and the same. This is not even done on "big" Python, let alone it's smaller cousin.

Code: Select all

>>> a = (255, 0, 0)
>>> b = (255, 0, 0)
>>> id(a)
1073674352
>>> id(b)
1073674464
>>> c = 255
>>> d = 255
>>> id(c)
511
>>> id(d)
511

What about inside functions?


Sent from my iPhone using Tapatalk Pro

__deets__
Posts: 19
Joined: Sun Aug 20, 2017 4:50 pm

Re: ws2811/2812 Programs very slow

Post by __deets__ » Fri Sep 28, 2018 2:27 pm

What about them? Put the code into a function and try for yourself. The result doesn't change.

I don't know what stackoverflow article suggests that there is an optimisation. I just checked, it is actually optimised for the simple case in CPython, but statements about optimizations can't be generalised over different python implementation.

And the CPython-optimisation is pretty easy to break:

Code: Select all

i = 255
def foo():
    for _ in range(10):
        t = (i, 0, 0)
        print(id(t))

foo()
does not optimize anymore.

wangshujun@tom.com
Posts: 2
Joined: Fri Feb 15, 2019 9:22 am

Re: ws2811/2812 Programs very slow

Post by wangshujun@tom.com » Fri Feb 15, 2019 9:34 am

There is a fill method in the module of neopixel, which can fill all the pixels quickly, so that your second cycle can be cancelled, and the speed should be improved a lot.

In addition, the data in NP [i] can be read out, so that only the elements that need to be changed can be changed, and it is not necessary to modify them all once.


np.fill((0,0,0))
>>> print(np[0])
(0, 0, 0)
>>> np[0]
(0, 0, 0)

Post Reply