NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Timing for each line of code

June 13, 2012
by jmuthe
jmuthe's Avatar

I understand that it takes a small amount of time (usually microseconds) for every line of code to execute. It is usually quick but if it repeated often, the time could really add up. Is there any way to know how long it takes for a line of code or a block of code to execute. I ask because I am writing code that has to be executed with perfect timing so I need to know how long each line of code takes to run.

June 14, 2012
by hevans
(NerdKits Staff)

hevans's Avatar

Hi jmuthe,

In general, it is not good coding practice to try to assume anything about how fast each line of code runs. In theory you can decompile the code and see what actual assembly instructions are being executed, and add up the time it takes to run those, but that method is very impractical and certainly would not scale beyond very small C programs.

I suggest you change up the way you are trying to architect your system and use timers combined with interrupts to execute code at certain time intervals. It might take a little bit more work, but your system will be much more reliable.

Humberto

June 14, 2012
by Ralphxyz
Ralphxyz's Avatar

I ask because I am writing code that has to be executed with perfect timing

I doubt you will ever experience anything in life that is "perfect", especially microprocessor code.

But you could learn assembler to get a idea about how long a piece of code "might" take to execute.

What are you doing that? Seems it would be interesting.

Ralph

Post a Reply

Please log in to post a reply.

Did you know that you can control multiple LEDs from one microcontroller output? Learn more...