Computing the Time it takes for indivdula instructions to excute?

Hello,

I was wondering if anyone knew how you can compute the exact or approx. time of how long it takes the arduino to execute certain statements like decisions, assignments ect.
Thank you

You need to look at the compiled code:

if (x > 6)

is probably pretty quick (a couple of hundred nanoseconds), whereas if (strcmp (a, b) == 0) obviously isn't.

A good place is the processor data sheet from the Atmel site.

Or you could use the processor to time, say, 100000 operations of the type you're interested, but you need to be careful of compiler optimisations.

Or go a little cruder, capture micros() before & after the event, take the difference.
There are 16 clock cycles per microsecond, so this may not yield very good results for the shorter code chunks.

  1. count cycles

  2. run code in avr studio simulator and use the stopwatch

  3. http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1175115259

For individual AVR instructions see the Atmel reference document:
www.atmel.com/atmel/acrobat/doc0856.pdf
This document details each AVR instruction and its cycles.

For system type timings, I use a technique called hardware profiling quite often.
(I have used this technique for more than 25 years)
To do this you need external equipment such as an oscilloscope or better a logic analyzer.
Then, to get the timing you wiggle a pin hooked up to your external equipment
just before and just after the piece of code you
want to time. It is a very simple and a very accurate way to get timings
of the code.
You can use this to time large sections of code to help home in on sections of code
you want to consider for optimizing.

Because it has such low overhead, you can usually use it to profile code in a live running system
While software simulators and cycle counters are pretty good these days, nothing
beats being able to time the real code running full speed live in a system.

This technique is also good for certain types of debugging as you can use
multiple pins to indicate different locations in your code. Then not only can you
see timing but you can see the sequence and timing between different sections
of code.

Small USB based logic analyzers like the Saleae Logic:
are perfect for this type of task: http://www.saleae.com/home/

--- bill

Then, to get the timing you wiggle a pin hooked up to your external equipment
just before and just after the piece of code you
want to time.

I use these two macros

// some debugging aids used to trigger a logic analyser
#define PULSE0	asm("sbi 5,0\ncbi 5,0");  // PORTB  bit 0, 328 Arduino D8
#define PULSE1	asm("sbi 5,1\ncbi 5,1");  // PORTB  bit 1, 328 Arduino D9

Or variations. Used like this

PULSE0;
somecode();
PULSE0;

Almost no impact on the running code.


Rob