ESP32 very slow with MICROPYTHON !!! ??

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
ixbo
Posts: 36
Joined: Wed May 04, 2022 11:12 am
Location: France

ESP32 very slow with MICROPYTHON !!! ??

Post by ixbo » Thu May 19, 2022 1:29 pm

Hello always the same witch is a little surprised by the ESP32 with MicroPython I know that it's a language interpreted !

The time to put to 1 and return to 0 is about 10us (2*5us) , compare with a dsPIC4013 I often use in ASSEMBLER, the same thing is 70ns , about 142 time less (dsPic is old....!!)

On ESP32 the instruction "utime.sleep_us(1)" take 16us instead of 1 !!!!!!! it's important to know it !

I will generate a pulse of 10us for HC_SR04 a ultrasonic sensor, yes i can use simply pulsehigh 2 time, but is there an another way to generate a accurate time , perhaps with timer .....?

Or perhaps it will be better that I use ARDUINO's language for better performance ? what do you think about that ?

I'am frustrated by the performance of ESP32 (...with Micropython!)

Thank s very much

Best regard

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: ESP32 very slow with MICROPYTHON !!! ??

Post by scruss » Thu May 19, 2022 7:39 pm

The approach for MicroPython is completely different from PIC assembler. Don't hand-roll everything. I've used that library successfully. You don't need to care how long the pulses are that you're sending. It returns the distance directly as a floating point value in centimetres.

You can measure a pulse accurately with machine.time_pulse_us()

The ESP32 port has four hardware timers, too.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Not entirely true.

Post by pythoncoder » Fri May 20, 2022 9:26 am

Different platforms provide their own support for fast operation. In addition to the suggestions of @scruss, ESP32 has RMT. The RP2 has the amazingly flexible PIO. Many platforms have PWM which can be used to provide stable, precise clocks with programmable frequency and duty cycle. STM32 has flexible hardware timers.

Also see the native and viper code emitters. Platforms such as STM32 and RP2 support inline assembler code.

The list goes on...
Peter Hinch
Index to my micropython libraries.

ixbo
Posts: 36
Joined: Wed May 04, 2022 11:12 am
Location: France

Re: ESP32 very slow with MICROPYTHON !!! ??

Post by ixbo » Sat May 28, 2022 8:32 am

Hello thanks for you answers....
Yes i know that the pulse width is not very important it must be > 10us but it was a surprise for me to see that an ESP32 witch is a super microcontroleur was so slow , in fact it's micropython that create this slowness...

I very happy of your answers
Best regards

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: ESP32 very slow with MICROPYTHON !!! ??

Post by karfas » Sat May 28, 2022 12:00 pm

ixbo wrote:
Sat May 28, 2022 8:32 am
In fact it's micropython that create this slowness...
What did you expect from an interpreted, highly dynamic language like (micro)python? Even CPython isn't famous for its performance. It's not a surprise for me that simple function calls require a few ns.

For really time critical applications both micropython as well as the ESP32 are most likely not the right tools for the job.
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

ixbo
Posts: 36
Joined: Wed May 04, 2022 11:12 am
Location: France

Re: ESP32 very slow with MICROPYTHON !!! ??

Post by ixbo » Sun May 29, 2022 9:58 am

Hello thanks for your answer

I do not expect anything ,it's just a surprise for me I use most frequently ASSEMBLER with dsPIC4013 (MICRCHIP) with this language I know excatly for each instruction what happen....and speed of my code.
With these sort of language I' m very far to know what happen but it's VERY VERY rapid to make a program !
(for example I have done a drone all the code written in ASSEMBLER , all computing in 32 bit (dsPIC is 16 bit !, about 60000 lines ! , yes it's a little crazy !!! , perhaps little is not the good word !!l)

Thanks very much
Best regards

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Assembler is lovely. In small doses.

Post by pythoncoder » Mon May 30, 2022 5:36 pm

The key to optimisation is to identify the bottlenecks and optimise only those. The beauty of MicroPython is that it enables you to do exactly that, using the techniques mentioned above.

I too have written substantial programs in Assembler, but only when there was absolutely no choice. Given a choice I'd choose an interpreted language for those parts where speed is not an issue, dropping down to C, Assembler, or specialist hardware support where necessary. How can you do that on a microcontroller? MicroPython...
Peter Hinch
Index to my micropython libraries.

User avatar
andypiper
Posts: 25
Joined: Wed Feb 02, 2022 12:17 pm
Location: Kingston upon Thames, UK
Contact:

Re: ESP32 very slow with MICROPYTHON !!! ??

Post by andypiper » Tue May 31, 2022 9:55 am

scruss wrote:
Thu May 19, 2022 7:39 pm
[*]Look at Awesome MicroPython.
Aside: we really should get that page linked more prominently from the MicroPython website and docs, I should get to work on this issue.

Post Reply