Code size

in case I or someone wanted/needed it to loop at a known or controllable frequency.

You don’t need to know and you don’t need to do this. If you think you need to do this you are tackling the problem incorrectly.

So, I asked a co-worker to look at Blink

There is your problem you co-worker knows nothing of this environment and is giving you rubbish advice.

Someone else then jumped in and said "make it print out a MAP file,

Two rubbish co workers.

I await yet another attempt by some patient person

This is rather like asking a car mechanic to explain the vapour pressure profile in your carburettor because one day you might want to put a gallon of gas in the tank.

It seems to me you are asking a complex question that people have done their best to explain but you are not able to understand the answer because you don’t know what is relevant to what situation.

Sorry if this sounds like a flame but I am called Grumpy

How about this: When compiling small programs, the size of the resulting binary code can be larger than you might expect, because in addition to the code that you have written, the compiler brings in additional code needed to implement any functions you envoke, plus other “support functions.” This “hidden” code is usually loaded in “clumps” (library files) and each clump may include more code than is strictly needed to implement your program. As programs get larger, these libraries are reused, and any unused portions are more likely to be used anyway, so the “overhead” you see in a small program becomes a smaller percentage of the total size of a large program.

This is in contrast to a “interpreter” like the Basic Stamp, where the equivalent of the overhead code is built permanently into the chip, and the “binary” of your program consists only of references to that already-existing code (in one form or another.)

This is also in contrast to writing software in assembler or “machine code”, where you would have to write EVERYTHING that the microcontroller would have to do, but wouldn’t have to go one instruction beyond that.

Also, the arduino environment and libraries are somewhat optimized for “clarity of expression” rather than making small programs very small. You COULD write a “blink” program using the compiler behind Arduino that was much smaller than the ~1500 bytes of the blink sketch, but it wouldn’t be understandable by most of the arduino community…

So let’s look at that output of “avr-nm -S” in reply #6 in more detail. (This is essentially the “map” output that your co-worked was talking about. It might be slightly clearer for other implementations of “map file”, but in general telling a beginner that a map file will show him where the memory went is about like telling your grandmother that you use the internet to send email…)

000002fc 00000054 T delay
00000400 00000096 T digitalWrite
0000008b 00000014 T digital_pin_to_bit_mask_PGM
00000077 00000014 T digital_pin_to_port_PGM
0000009f 00000014 T digital_pin_to_timer_PGM

This first set of functions is the code that implements the “delay” and “digitalWrite” functions. Almost 300 bytes, since Arduino does some pretty complicated things to make digitalWrite work on all the possible pins, and Delay deals wirth 32bit numbers (on an 8bit CPU) for the time values.

0000057a W exit
00000350 00000074 T init
0080010a 00000004 b intFunc

This is the code that initializes the arduino to the state that it is expected to be in when a sketch starts… About 100 bytes worth.

00800100 00000002 D ledPin
0000010a 0000002e T loop

This is the loop function from “blink.” (and the ledPin variable.) This is a much more reasonable (compared to the size of the source code) 46 bytes.

00000144 0000000e T main
000003c4 0000003c T pinMode
00000072 00000005 T port_to_input_PGM
00000068 00000005 T port_to_mode_PGM
0000006d 00000005 T port_to_output_PGM

library functions that implement the pinMode() function. (and some more stuff that goes with digitalWrite)

0080011e 00000080 B rx_buffer
0080011a 00000002 B rx_buffer_head
0080011c 00000002 B rx_buffer_tail
00000496 00000010 T serialWrite

library code for dealing with the serial port (“Serial.print()”) that shoudn’t really be in your sketch, and won’t be in v15 (probably)

00000138 0000000c T setupThe setup() function from your sketch. 12 bytes.

00800112 00000004 B timer0_clock_cycles
00800116 00000004 B timer0_millis
0080010e 00000004 B timer0_overflow_count

More of the code that implements millis() and the timer for delay()

This is my attempt answer the question “Does a bigger number of lines of code make the program run slower?”.

Let’s assume that the sketch has a number of functions defined (and implemented, of course), adding a lot of lines to the program.

There may be 2 situations here:

  1. loop() calls a number (or all) of these functions;
  2. loop does not call any of these functions;

In case 1, the program will run slower, of course.
In case 2, the program will run as fast as if the functions would not be defined at all.

In both cases, the program is as big, regardless of the the fact that the functions are called or not (at least this happens in Arduino 011 which I am currently using).

So, the compiler generates code for all the defined functions, but that does not mean that the loop() gets executed slower, unless those functions are called in the loop().

Does this make sense?

Ah, a pair of patient teachers. Thanks westfw and florinc. Now I get it.

To Grumpy_Mike: What my “rubbish coworkers” were ignorant of was the extent to which the Arduino environment is optimized to make it easy to program as opposed to using a micro efficiently.

And as inventor of the Roomba (the non-code part of course), I know that the person who wrote all the original code for Roomba did everything he could fit it on the micro and make it run as efficiently as possible to keep up with the inputs and outputs. That’s what got my thinking about this in the first place.