MicroPython code optimization guidelines (idea)

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

MicroPython code optimization guidelines (idea)

Post by pfalcon » Thu May 01, 2014 6:17 pm

Besides http://forum.micropython.org/viewtopic.php?f=3&t=50 , a project I would like to start is to provide guide on how to get most speed/least memory usage out of Python code run with MicroPython. While it's clear that some tricks clear will offer speed benefit, others are not so clear, plus many tricks conflict with abstraction/object-orientation best practices, so it shouldn't be "IMHO, you should" guide, but rather should back claims with performance tests, so people can decide for themselves whether they want to compromise extensibility/flexibility for 3% speedup.

Well, even http://forum.micropython.org/viewtopic.php?f=3&t=50 is backlogged, and nobody contributes to it so far. So, I don't really feel like opening another front of work. Thus, I open this thread to collect initial ideas and get together people interested in working on this.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: MicroPython code optimization guidelines (idea)

Post by pfalcon » Sun May 04, 2014 10:34 pm

I gave up and landed some proof-of-concept framework: https://github.com/micropython/micropyt ... acfde91d0e . Even initial results are wildly unexpected, showing that intuitive hunches are bad helper in optimization ;-).
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: MicroPython code optimization guidelines (idea)

Post by pfalcon » Tue May 06, 2014 9:10 pm

Per discussion at https://github.com/micropython/micropyt ... t-42213955 , docstrings, while don't have much use for MicroPython, do take precious memory. So, if you're tight on memory, don't use docstrings. (And we'll need to fix this eventually.)
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: MicroPython code optimization guidelines (idea)

Post by pfalcon » Thu Jun 19, 2014 7:52 pm

pfalcon wrote:I gave up and landed some proof-of-concept framework: https://github.com/micropython/micropyt ... acfde91d0e .
I'd like to remind about this project and invite folks to give it a run and contribute (well thought ;-) ) cases. I just landed few more interesting testcases, based on discussion in http://forum.micropython.org/viewtopic. ... rt=10#p621

Here's walkthru to get you started. Assumed you build unix version of MicroPython already. Simple way is to go to "test/" dir and run "./run-bench-tests" that will run all available comparative benchmarking tests, and will take a while. You can also run testcase groups one by one:

Code: Select all

$ ./run-bench-tests bench/funcall-*
bench/funcall:
    0.713s (+00.00%) bench/funcall-1-inline.py
    3.725s (+422.49%) bench/funcall-2-funcall.py
    3.105s (+335.54%) bench/funcall-3-funcall-local.py
1 tests performed (3 individual testcases)
So, we just learned, that for trivial operation, a function call makes 5.22 times more overhead than running (trivial!) operation inline. We also learned that using a known trick of caching something (function reference in this case) in a local variable does help a bit, but not that much, cutting the overhead to 4.35 times.

Few obvious things, which still rather be said explicitly:

1. The above results doesn't mean that you should avoid using function. Only that you should avoid using functions excessively. For example, if you have an object with fields x, y & z, there's no need to add trivial get_x(), get_y(), get_z() methods. Python is not C++ or Java, do "obj.x" directly.

2. If you're really short of speed, yes, you may consider to get rid of function call and inline the code instead. But that will give useful results only for relatively simple functions called very many times in loop. If both of the above is not your case, you won't see much (if any) speed up. So again, do not avoid functions.

3. The exact results shown above are valid only for my system, my compiler, my compiler options. That's why, instead of giving you my results, I encourage you to run tests yourself and assess your own results (and then keep in mind, that those are only your results, someone else may expect different values). With that in mind, these tests surely show general picture.

4. These tests are not just to optimize your apps, they're also to show bottlenecks in MicroPython implementation. So, if you find outrageous that calling a function 5 times slower than not calling it, patches welcome! ;-)
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Post Reply