Pages: [1]   Go Down
Author Topic: how to measure the number of clock cycles from execution time of a code  (Read 394 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

   
I am using micros() function to get the execution time of my code on Mega 2560. But can anyone tell me how to get the number of clock cycles required for executing it.
Logged

Norfolk UK
Offline Offline
Edison Member
*
Karma: 52
Posts: 2214
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have maybe posted this question in the wrong forum section?
What if you setup several global unsigned longs (maybe t0 to t9) and then call tx = micros() function tens times in a row (not using loop) and then compare the average difference between each call. This will give you something like the execution time though interrupts will effect the results. Run it enough times and you should get an idea of the time it will take.
The long winded way would be to disassemble the code and work out how many machine code instructions are needed but you will still have interrupt overheads to take into account.
Logged

Handle every stressful situation like a dog. If you can't eat it or hump it. Piss on it and walk away.

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12483
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino.h has these 3 macro's to help you

Code:
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: