Does MicroPython have constant evaluation (folding)?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
Vinci
Posts: 3
Joined: Sat Jan 26, 2019 6:14 pm

Does MicroPython have constant evaluation (folding)?

Post by Vinci » Mon Mar 25, 2019 2:13 pm

Hello

Does MicroPython support constant evaluation of functions when they get compiled to bytecode? Let's assume I compile a C-string which calls a function with an integer argument (so something like "f(42)") to bytecode and then execute it, is there any chance that the result of that call is pre-calculated?

For some reason my benchmark currently tells me that the MicroPython version of that function is about ~250 times faster than the native C version... ?

thanks

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Does MicroPython have constant evaluation (folding)?

Post by stijn » Mon Mar 25, 2019 2:42 pm

f(42) might have side effects so I'd be surprised if that can somehow get folded, there's nothing which guarantees it will always return the same result? Maybe your bencmark code is incorrect?

ThomasChr
Posts: 121
Joined: Sat Nov 25, 2017 7:50 am

Re: Does MicroPython have constant evaluation (folding)?

Post by ThomasChr » Mon Mar 25, 2019 2:44 pm

I'm quite sure your benchmark code must have some kind of problem. Too fast usually means it's simply not running (because of an error).
If you post your code we can check it.

Vinci
Posts: 3
Joined: Sat Jan 26, 2019 6:14 pm

Re: Does MicroPython have constant evaluation (folding)?

Post by Vinci » Tue Apr 02, 2019 7:35 am

I accidentally measured the compilation of the C-string into bytecode instead of the execution itself... sry. The benchmark produces reasonable results now.

Post Reply