Time execution in a loop

Thread Starter

sidd342

Joined Oct 9, 2023
32
I'm using a PIC based MCU. In the main function, I'm using a while loop and doing multiple if else analysis.
I want to calculate the time taken to run the loop each time for some further analysis.
Is it possible to have a timer within the code to calculate this?
 

Ian0

Joined Aug 7, 2020
13,097
Yes. I think the PIC has a simple free-running timer.
Set it to zero at the beginning. Read it at the end. Simple as that.
 

Jerry-Hat-Trick

Joined Aug 31, 2022
778
Or put your loop within another loop within another loop and run a large number of cycles and literally time it with a stop watch. I got a fairly accurate result for how long analogRead() takes in a Arduino compared to a Seeeduino XIAO by putting that one instruction in a loop within a loop, turning an LED on at the beginning and off at the end. Comment out the code within the two loops to subtract out the time they take with no operation (in your case the loop you want to time) between.
 
Top