News:

;) This forum is the property of Proton software developers

Main Menu

Easy way to calculate how many cycles (time) does the code take?

Started by trastikata, Mar 21, 2021, 05:51 PM

Previous topic - Next topic

trastikata

Hello,

I was wondering if there is an easy way to determine how many instruction cycles (or time) does a piece of code take, like a subroutine for example?

RGV250

Hi,
It was not me who wrote it but I cannot remember who, it is very old code and may need tweaking to get it to work on the new compiler.
It is written for a GLCD display but the serial port is probably easier.
Include "PROTON18_G20.INT" 'Proton 20MHz Graphics LCD
Dim TIMER1 As TMR1L.Word 'See help file
DIM X AS FLOAT
DIM Y AS FLOAT 'Dim some FLOATS
Dim pressure As Float

DelayMS 500 'Give PIC a chance to wake-up

Cls 'Clear the GLCD

X = 1234.321 'Load 1234.321 into X FLOAT
Y = 3.33 'Load 3.33 into Y FLOAT

Clear TIMER1 'Clear TIMER1
'#########################
T1CON = %00000001 'Enable the Timer

'Code to calculate
pressure = X * Y

T1CON = %00000000 'Stop the Timer
'#########################

Print At 0,0,"cycles ",Dec TIMER1 'Print out the number of Cycles
Print At 2,0,"ANSWER ",Dec pressure 'Print out Answer
End

Include "FONT.INC" 'Include the font for GLCD
'--------------------------------------------------------------------------------
'Put the code you want to calculate the number of cycles
'between Enable Timer---Stop Timer

'Remember Timer1 on the 16F877 is 16 bits
'so 65535 is the max you can count before Timer overflows.

'In the above example 1234.321 is multiplied by 3.33
'Which gives an answer of 4110.289 and took 480 Cycles...


Bob

trastikata

Quote from: RGV250 on Mar 21, 2021, 06:02 PMIt was not me who wrote it but I cannot remember who, it is very old code and may need tweaking to get it to work on the new compiler.

Thank you RGV250, but I think you misunderstood me. I was thinking if there is a way to see the cycle time a code takes, while I am writing it in the IDE.

For example if I have a limited processing time and I need to know how long does it take for each of several subroutines to execute.

RGV250

Hi,
There isn't one while writing, you could go through all the asm and work it out. The above code can be run in the VSM so all I do if I need to know is put a code snippet in the routine and run it to get the cycles. You could modify it to take the Xtal into account and show the actual time.

Bob

Stephen Moss

Quote from: trastikata on Mar 21, 2021, 06:21 PMI was thinking if there is a way to see the cycle time a code takes, while I am writing it in the IDE.
As Bob said, I don't think there is a direct way of working out how long a piece of code will take to execute, particularly from the BASIC code window, as it is will depending on how many lines of assembler are created for it.
Therefore the closest you can come to working it out is to compile the code, count the number of lines of assembler in the routine then multiply that by either...
(1 / Fosc) * X or
1 / (Fosc / X )
where X = 2 for PIC 22 & PIC 33 devices and 4 for other devices.

trastikata

Thank you both for the suggestion - using the assembler code will do the job for me. The only thing is that some instructions take more than 4 cycles.

Now comes to mind, that I can use the Microchip's instruction set and write a small Basic program to read the Assembler file line by line and look up the instructions and count the cycles. Now that every line in the assembler file is commented with the corresponding BASIC line, this will make the program easier to write.

Thanks again.