Calculate execution time(Arduino-Ethernet shield)?

Hello friends! 1.what is the best way to calculate execution time for each function in arduino? 2.What about ethernet shield? for example calculate connection time, sending/receiving data...

Thanks

Define ‘best’

A1: create assembly listing and count the assembly instructions for a function. A2: communication over ethernet depends on too much factors to be calculated in practice. Use statistics. Welcome

AWOL :

Define ‘best’

robtillaart :

A1: create assembly listing and count the assembly instructions for a function.
A2: communication over ethernet depends on too much factors to be calculated in practice. Use statistics.
Welcome

Thank you robtillaart and AWOL for reply.
In this link there are some info…
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1275488765
but not sure is it ok or not.
Using timer ( mili before after a function )is a normal way?

i want to calculate execution time of my codes (in real time) when arduino directly connected to internet.

for example calculate execution time of specific part of ethernet sample code as you see below (reading received data …) :

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte server[] = { 64, 233, 187, 99 }; 

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);

  delay(1000);

  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /search?q=arduino HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
 if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
    for(;;)
      ;
  }
}

robtillaart , what do u mean by Using statistics?

You can also do this (putting all the declarations in the right places, etc):

unsigned long start_time = 0; unsigned lond end_time = 0;

prior to the code to be measured:

start_time = micros();

after the executed code: end_time = micros();

the difference: end_time - start_time, is your execution time in microseconds.

I did something similar today, was trying to get a loop that executed in 100 uS so I could exercise things at 0.1mS intervals, or multiples of that.

In my case I used a pc/usb oscilloscope (dpscope.com, $89 - very handy tool) to monitor an output that I toggled high/low everytime the loop started: byte toggle = 0;

if (toggle ==0){toggle = 1;) else {toggle = 0;) digitalWrite (somepin, toggle); and monitored the pin

figure there's some shortcut to do the same, basically toggle = not(toggle); but I haven't found it yet.

My loop started at nearly 600uS, I found ways to get it shorter, like not doing a 16-bit shift-out when data hadn't changed. Is currenty looking like 106uS.