Time loss in this code of IR transmission

Discussion in 'Programmer's Corner' started by KLillie, Mar 14, 2015.

  1. KLillie

    Thread Starter Member

    May 31, 2014
    126
    14
    In this code a youtube poster said the compiler optimizes out the division time, that the digitalwrite is the slow part of the code. Thoughts? Here's the partial code.
    Code (Text):
    1.  
    2. void IRcarrier(unsigned int IRtimemicroseconds)
    3.  
    4. {
    5.  
    6. for(int i=0; i < (IRtimemicroseconds / 26); i++)
    7.  
    8. {
    9.  
    10.     digitalWrite(IRLEDpin, HIGH); //turn on the IR LED
    11.  
    12.     //NOTE: digitalWrite takes about 3.5us to execute, so we need to factor that into the timing.
    13.  
    14.      delayMicroseconds(9); //delay for 13us (9us + digitalWrite), half the carrier frequnecy
    15.  
    16.     digitalWrite(IRLEDpin, LOW); //turn off the IR LED
    17.  
    18.     delayMicroseconds(9); //delay for 13us (9us + digitalWrite), half the carrier frequnecy
    19.  
    20. }
    21.  
    22. }
    23.  
    24.  
    25.  
    The video is here.
    The full code is here.
     
    Last edited: Mar 14, 2015
  2. Papabravo

    Expert

    Feb 24, 2006
    10,179
    1,800
    The claim might be valid for some hardware platforms and for others the claim would be preposterous. The only way to be sure is to dump the compiler's assembly language output. The key here is that the value can be computed outside of the loop. If that is true and the platform has an integer compare instruction, then the claim is plausible. I'm guessing more time is wasted setting up stack frames and calling and returning from functions, but what do I know?

    Next time you include a code fragment please enclose it in
    Code (Text):
    1. ...
    brackets to preserve the indenting, making it easier to read.
    Thanks ever so much.
     
  3. KLillie

    Thread Starter Member

    May 31, 2014
    126
    14
    The code tags didn't pretty it up much so I did it manually. Looked at dumping the compiler's assembly language output. What a pain in the A%%! At least I got an answer. o_O Maybe someone else will be able to wade through this 7 lines of code.
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Optimizes out WHAT division time? The only one I see is in the for loop "IRtimemicroseconds / 26"

    Since IRtimemicroseconds is an unsigned int it is unknown at compile time and is NOT optimized out.

    It is performed (hopefully just once) the for loop is called. It may also be called each time the loop loops, that is compiler specific as it needs to retain that value.

    I doubt it does as nothing prevents IRtimemicroseconds from changing during the loop so it needs to be computed each time. Only a very expensive optimizing compiler could notice that.
     
  5. KLillie

    Thread Starter Member

    May 31, 2014
    126
    14
    Ernie, this is what I kinda figured. It is a variable and thus subject to change. This was written for an arduino. I figured it would have to be a sophisticated compiler indeed. It would have to "run" the program first, decide what this variable will change to and store these numbers in memory to be used when the program is burnt and really runs; very intuitive! Secondly, division on Atmel chips has been known to slow in comparison to other operations. Bottom line is we have no proof no matter how unlikely.
     
  6. Papabravo

    Expert

    Feb 24, 2006
    10,179
    1,800
    Where is the assembly language output. I'll wade through it and give you an answer.

    BTW - the value of IRtimemicroseconds cannot change inside the function. It never occurs as an lvalue. It is not passed to any of the subsidiary functions that are called. It is passed into the function on the stack frame, the termination value involving the division should be performed before the loop begins, and kept on the stack as a temporary variable. I don't think the optimizer will ever be concerned with this code.
     
  7. KLillie

    Thread Starter Member

    May 31, 2014
    126
    14
    Der! PapaB is correct. ("...the termination value involving the division should be performed before the loop begins, and kept on the stack as a temporary variable.") I've been away from programming for so long. I don't even know where to begin at getting the assembly output of this code. I've just been working with the Arduino IDE. The last time I messed with C I had trouble just using makefiles. Thanks all for your thoughtful responses. And Ernie I may have misunderstood your post. :confused:
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,179
    1,800
    Yeah. I don't know if you can access the assembly language code from an Arduino sketch. Supposedly they use the GNU compiler and if the shetch output file is an ELF or other compatible format then the objdump utility may be of use to you. Give it a whirl.
     
Loading...