user25974: How much time does an Arduino Uno or Mega need to only call a function. And how does the time change if this function is inside a Class, for example inside a Library?
The same thing for a variable. How much time does a micro-controller need to read a declared variable?
How to determine this values? Is there a mathematic formula? Can we predetermine this type of delays and other similar micro-controller related delays?

user10794: 
How much time does an Arduino Uno or Mega need to only call a function.

Why do you want to know? This sounds like an X-Y problem.
It will vary, depending on a lot of things. 

The number of arguments, because the more arguments the more work involved. Plus if an argument involves copying it might invoke a copy constructor.
Whether or not the compiler in-lines the function. If it is in-lined (copied into the other function) then there is less work, for example the compiler doesn't need to generate call and ret instructions.
What the function does. The more things a function does, the more registers the compiler may have to push and then pop afterwards to save the state of registers which had useful information in them, outside the called function.
The size of the arguments. Longer arguments take longer to copy onto the stack, or put into registers. For example, long arguments will take longer than char arguments.



How much time does a micro-controller need to read a declared variable?

Again, it varies. It depends on the variable size, and whether or not it is already in a register. If the variable happens to be in a register at the time, you could argue it takes zero time to read it.


How to determine this values?

The sensible thing to do is to time some test code. Here's an example from another thread:
void setup ()
  {
  Serial.begin (115200);
  Serial.println ();

  unsigned long start, finish;

  start = micros ();
  for (byte i = 0; i < 100; i++)
    digitalWrite (13, HIGH);
  finish = micros ();

  Serial.print ("Time taken to do digitalWrites: ");
  Serial.println (finish - start);
  Serial.flush ();

  }  // end of setup

void loop ()
  {
  }  // end of loop

In the middle of the code there I time doing digitalWrite 100 times. By dividing the resulting output by 100, I can work out how long each digitalWrite takes.

You don't normally need to know how long, exactly, it takes to read variables, and call functions. Either your code works at an acceptable speed, or it doesn't.
Trying to answer this question on test code may give meaningless results. For example, in my test code the compiler may have in-lined the digitalWrite call. But if I use digitalWrite in many places it will no longer in-line it, which means the timing figures are now incorrect.


The Questions is how much time the millis() and micros() functions need to finish their work, so the measurement is accurate.

If that was what you wanted to know, it would have been better to ask that exact question. That saves us waffling on about copy constructors and so on.
I tested with the code below, which calls micros() and saves the results to a variable foo. I made it volatile so that the compiler wouldn't be tempted to optimize away the saving altogether (because it isn't used anywhere else).
void setup ()
  {
  pinMode (13, OUTPUT);
  PORTB = 0;
  }  // end of setup

volatile unsigned long foo;

void loop ()
  {
  PINB = bit (PB5);   // toggle D13
  foo = micros ();
  PINB = bit (PB5);   // toggle D13
  }  // end of loop

I measured 3.380 µs on my oscilloscope for the time when D13 is high. That would be 54 clock cycles. A couple (only) would be the time taken to toggle D13. Thus you can probably reckon micros() in general as taking 3.38 µs.
Changing it to call millis() instead, it only takes 1.760 µs (28 clock cycles). This is probably because all millis() does is return a variable which has already been calculated (in the timer ISR).
Test done on an Arduino Uno running at 16 MHz.

user7508: It is hard to give a general answer, as it depends on the specifics of
your code and on how the compiler compiles it:

How much time does an Arduino Uno or Mega need to only call a function.

This can involve a number of things:

the caller puts the arguments on some predefined CPU registers, unless
they are already there from a previous operation (the compiler tries
to optimize the code so that this happens whenever possible)
the caller issues the call instruction to do the actual function
call
the callee pushes some CPU registers into the stack in order to use
them without clobbering the caller's data
the callee does its job
the callee pops the previously pushed registers
the callee issues the ret (meaning return) instruction
the caller finds the return value in some CPU registers

On the Uno, the call and ret instructions cost 4 CPU cycles
each (0.25 µs). On the Mega, it's 5 cycles (0.3125 µs)
each. push and pop are 2 cycles each, and you need one such
pair for every 8-bit register you need to save.

And how does the time change if this function is inside a Class, for
  example inside a Library?

The only difference is that a class method receives an extra hidden
parameter called this. Except if it's a virtual method, which is more
expensive because it involves some pointer dereferencing.

How much time does a micro-controller need to read a declared
  variable?

“Reading” generally means transferring the contents of the variable to
some CPU registers. Local variable are usually allocated to CPU
registers in the first place, so there is no need to read them. Global
(and local static) variables are stored in RAM, and reading them takes
2 cycles per byte (e.g. 4 cycles for a 16-bit int).

How to determine this values?

Disassemble the code generated by the compiler. Then look up the
instructions in the instruction set manual: it documents the number
of clock cycles needed by each instruction.

25974: <p>How much <strong>time</strong> does an Arduino Uno or Mega need <strong>to only call a function</strong>. And how does the time change if this function is inside a Class, for example inside a Library?</p>

<p>The same thing for a variable. How much time does a micro-controller need <strong>to read a declared variable</strong>?</p>

<p>How to determine this values? Is there a mathematic formula? Can we predetermine this type of delays and other similar micro-controller related delays?</p>
 10794: <blockquote>
  <p>How much time does an Arduino Uno or Mega need to only call a function.</p>
</blockquote>

<p>Why do you want to know? This sounds like an <a href="http://xyproblem.info/" rel="nofollow">X-Y problem</a>.</p>

<p>It will vary, depending on a lot of things. </p>

<ul>
<li><p>The number of arguments, because the more arguments the more work involved. Plus if an argument involves copying it might invoke a copy constructor.</p></li>
<li><p>Whether or not the compiler in-lines the function. If it is in-lined (copied into the other function) then there is less work, for example the compiler doesn't need to generate <code>call</code> and <code>ret</code> instructions.</p></li>
<li><p>What the function does. The more things a function does, the more registers the compiler may have to <code>push</code> and then <code>pop</code> afterwards to save the state of registers which had useful information in them, outside the called function.</p></li>
<li><p>The size of the arguments. Longer arguments take longer to copy onto the stack, or put into registers. For example, <code>long</code> arguments will take longer than <code>char</code> arguments.</p></li>
</ul>

<hr>

<blockquote>
  <p>How much time does a micro-controller need to read a declared variable?</p>
</blockquote>

<p>Again, it varies. It depends on the variable size, and whether or not it is already in a register. If the variable happens to be in a register at the time, you could argue it takes zero time to read it.</p>

<hr>

<blockquote>
  <p>How to determine this values?</p>
</blockquote>

<p>The sensible thing to do is to time some test code. Here's an example from another thread:</p>

<pre class="lang-c++ prettyprint-override"><code>void setup ()
  {
  Serial.begin (115200);
  Serial.println ();

  unsigned long start, finish;

  start = micros ();
  for (byte i = 0; i &lt; 100; i++)
    digitalWrite (13, HIGH);
  finish = micros ();

  Serial.print ("Time taken to do digitalWrites: ");
  Serial.println (finish - start);
  Serial.flush ();

  }  // end of setup

void loop ()
  {
  }  // end of loop
</code></pre>

<p>In the middle of the code there I time doing digitalWrite 100 times. By dividing the resulting output by 100, I can work out how long each digitalWrite takes.</p>

<hr>

<p>You don't normally need to know how long, exactly, it takes to read variables, and call functions. Either your code works at an acceptable speed, or it doesn't.</p>

<p>Trying to answer this question on test code may give meaningless results. For example, in my test code the compiler may have in-lined the digitalWrite call. But if I use digitalWrite in many places it will no longer in-line it, which means the timing figures are now incorrect.</p>

<hr>

<blockquote>
  <p>The Questions is how much time the millis() and micros() functions need to finish their work, so the measurement is accurate.</p>
</blockquote>

<p>If that was what you wanted to know, it would have been better to ask that exact question. That saves us waffling on about copy constructors and so on.</p>

<p>I tested with the code below, which calls <code>micros()</code> and saves the results to a variable <code>foo</code>. I made it volatile so that the compiler wouldn't be tempted to optimize away the saving altogether (because it isn't used anywhere else).</p>

<pre class="lang-c++ prettyprint-override"><code>void setup ()
  {
  pinMode (13, OUTPUT);
  PORTB = 0;
  }  // end of setup

volatile unsigned long foo;

void loop ()
  {
  PINB = bit (PB5);   // toggle D13
  foo = micros ();
  PINB = bit (PB5);   // toggle D13
  }  // end of loop
</code></pre>

<p>I measured 3.380 µs on my oscilloscope for the time when D13 is high. That would be 54 clock cycles. A couple (only) would be the time taken to toggle D13. Thus you can probably reckon <code>micros()</code> in general as taking 3.38 µs.</p>

<p>Changing it to call <code>millis()</code> instead, it only takes 1.760 µs (28 clock cycles). This is probably because all <code>millis()</code> does is return a variable which has already been calculated (in the timer ISR).</p>

<p>Test done on an Arduino Uno running at 16 MHz.</p>
 7508: <p>It is hard to give a general answer, as it depends on the specifics of
your code and on how the compiler compiles it:</p>

<blockquote>
  <p>How much time does an Arduino Uno or Mega need to only call a function.</p>
</blockquote>

<p>This can involve a number of things:</p>

<ul>
<li>the caller puts the arguments on some predefined CPU registers, unless
they are already there from a previous operation (the compiler tries
to optimize the code so that this happens whenever possible)</li>
<li>the caller issues the <code>call</code> instruction to do the actual function
call</li>
<li>the callee pushes some CPU registers into the stack in order to use
them without clobbering the caller's data</li>
<li>the callee does its job</li>
<li>the callee pops the previously pushed registers</li>
<li>the callee issues the <code>ret</code> (meaning return) instruction</li>
<li>the caller finds the return value in some CPU registers</li>
</ul>

<p>On the Uno, the <code>call</code> and <code>ret</code> instructions cost 4&nbsp;CPU cycles
each (0.25&nbsp;µs). On the Mega, it's 5&nbsp;cycles (0.3125&nbsp;µs)
each. <code>push</code> and <code>pop</code> are 2&nbsp;cycles each, and you need one such
pair for every 8-bit register you need to save.</p>

<blockquote>
  <p>And how does the time change if this function is inside a Class, for
  example inside a Library?</p>
</blockquote>

<p>The only difference is that a class method receives an extra hidden
parameter called <code>this</code>. Except if it's a virtual method, which is more
expensive because it involves some pointer dereferencing.</p>

<blockquote>
  <p>How much time does a micro-controller need to read a declared
  variable?</p>
</blockquote>

<p>“Reading” generally means transferring the contents of the variable to
some CPU registers. Local variable are usually allocated to CPU
registers in the first place, so there is no need to read them. Global
(and local static) variables are stored in RAM, and reading them takes
2&nbsp;cycles per byte (e.g. 4 cycles for a 16-bit <code>int</code>).</p>

<blockquote>
  <p>How to determine this values?</p>
</blockquote>

<p>Disassemble the code generated by the compiler. Then look up the
instructions in the <a href="http://www.atmel.com/images/Atmel-0856-AVR-Instruction-Set-Manual.pdf">instruction set manual</a>: it documents the number
of clock cycles needed by each instruction.</p>
