How Fast Does Your Arduino Code Run? ⏱

  Рет қаралды 26,805

Wokwi

Wokwi

Күн бұрын

Пікірлер: 58
@Electrologia
@Electrologia 2 жыл бұрын
6:35 TIP: If your frequency is 16 Mhz then (1/f = T) it means that 1 cycle takes 1/16 000 000 = 62.5 ns and if you have 38 cycles. 38 * 62.5 ns = 2375 ns = 2.38 micro seconds.
@VivekYadav-ds8oz
@VivekYadav-ds8oz 2 ай бұрын
is it really tip or is that extremely basic mathematical aptitude?
@noon1117
@noon1117 4 жыл бұрын
Man, I was looking for these type of videos where they explain the register level timing codes and their functions. Great video!
@Wokwi
@Wokwi 4 жыл бұрын
Happy to hear that! What would you like to see next?
@noon1117
@noon1117 4 жыл бұрын
@@Wokwi Not sure if it'll be interesting for everyone. But I was wondering if we could generate high frequency (~10--100 Khz) with PWM control using registers directly.
@Wokwi
@Wokwi 4 жыл бұрын
@@noon1117 Thanks! Yes, that'a good idea!
@nsfeliz7825
@nsfeliz7825 4 жыл бұрын
yes yes, I made a crude arduino clock a few months ago, you idea will help improve it. it won't be Casio accuracy but its good enough.
@I_am_Ashok
@I_am_Ashok 4 ай бұрын
Mind blowing video. May god keep you happy and healthy.
@UltimateRobotics
@UltimateRobotics 4 жыл бұрын
Thanks! Always knew digitalWrite takes a while, but never cared enough to measure how long precisely :)
@Wokwi
@Wokwi 4 жыл бұрын
Now you know :)
@talofer99
@talofer99 4 жыл бұрын
Super cool and interesting, the part with the delaymicroseconds not being accurate actually surprised me. And thanks a lot for the reference on the port manipulation video :)
@jenalynreyes1603
@jenalynreyes1603 4 жыл бұрын
Subscribed. Awsome content it makes my code more optimized.
@thomasdalberto6762
@thomasdalberto6762 8 ай бұрын
It would be interesting to see how this could be adapted to work with the new Arduino UNO R4. It is not based on an ATmega chip therefore the registers are different and from what I could see also the execution of instruction is rather different. I tried to apply the same concept presented in this video using the General Purpose Timers but I could not obtain a working example. Maybe you are able to do it having more experience in the field.
@autotuneengineering
@autotuneengineering 11 ай бұрын
Great video! Thanks. Subscribed.
@Wokwi
@Wokwi 11 ай бұрын
Thank you!
@pmwicaksana
@pmwicaksana 7 ай бұрын
Keren om, penjelasannya sangat bagus👍
@ExatasDistribuidas
@ExatasDistribuidas Жыл бұрын
Excelente vídeo. Obrigado pela aula
@fersto6403
@fersto6403 4 жыл бұрын
Excellent video!
@Wokwi
@Wokwi 4 жыл бұрын
Thank you very much!
@mrsubery
@mrsubery 4 жыл бұрын
Excellent video! Thank you
@joostpalucki6560
@joostpalucki6560 8 ай бұрын
useful information, thanks
@chucklearnslithics3751
@chucklearnslithics3751 2 жыл бұрын
This is what I've been looking for. Experimenting with branchless Arduino programming and measuring its speed improvement has been problematic. I would be interested in your take on branchless Arduino programming.
@edgarbonet1
@edgarbonet1 Жыл бұрын
Branchless programming is not really interesting on AVR-based Arduinos. The AVR has a very shallow pipeline with only two stages: fetch and execute. The branch penalty is then minimal: - an unconditional jump takes always two cycles - a conditional jump takes two cycles if the branch is taken, and one cycle if it's not.
@ajciccone88
@ajciccone88 2 жыл бұрын
Fantastic video. This video, this video and the optimize video are really explained very well and I learned a good bit in them. Hopefully we see more of these in the future! Btw WOKWI is a godsend and I am grateful everyday for it.
@Wokwi
@Wokwi Жыл бұрын
Thank you! What project are you working on?
@ajciccone88
@ajciccone88 Жыл бұрын
@@Wokwi I'm just jumping into the world of arudino's and microcontroller. My current project was a self watering potted plant that I can interact with via telegram. It was more or less a proof of concept for going s but larger scale and automation hanging planters and perhaps a garden in my yard.
@Wokwi
@Wokwi Жыл бұрын
@@ajciccone88 Sounds like a cool project. Enjoy your journey!
@sarwarn2107
@sarwarn2107 2 жыл бұрын
So if we did 1000/microseconds we will get the frequency of the loop in Hz or as known as pulling rate? I am little bit confused about (sampling rate, frequency rate and pulling rate)? Also can this code be used with STM32? Thanks
@Wokwi
@Wokwi 2 жыл бұрын
Frequency (without "rate"), when measured in hertz units, is how many times per seconds something happens (e.g. if you clap your hands 3 times per seconds, then your clapping frequency is 3Hz. If you clap your hands once every 10 seconds, than your clapping frequency is 0.1Hz). Sampling rate is how often you are measuring (sampling) something. E.g. if you look at the watch once every two seconds, your sampling rate is 0.5 times per second. Not sure what's "pulling rate". STM32 - no, you'll have to use code specific for the STM32 timers. The principles are similar, but the code is completely different.
@henkoegema6390
@henkoegema6390 2 жыл бұрын
Well done ! 🙂
@rishitrathod9094
@rishitrathod9094 4 жыл бұрын
Superb cool ; keep making videos....
@Wokwi
@Wokwi 4 жыл бұрын
Thank you :)
@nishantkurade1581
@nishantkurade1581 4 жыл бұрын
How u make this website to run code make video on it
@Wokwi
@Wokwi 4 жыл бұрын
Hi Nishat, the website is wokwi.com You can learn how the Arduino simulator was created here: blog.wokwi.com/avr8js-simulate-arduino-in-javascript/
@Ersin_Dogan
@Ersin_Dogan 3 жыл бұрын
Really good. Thx
@jonnupe1645
@jonnupe1645 3 жыл бұрын
Any more advice on this topic?
@Wokwi
@Wokwi 3 жыл бұрын
You may want to check out this free Arduino/AVR internals course that goes deeper into these topics: kzbin.info/aero/PLLomdjsHtJTz4st9xzb5qrqWAAatEzq-g
@jonnupe1645
@jonnupe1645 3 жыл бұрын
@@Wokwi thank you
@diegoflores4587
@diegoflores4587 Жыл бұрын
I think if we are going to use thath is better use justo c to program
@NikKappa
@NikKappa 4 жыл бұрын
Very nice video
@duvanquintero
@duvanquintero 4 жыл бұрын
Thanks for share it.
@geoninja8971
@geoninja8971 3 жыл бұрын
This stuff is digital gold.
@xurshidsodiqov9323
@xurshidsodiqov9323 2 жыл бұрын
🤯👍🔥💥 useful 👌 👍 👏
@e.x.p8832
@e.x.p8832 5 ай бұрын
how you Know all this information :)
@ricardogava1604
@ricardogava1604 4 жыл бұрын
what if you use port manipulation, create an array like const byte leds [6] = { B00010000, B... the 1s is the address in the port D you want to turn on. so you call just the memory to PORTD = leds[number];
@Wokwi
@Wokwi 3 жыл бұрын
Sounds like it could work! Depending on how you use it, if you put a constant number in the array index, the compiler may even be smart enough to optimize it and just put the constant value in place...
@diamondminer81
@diamondminer81 3 жыл бұрын
Why is digitalWrite so slow?
@Wokwi
@Wokwi 3 жыл бұрын
It has some logic within to deal with PWM (for some pins) and map from Arduino pin numbers to port register and pin index. You can take a look at the source code and see everything that it does: github.com/arduino/ArduinoCore-avr/blob/24e6edd475c287cdafee0a4db2eb98927ce3cf58/cores/arduino/wiring_digital.c#L140
@saliksheraz6236
@saliksheraz6236 4 жыл бұрын
Very cool
@PiBOTSMakerHub
@PiBOTSMakerHub 4 жыл бұрын
Cool
@realchristopher4334
@realchristopher4334 3 жыл бұрын
I do not understand half of the thing mentions here because I am a true n00b. But I learn that speaking near to machine language is the best. If we can code with pure 1 and 0 then we would achieve the fastest and most accurate timing.
@Wokwi
@Wokwi 3 жыл бұрын
One step at a time :) But if you understand half, that's already a good starting point! Actually, coding with Machine Language yourself will usually not get you the fastest timing. The C++ compiler can sometimes do optimizations that a human would usually overlook. But the ability to read the machine code that the compiler generates can be useful in those cases when you have a performance bottleneck in a specific point and you want to optimize it. Here's another example of using machine language within MicroPython (an embedded version of the Python language), to optimize a specific function that happened to be a bottleneck: urish.medium.com/embedded-python-cranking-performance-knob-up-to-eleven-df31a5940a63 What project(s) are you currently working on?
@realchristopher4334
@realchristopher4334 3 жыл бұрын
@@Wokwi Urgh! I might not have the flair for programming languages. Few successful programmers say that to be successful in programming one has to sit before their computers for hours on end. I tried and I was committed (No poking phone. No computer game. No shopping. No unnecessary distraction). I was genuinely interested in getting it (whatever project I was doing) done. But...oh well. I think the problem with me is I have high tendency to go to the most efficient way of doing things. Excellent example is this one - speak to ATmega's registers rather than human-readable-commands. Having said that, I THINK (only in my bloody tiny brain cell) that I would be more successful in messing about ATmega's language. Because it looks very simple and easy. Certainly looks can be deceiving! Ah ha! I do not know yet. I do not know yet because I have not obtained the board. I am waiting for that thing to arrive, so I am first doing theoretical research. Arduino is meant for idiots, I think I would do well. I think I would do well because I probably would be using zero brain cell because the project I am interested in at present is using Arduino as cheapo oscilloscope. I was understood that oscilloscope is a low-level project, it utilizes ATmega MCU capabilities directly, without the Arduino library. I just hope that I do not fry any I/O pins nor the chip! Ruggeduino has every safety feature possible, but I am not willing to fork out that money. Twisted positive thinking, justification, and rationalisation of my own inability to pay tell me that being in danger zone may train me be more analytical and careful rather than taking life for granted. Now, that is a little unfunny humor!
@Wokwi
@Wokwi 3 жыл бұрын
It's a good tendency to explore and learn how things works under the hood. You may be interested in this free AVR internals class: kzbin.info/www/bejne/jYaxqZSHfKZ9oLc
@edgarbonet1
@edgarbonet1 Жыл бұрын
You would not want to program in machine language (the 1s and 0s). Assembly is just as fast (as it is translated word-for-word into machine language) and a lot easier to write. And as Wokwi said, C and C++ compilers at quite good at optimizing. Note that often humans can write more efficient assembly than the compiler, as they have one great advantage: they can run the compiler to see its output, then improve upon it. Yet, it is very unlikely you will ever need the performance of hand-written assembly and, if you ever need it, it will be for very specific time-critical code sections.
@danielmarcosmejiahuanca1911
@danielmarcosmejiahuanca1911 4 жыл бұрын
Subtítulos spanish
@Wokwi
@Wokwi 4 жыл бұрын
Quieres agregarlos?
Port Register Control | Increase speed of Read/Write - Arduino101
15:22
Human vs Jet Engine
00:19
MrBeast
Рет қаралды 198 МЛН
Mia Boyka х Карен Акопян | ЧТО БЫЛО ДАЛЬШЕ?
1:21:14
Что было дальше?
Рет қаралды 11 МЛН
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 1,2 МЛН
ЛУЧШИЙ ФОКУС + секрет! #shorts
00:12
Роман Magic
Рет қаралды 30 МЛН
Level Up Your Arduino Code: Registers
21:09
SparkFun Electronics
Рет қаралды 186 М.
Optimizing Arduino Code: no setup(), no loop() ⛔
9:27
Wokwi
Рет қаралды 208 М.
How to Use Arduino Interrupts The Easy Way
33:28
Rachel De Barros
Рет қаралды 89 М.
If you don’t learn sprintf(), your code will hate you later
8:34
Programming Electronics Academy
Рет қаралды 52 М.
Level Up Your Arduino Code: External Interrupts
18:55
SparkFun Electronics
Рет қаралды 174 М.
Arduino Pointers and Memory Addresses #Arduino #C #C++
8:09
Programming Electronics Academy
Рет қаралды 10 М.
Pin Change Interruptions ISR | PCINT | Arduino101
14:19
Electronoobs
Рет қаралды 58 М.
Level Up Your Arduino Code: Timer Interrupts
17:22
SparkFun Electronics
Рет қаралды 230 М.
Human vs Jet Engine
00:19
MrBeast
Рет қаралды 198 МЛН