Click here to Skip to main content
16,017,249 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,
I want to know how to calculate the execution time of the ANSI c code instructions;
I am using microcontroller, the crystal frequency is 20Mhz.

Thanks in advance,
z3ngew
Posted

You can take the reference on the microcontroller, retrieve the duration of every commands involved, which is usually specified in processor's "ticks". Calculate the duration of the tick from the tact frequency used. Summarize all together.

Another approach is to use real-time clock (it it is available; usually it is), create a relatively long running code by repetition of your code fragment some considerable number of times, to reduce possible measurement errors related to the fact that the contribution of the time measurement process can be estimated not quite accurately, calculate elapsed time, divide by the number of repetition. Such experimental results, if conducted accurately, usually give good accuracy.

Some validity notes:

Of course, such measurements are only accurate enough when you are using non-multithreading or even multithreading but true real-time system, the execution is not interrupted by the hardware interrupts, there is no page faults used in virtual memory; in other words, in simple systems. Even then, if you use the calculation approach, you get "pure" execution time. The really observed time can be slightly longer, because in all systems you still typically experience at least one kind of hardware interrupt: system timer.

—SA
 
Share this answer
 
v3
Comments
z3ngew 7-Aug-13 20:33pm    
Is there an average value for all instructions, for example any instructions = n machine cycle. and n is an average value for one instruction,

Thanks
z3ngew
Sergey Alexandrovich Kryukov 7-Aug-13 20:55pm    
You are welcome.

If you use some "average" value, your calculations would be incorrect. Some commands are faster, some are slower. If you have more slower commands, your correct timing will be longer then your estimate based on "average".
But why? You can do precise calculations. Yes, it need some job to be done...

—SA
Ron Beyer 7-Aug-13 22:53pm    
+5, good points, added some additional commentary as another solution below.
Sergey Alexandrovich Kryukov 7-Aug-13 23:07pm    
Thank you, Ron.
—SA
Ron Beyer 7-Aug-13 23:15pm    
I wrote it, but didn't click the stars, you have it now :)
Sergey's answer is the best, I just wanted to add a couple notes that are longer than what would be placed in a comment...

First, the crystal frequency does not really dictate processor clock cycle time, due to hardware multipliers and other factors. Processors can execute more than one clock cycle per crystal oscillation. The number of clocks per oscillation can be calculated using the hardware and software multipliers typically set up in the initialization routine.

Second, the only way to "calculate" the number of clock cycles it takes for an operation is to compile the software to byte code and analyze the output. Some linkers create assembly files that have the human readable assembly before going to ELF or S32, usually this is a linking option you have to set. You can open this file and use the processor manual to calculate the number of cycles for each operation. This still isn't 100% accurate as memory fetches take clock cycles and as Sergey said, so do the interrupts if enabled.

So as Sergey put pretty clearly above, the best way to get an accurate execution time is to use a real-time clock (although not available on a lot of processors, you can use timers though in place of an RTC to get a time instead of a wall-clock) and measure the time it takes. This is best done by averaging a number of runs of the code, for example the time it takes to do 1000 loops.

If you want to do more research, look up how to do Dhrystone and Whetstone benchmarking for processors.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Aug-13 23:11pm    
Very good points, my 5.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900