Possible to determine clock-cycles needed for code snippet ?

Is it possible to determine the total no. of clock cycles needed for a certain Arduino code snippet ? Idea is to see if it'd be prudent (or realistic) to place it in an ISR, without starving loop().

If the Arduino compiler creates intermediate assembler file with subroutine entry makers, I am guessing that this shouldn't be too hard.

Thanks, in anticipation.

If you apply "avr-objdump" with the "-D" option to your output.elf file, you can get the disassembly of your sketch. Instruction cycle counts are listed on the processor datasheet.

Would this be a reasonable (if not accurate) technique to determine length of code snippet ? For example my code snippet is a function called myISR().

step-1) place the code snippet in myISR() and compile in Arduino IDE. Note the no. of bytes reported for "Binary sketch size:", say it is X bytes. step-2) comment out the code snippet in myISR() and compile again. Note the no. of bytes reported for "Binary sketch size:", say it is Y bytes. step-3) the length of the code snipped is (X - Y) ?

I suspect that this is over simplification. Since I have some strings that I print using Serial.println(), I am guessing that those go into the static data segment, and not the entire chuck of (X-Y) are instructions, right ? Can, I then make an assumption that 90% would be instructions, and all of them a 1-byte instructions, with an avg. 2 clock/cycles per instruction ?

Of course, if there is a precise deterministic way, that doesn't involve JTAG/ICE, would be great.

Of course, if there is a precise deterministic way, that doesn't involve JTAG/ICE, would be great.

See reply #1.

Thanks @AWOL. I think I submitted that 2nd msg within a second you did. Of course, your method should be perfect. While I know where to look for avr-objdump.exe, I am not sure where I can find the sketch's elf output !

If you click on the "verify" or "upload" button, and hold down the shift key at the same time, the compiler will give you the pathname of where your sketch is. Locate the directory, and you'll see a variety of .cpp, .hex and .elf files, amongst others. Quite a useful insight into the IDE's thought processes.

Figuring out ISR overheads is a little trickier, but if you've got an oscilloscope, you can get a fairly accurate result with some simple code. Simply having a background loop resetting an output pin, and the ISR setting the same pin will give you a rough result.