1

Can an Arduino Uno calculate the time that an assembly instruction will take in nanoseconds, not microseconds? I don't want to use a PC, because the calculation will be more accurate using an Arduino, as the processor won't be running other tasks.

If the Arduino can't, is there any way to improve it, or any other open source hardware that can do?

1
  • Hardware timers might help – Paul Jan 2 '16 at 15:02
2

I just want to know how much time (in nanosecond example: 14 microsecond and 145 nanosecond ) it'll take for a CMP instruction comparing 2 strings, because in theory it will take less time if the two strings doesn't match.

You can set up a hardware timer (eg. Timer 1) with a prescaler of one, so that each "tick" counts one clock cycle. Thus it can time to a resolution of 62.5 ns if running at 16 MHz.

Then you could read the timer, do your target instruction (eg. compare two strings) and then read the timer again. The difference in counts will be the time (in units of 62.5 ns).

I have a page about timers which might help you get started.


... calculate the time that an assembly instruction will take ...

Generally speaking the C++ compiler does a good job of generating code. You won't necessarily make things faster by writing in assembler and you might make it slower, because compiler-writers know various tricks which you might not.

3

How fast is an Arduino?

Arduino Uno is based on AVR MCU and run at 16 MHz. Due to the processor architecture it can execute almost 1 instruction per clock cycles (62.5 ns). It uses a simple two step pipeline which may require restart if a branch is taken. There are some instructions such as memory access that take more than one clock cycle.

Can an Arduino Uno calculate the time that an assembly instruction will take in nanoseconds, not microseconds?

It is possible to measure the execution time with a resolution of 4 us with micros(). Please describe what you want to calculate/measure.

For more details on ATmega328p please see the data-sheet, chap. 6.6 Instruction Execution Timing.

3
  • i just want to know how much time (in nanosecond example: 14 microsecond and 145 nanosecond ) it'll take for a CMP instruction comparing 2 strings, because in theory it will take less time if the two strings doesn't match. thank you – Gimy boya Jan 2 '16 at 16:20
  • Could you provide a list of some functions that you would like to know the execution time for? With the assemble listing for the functions (written in C/C++ I assume), the number of clock cycles per instruction, and the clock frequency it is possible to calculate the execution time accurately given specific parameters e.g. to explicit strings. – Mikael Patel Jan 2 '16 at 17:03
  • Here are some benchmarks that might help answer your question(s): nongnu.org/avr-libc/user-manual/benchmarks.html – Mikael Patel Jan 2 '16 at 17:04
2

If you're asking "can an AVR measure how long it works on a task" then the answer is yes, but I don't think that's what you're looking for.

If you're asking "can I craft a program that takes a specific amount of time to run" then the answer is also yes. The trick is to look in the datasheet or in the AVR Assembly Instruction manual to see how long each instruction takes in cycles and then multiply that by the MCU's clock period.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.