0

Circuit Python I/O speed
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Circuit Python I/O speed

by imolas3 on Sun Aug 01, 2021 6:03 am

HI

I am a lecturer in a university in the UK and am considering adopting CP on our course. I have played around with aspect of it and found the direct toggling of I/O pins is very slow. In fact, considerably slower on a 120MHz 32-bit M4 express board than a bog-standard arduino 8-bit 16MHz UNO in C++

I then tested C++ on the M4 and it toggled I/O pins almost 29 times faster. Yes, twenty nine time faster

I hoped to contact Scott to discuss this on his Friday session, but can find no way - he does say at the end of a recorded video "contact me to suggest topics" but does not say how to contact him and I have no idea how.

For our course, better I/O speed is important and I was hoping to discuss ways to implement or access a "fast I/O" mode - I see no reason why a library to support it could not be made part pf CP

I understand Python is VERY slow at the best of times, and you have been abstracting I/O layers to make it easier to support different boards, but I do not see why we should accept a factor of almost 30 times reduction in hardware control:

M4 120MHz CP - toggles 37.8kHz
UNO with C++ toggles 148kHz
M4 120MHz C++ toggles 1098 kHz

imolas3
 
Posts: 3
Joined: Wed Jun 16, 2021 5:27 pm

Re: Circuit Python I/O speed

by tannewt on Mon Aug 02, 2021 3:07 pm

Scott here. Generally I expect folks to contact me on the Adafruit Discord: https://adafru.it/discord Sometimes I forget to say that.

I think you are measuring the execution speed of Python, not necessarily the speed of the toggle itself. This is the trade off that Python makes.

Why do you need pin toggling from software that is faster than 37khz?

tannewt
 
Posts: 2577
Joined: Thu Oct 06, 2016 8:48 pm

Re: Circuit Python I/O speed

by imolas3 on Mon Aug 02, 2021 6:09 pm

Hi Scott - thanks for finding this and contacting me :)

I haven't used discord before and had a look before posting here, but couldn't see how to contact you. Never used or Twitter, discord before............ (and can't see me being a fan)

We do a lot of hardware control in the courses and it is often necessary to generate pulse trains to drive several things and we also read encoders and look at closed loop control.

All works well in C, but my early Python experiments are still inconclusive.

C is great for people who can code already, but students really get frustrated with debugging, often difficult and pedantic syntax, etc.

Python looks easy to learn as a first-time programmer but is looking useful for apps running at 'user speeds' (general interactive apps) rather than hardware speeds (motor control, etc.)

I was hoping there might be some libraries that are speed optimised with less overhead - for example, many C libraries set the port pin directions and other housekeeping before actually setting the pin whereas it can take only a couple of lines of code.

The Arduino DigitalWrite has 13 lines of C and produces 37 assembler instructions which take 6uS to execute. It checks for PWM pin, messes with pushing and popping interrupts and status registers, etc. But If all i am doing is writing to a port pin, then it is a line or two of assembler and none of that other stuff is necessary, It is just belt and braces for the 'general user'.
Writing to a port without this baggage is a line or two of assembler

So, two libraries can exist, one for people who set things up properly and need speed, and one for the less experienced or who don't need speed

imolas3
 
Posts: 3
Joined: Wed Jun 16, 2021 5:27 pm

Re: Circuit Python I/O speed

by tannewt on Tue Aug 03, 2021 5:42 pm

I understand not wanting to get into Twitter but I highly recommend our Discord. It is a moderated chat room. There are many folks who can help with projects and ideas. You can contact me by putting `@tannewt` in a message there though others may be able to answer your questions as well.

MicroPython and CircuitPython aren't good for real-time closed loop control because at any point a garbage collection can happen and they take a non-trivial amount of time. You can get slightly into that with native C modules in *Python but that is still limited by other things occurring.

Coding speed vs performance is a classic trade-off for Python and CircuitPython is no different.

CircuitPython has native modules to handle timing critical tasks such as pwmio to output a PWM signal. This model better matches systems on a chip because we usually will use a separate peripheral to implement the native module. This means that Python isn't good for arbitrary real-time code though like C can be.

Again, I don't think the comparison to arduino's digitalout is quite the same because *Python is running a non-trivial amount of code to execute one Python bytecode to the next.

I think you'll get a quicker uptake with Python like you want but would need to rethink the examples that you use in your teaching. *Python isn't good for motor control and will never be as good as C is. (Rust may be more interesting for future proofing.) See our Learn guides for the areas that *Python excels.

tannewt
 
Posts: 2577
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.