Page 1 of 1

Does MicroPython have constant evaluation (folding)?

Posted: Mon Mar 25, 2019 2:13 pm
by Vinci
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

Re: Does MicroPython have constant evaluation (folding)?

Posted: Mon Mar 25, 2019 2:42 pm
by stijn
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?

Re: Does MicroPython have constant evaluation (folding)?

Posted: Mon Mar 25, 2019 2:44 pm
by ThomasChr
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.

Re: Does MicroPython have constant evaluation (folding)?

Posted: Tue Apr 02, 2019 7:35 am
by Vinci
I accidentally measured the compilation of the C-string into bytecode instead of the execution itself... sry. The benchmark produces reasonable results now.