PDA

View Full Version : instruction execution time



tjg
- 17th April 2004, 16:13
hello ,
i'm on a project that really imposes me knowing the different execution times of the various picbasic pro instructions but i can't really lay hands on this information.
Does anybody know where i can find this?

Darrel Taylor
- 17th April 2004, 23:27
Hi tj,

I don't think there is a list that shows execution times for each statement, because it depends on where the code is located in program memory, what bank the variables are in, and what size variable is being used. All the bank switching and code page changing takes time too.

However, it's pretty easy to actually measure the time it takes to execute any block of statements. This saves you from having to add them all up anyways.

Since a timer set to 1:1 prescaler uses FOSC/4, each tick represents the time it takes to execute 1 instruction cycle.

So you can simply zero the timer and then turn it on just before the statements you want to time. Then turn it off immediately after those statements. The value in the timer will indicate the number of cycles used. You can then convert that to uSeconds if needed fairly easily by multiplying * 1/(OSC*1000000/4).

Here's an example of measuring the time to do a 16/16 bit divide. But you can have any number of statements inbetween, as long as the time does not exceed 65535 instructions.

T1CON = 0 ' Prescaler = 1:1, Timer off
@ MOVE?CB OSC, _PicOSC ; Get OSC value, for Time calculation

W1 var word
W2 var word
Dummy var word

W1 = 12345
W2 = 12

' ----- Measure Time for 16/16-bit variable divide -----
Gosub ClearTimer1:
@ bsf T1CON, TMR1ON ' Start timer
Dummy = W1/W2 ' The statement to measure
@ bcf T1CON, TMR1ON ' Stop timer
Hserout ["16/16 Var divide= "] : Gosub ShowTime

STOP
'----------------------------------------------------------------
PicOSC Var Byte
Cycles Var Word
Period Var Word
Time Var Word

ClearTimer1:
TMR1H = 0
TMR1L = 0
Return

ShowTime:
Cycles.lowbyte = TMR1L
Cycles.highbyte = TMR1H
Period = 1000 / PicOSC * 4 / 10 ' Time for 1 Instruction Cycle in 100ns
Time = Cycles * Period
Time = Div32 10
Hserout [ Dec Cycles, " ",Dec Time/10, ".", Dec Time//10," uS", 13,10]
Return
@ 20Mhz, the result for this is 382 instructions or 76.4uS

HTH,
   Darrel

tjg
- 21st April 2004, 12:00
Thanks very much Taylor for such an inspiration.
I tried it out and with some minor modifications, I finally obtained what i was looking for. I'm still on the project and was also wondering if this technique could be extended to a PC. Can you help me out?

Darrel Taylor
- 21st April 2004, 19:15
Not quite sure what you mean, do you want to time a program that's running on the PC, or send the results from the PIC to the PC?

DT