How Fast Does Your Arduino Code Run? ⏱

Поделиться
HTML-код
  • Опубликовано: 14 июл 2024
  • In this video you will learn how to measure the execution speed of any Arduino code very accurately just by adding a few lines of code! We'll talk about clock cycles, timers, and how to set them up to figure out exactly how fast your code is.
    By the end of this video, you will have a powerful way to tell how fast a piece of Arduino code runs. We'll also measure the accuracy of delay(), compare the speed of digitalWrite() versus direct port register manipulation, and see how choosing different pins can affect digitalWrite's speed.
    Arduino Port Registers Reference:
    www.arduino.cc/en/Reference/P...
    Dice Roller Tutorial:
    • Programming Arduino Di...
    The Arduino Uno:
    wokwi.com/arduino/libraries/d...
    Code from the video:
    wokwi.com/projects/3286758962...
    Blog post: 👇👇
    blog.wokwi.com/how-to-measure...
  • НаукаНаука

Комментарии • 57

  • @Electrologia
    @Electrologia 2 года назад +15

    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.

  • @I_am_Ashok
    @I_am_Ashok 17 дней назад

    Mind blowing video. May god keep you happy and healthy.

  • @noon1117
    @noon1117 4 года назад +6

    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 года назад +1

      @@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!

  • @talofer99
    @talofer99 4 года назад +2

    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 года назад +1

    Subscribed. Awsome content it makes my code more optimized.

  • @UltimateRobotics
    @UltimateRobotics 4 года назад +3

    Thanks! Always knew digitalWrite takes a while, but never cared enough to measure how long precisely :)

    • @Wokwi
      @Wokwi  4 года назад

      Now you know :)

  • @mrsubery
    @mrsubery 4 года назад +1

    Excellent video! Thank you

  • @nsfeliz7825
    @nsfeliz7825 4 года назад +2

    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.

  • @ExatasDistribuidas
    @ExatasDistribuidas Год назад +2

    Excelente vídeo. Obrigado pela aula

  • @autotuneengineering
    @autotuneengineering 7 месяцев назад +1

    Great video! Thanks. Subscribed.

    • @Wokwi
      @Wokwi  7 месяцев назад

      Thank you!

  • @joostpalucki6560
    @joostpalucki6560 5 месяцев назад

    useful information, thanks

  • @fersto6403
    @fersto6403 4 года назад +2

    Excellent video!

    • @Wokwi
      @Wokwi  4 года назад +1

      Thank you very much!

  • @duvanquintero
    @duvanquintero 4 года назад +1

    Thanks for share it.

  • @Ersin_Dogan
    @Ersin_Dogan 3 года назад +1

    Really good. Thx

  • @henkoegema6390
    @henkoegema6390 Год назад

    Well done ! 🙂

  • @pmwicaksana
    @pmwicaksana 3 месяца назад

    Keren om, penjelasannya sangat bagus👍

  • @NikKappa
    @NikKappa 4 года назад +1

    Very nice video

  • @rishitrathod9094
    @rishitrathod9094 4 года назад +1

    Superb cool ; keep making videos....

    • @Wokwi
      @Wokwi  4 года назад

      Thank you :)

  • @ajciccone88
    @ajciccone88 Год назад

    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!

  • @xurshidsodiqov9323
    @xurshidsodiqov9323 2 года назад

    🤯👍🔥💥 useful 👌 👍 👏

  • @thomasdalberto6762
    @thomasdalberto6762 4 месяца назад

    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.

  • @saliksheraz6236
    @saliksheraz6236 4 года назад +1

    Very cool

  • @chucklearnslithics3751
    @chucklearnslithics3751 Год назад

    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 Год назад +1

      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.

  • @diegoflores4587
    @diegoflores4587 Год назад

    I think if we are going to use thath is better use justo c to program

  • @ricardogava1604
    @ricardogava1604 3 года назад

    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 года назад +1

      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...

  • @sarwarn2107
    @sarwarn2107 Год назад +1

    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  Год назад

      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.

  • @geoninja8971
    @geoninja8971 3 года назад

    This stuff is digital gold.

  • @nishantkurade1581
    @nishantkurade1581 4 года назад +2

    How u make this website to run code make video on it

    • @Wokwi
      @Wokwi  4 года назад +1

      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/

  • @PiBOTSMakerHub
    @PiBOTSMakerHub 4 года назад +1

    Cool

  • @jonnupe1645
    @jonnupe1645 3 года назад +1

    Any more advice on this topic?

    • @Wokwi
      @Wokwi  3 года назад +1

      You may want to check out this free Arduino/AVR internals course that goes deeper into these topics: ruclips.net/p/PLLomdjsHtJTz4st9xzb5qrqWAAatEzq-g

    • @jonnupe1645
      @jonnupe1645 3 года назад

      @@Wokwi thank you

  • @e.x.p8832
    @e.x.p8832 Месяц назад

    how you Know all this information :)

  • @diamondminer81
    @diamondminer81 3 года назад +1

    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

  • @danielmarcosmejiahuanca1911
    @danielmarcosmejiahuanca1911 4 года назад +1

    Subtítulos spanish

    • @Wokwi
      @Wokwi  4 года назад

      Quieres agregarlos?

  • @realchristopher4334
    @realchristopher4334 3 года назад +1

    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 года назад +1

      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 года назад +1

      @@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: ruclips.net/video/WUNxaQGAJjU/видео.html

    • @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.