Does unused codes in library reduce speed or just take up space?

Does the extra unused codes not in the loop reduce overall speed? I'm only at 60kb and was wondering if it was worth it to remove the unneeded codes i wasn't using.

Depends on how you made that code “unused”

If it is commented out then the compiler ignores it. The arduino doesn’t need your comments… they are only for you to read.

If you have code that has things that are being check and that is changing outputs but you have nothing connected to those outputs then technically yes, it takes time and space on the chip.

In the end, unless you are out of memory to store your sketch or your program is not performing properly because of some additional code that isn’t being functionally used… then it doesn’t make any difference what-so-ever.

Most of the code that you don't use gets "optimised" out of your final sketch. It certainly can't slow your sketch down if you don't call it.

KenF: Most of the code that you don't use gets "optimised" out of your final sketch. It certainly can't slow your sketch down if you don't call it.

So, let's say I have a library ("database.h") that is 1 MB (basically, it's a really big one). In that library is a class that is five lines long (called, say, "User"). There are 1000 other classes outlined in that library, but I only need to call the "User" class in my Arduino sketch.

When I go to compile and upload my Arduino sketch, will it look at my sketch, see what is called from the database.h, go into that and pull the applicable classes out, and continue only pulling "User"? Or will it load (or try to load) the whole library, and then pull just the "user" class when called?

(I have no clue if that even makes sense - I don't really know how libraries work fully, but basically am trying to ask if the compiler pulls only the relevant data from a library for a sketch, or if an entire library is loaded THEN the sketch as well, to the Arduino. Sorry if this is convoluted as hell... :-[ )

Piethon: So, let's say I have a library ("database.h") that is 1 MB (basically, it's a really big one). In that library is a class that is five lines long (called, say, "User"). There are 1000 other classes outlined in that library, but I only need to call the "User" class in my Arduino sketch.

Actually, I wouldn't put much faith in this. Just as a test I just created a test library, included it from a test sketch but without calling on ANY of the resources I got a nicely bloated sketch. Seems the optimisation isn't as good as I thought :(

Piethon: Sorry if this is convoluted as hell... :-[ )

No it isn't, and I think you are right. There may be varying degrees of efficiency, but essentially, the IDE reads the library and just takes what it needs. I believe there is evidence of this when you delete the call for a redundant library and see virtually no change in the size of the compiled code.

Thanks KenF and Nick_Pyner for your kind replies! :grin:

Thanks for the tips guys.

Anything the compiler can see directly it can compile out if not used.

This builds on the notion that each .cpp is compiled separately and therefore cannot see the code in another .cpp (compilation/translation unit).

A class separated into .h,.cpp can include overhead as the sketch only sees the .h file.

The new compiler in IDE 1.5.7 can sometimes inline a .cpp if it sees no problem (only when used in context). So you may see a drop in code size between simply including a basic library and actually using it (different optimizations).

There are places where the compiler cannot remove dead code. These are where it cannot guarantee the code will never be used. You can also force this with volatile member functions/variables.

Take for example interrupts. These can be triggered from external interrupts, and are basically sources of input the AVR has no control over, also meaning ‘no guarantees’.

So adding something like the code below, will add 200 bytes RAM usage regardless of whether the timer is actually used.

ISR (TIMER1_COMPA_vect)
  {
    static int Steps[ 100 ];

    for( int i = 0 ; i < 100 ; ++i ){
      Steps[i] = random(0, 0x777f);
      GPIOR0 = Steps[i];
    }
    return; 
}

But wait! There’s more…

Long story short, the loop contains code that requires the array to be in RAM. If we remove it, the RAM usage drops by 200 bytes because the compiler can directly see the array, and can also see it is unused.

However there is still a flash overhead as the ISR code is still added in.

So basically, the more information you give the compiler at any given point may allow it to render a better result. Remember some libraries use interrupts themselves, and interrupts, ports, volatile members, .cpp files are all things which effectively hide information or state there is no guarantees of what condition its current pathway has. So its best to take the long way around and rule out any chance of wandering of course.

That's interesting - thanks Pyro!