# SAM3X8E clock cycles for different tasks

Hi,

I was wondering if there is a way to find out how many clock cycles the sam3x8e needs to process different tasks like if-statements, incrementing a variable, doing summations, calculating fractions, etc.
If you know something please let me know!

Best regards!

Counting precisely clock cycles requires a precise granularity timer. SysTick Timer counts down from 84000 to 0 in 1 ms and then again from 84000 to 0 etc...

There is 1 tick of SysTick timer for each clock cycle and SysTick is usefull to count clock cycles for a portion of code as long as this code uses less than 84000 clock cycles.

Search example codes with SysTick->VAL in the DUE sub forum.

SysTick or DWT->CYCCNT can be used to measure pieces of code, but it would be difficult to isolate something like "an if statement" (which is going to be mostly dependent on the complexity of the expression.)
You can also look at the machine code produced with `arm-none-eabi-objdump -SC sketch.elf` and count instructions (which won't QUITE give you a cycle count because of quasi-non-deterministic things like flash memory wait states, but it's good for comparisons.)

Thanks, I'm gonna try it with this code posted by you but I have a question. In the datasheet it says that you start the SysTick by setting the ENABLE Bit in the CTRL register to one and read it with the current value register. All you used is SysTick->VAL, why do you not need to set these other Bits?

``````noInterrupts();
t0 = SysTick->VAL;
// Do some stuff during less than 1 ms, e.g. :
delayMicroseconds(5);
t1 = SysTick->VAL;
t3 = ((t0 < t1) ? 84000 + t0 : t0) - t1 - 2 ;
interrupts();
Serial.print("number of ticks: ");
Serial.println(t3  );
``````

SysTick is already turned on by default in the Due core.
Itâ€™s used to keep track of millis()

Hey @ard_newbie,

I don't really understand your calculation of t3. So I was wondering if it considers the possibility that SysTick counted down to zero and restarted at 84000?

``````t0 = SysTick->VAL;
// Do some stuff during less than 1 ms, e.g. :
delayMicroseconds(5);
t1 = SysTick->VAL;
t3 = ((t0 < t1) ? 84000 + t0 : t0) - t1 - 2 ;
interrupts();
Serial.print("number of ticks: ");
Serial.println(t3  );
``````

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.