|
|
| Author |
Message |
pmckechnie
Joined: 02 Feb 2010
Posts: 10
|
|
Posted: 30 July 2012, 16:53 PM Post subject: Code speed |
|
|
Just a quick question. Which of the following program segments run the fastest, or does it make any difference? In other words is it faster to combine calculations on one line or brake them down to smaller segments?
A=A+B+C+D+E+F
or
A=A+B
A=A+C
A=A+D
A=A+E
A=A+F
Thanks
Paul |
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2681
Location: Portland, OR
|
|
Posted: 30 July 2012, 18:12 PM Post subject: Re: Code speed |
|
|
| pmckechnie wrote: | | Which of the following program segments run the fastest, or does it make any difference? | The answer depends on the target. For a VM target (e.g. the ZX-24p) the second form results in larger and slower code. This can be confirmed by looking at the listing file. For example, I put each of the code sequences in a separate subroutine. Here is the resulting code: | Code: | Sub foo()
a = a + b + c + d
0020 1d4001 PSHA_B 0x0140 (320)
0023 1d4101 PSHA_B 0x0141 (321)
0026 35 ADD_B
0027 1d4201 PSHA_B 0x0142 (322)
002a 35 ADD_B
002b 1d4301 PSHA_B 0x0143 (323)
002e 35 ADD_B
002f 204001 POPA_B 0x0140 (320)
End Sub
0032 06 RET
Sub bar()
a = a + b
0033 1d4001 PSHA_B 0x0140 (320)
0036 1d4101 PSHA_B 0x0141 (321)
0039 35 ADD_B
003a 204001 POPA_B 0x0140 (320)
a = a + c
003d 1d4001 PSHA_B 0x0140 (320)
0040 1d4201 PSHA_B 0x0142 (322)
0043 35 ADD_B
0044 204001 POPA_B 0x0140 (320)
a = a + d
0047 1d4001 PSHA_B 0x0140 (320)
004a 1d4301 PSHA_B 0x0143 (323)
004d 35 ADD_B
004e 204001 POPA_B 0x0140 (320)
End Sub
0051 06 RET |
For native mode target devices (e.g. ZX-24n) the optimizer of the back-end compiler recognizes that the effect is the same for both cases: | Code: | void
zf_foo(void)
{
mzv_a = ((mzv_a + mzv_b) + mzv_c) + mzv_d;
882: 80 91 05 01 lds r24, 0x0105
886: 90 91 04 01 lds r25, 0x0104
88a: 89 0f add r24, r25
88c: 90 91 06 01 lds r25, 0x0106
890: 89 0f add r24, r25
892: 90 91 07 01 lds r25, 0x0107
896: 89 0f add r24, r25
898: 80 93 04 01 sts 0x0104, r24
}
89c: 08 95 ret
0000089e <zf_bar>:
void
zf_bar(void)
{
mzv_a = mzv_a + mzv_b;
mzv_a = mzv_a + mzv_c;
mzv_a = mzv_a + mzv_d;
89e: 80 91 05 01 lds r24, 0x0105
8a2: 90 91 04 01 lds r25, 0x0104
8a6: 89 0f add r24, r25
8a8: 90 91 06 01 lds r25, 0x0106
8ac: 89 0f add r24, r25
8ae: 90 91 07 01 lds r25, 0x0107
8b2: 89 0f add r24, r25
8b4: 80 93 04 01 sts 0x0104, r24
}
8b8: 08 95 ret |
|
|
| Back to top |
|
 |
pmckechnie
Joined: 02 Feb 2010
Posts: 10
|
|
Posted: 31 July 2012, 13:51 PM Post subject: |
|
|
Thanks Don,
I'm sorry I didn't mention that I am using a ZX-24A and I should have given better examples of the code in question. If you don't mind I would like to ask the question with actual code. The examples read a voltage from an oil pressure sensor. The voltage is cut in half by a voltage divider to keep the level compatible with the ZX24a ADCs. There is also an averaging routine to filter out any little glitches that may sneak in. I really haven't tried the second example yet but don't get any errors when I compile it so I think it will work
-----------------------------------------------------------------------------
Public Sub GetOil() 'Example 1
'-----------------------------------------------------------------------------
Oil1 = GetADC(Oilpin)
Oil=csng(Oil1)
Oil=Oil*2.0 'Oil read is 1/2 of actual
Oil=(5.0*oil)/1024.0
error=12.0/volt 'Corrects for changes in battery voltage
Oil=oil*error
Oil=((14.726*Oil)-25.765) 'Oil now = Oil Pressure in PSI
Average = Oil
for a=2 to 5
OilData(a-1) = Oildata(a)
Average = Average+OilData(a-1)
next a
OilData(5) = Oil
average = average / 5.0
Oil = Average
If oil<0.0 then
oil=0.0
end if
End Sub 'Returns with the Oil Pressure in Oil as single
'-----------------------------------------------------------------------------
Public Sub GetOil() 'Example 2
'-----------------------------------------------------------------------------
Oil=csng(GetADC(Oilpin))
Oil=Oil*2.0
Oil=((5.0*oil)/1024.0)*(12.0/volt)
Oil=((14.726*Oil)-25.765) 'Oil now = Oil Pressure in PSI
Average = Oil
for a=2 to 5
Average = Average+OilData(a)
OilData(a-1) = Oildata(a)
next a
OilData(5) = Oil
Oil = average / 5.0
If oil<0.0 then Oil = 0.0
End Sub
Line 1 in second example combines lines 1 and 2 of first example
Line 3 in second example combines lines 4 , 5 & 6 of first example
I know that trouble shooting would be much easier using example 1 but would example 2 gain any speed of execution?
Thank you for your help. I'm trying to learn.
Paul |
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Posts: 2681
Location: Portland, OR
|
|
Posted: 31 July 2012, 15:04 PM Post subject: |
|
|
| pmckechnie wrote: | | I know that trouble shooting would be much easier using example 1 but would example 2 gain any speed of execution? | I would guess that the second example would be slightly faster and could be made faster still (e.g. rather than multiplying by 2 in one step and then scaling 5/1024 in the next, combine these steps by scaling 10/1024 or 5/512). However, unless speed is absolutely the top priority, always favor clear, easy-to-follow code (i.e. easier maintenance) over coding for speed. Since you are using a VM model, you can always switch to a native code model if you need more speed. |
|
| Back to top |
|
 |
pmckechnie
Joined: 02 Feb 2010
Posts: 10
|
|
Posted: 01 August 2012, 12:15 PM Post subject: |
|
|
Don,
Thank you very much. I am just trying to learn. Speed it not a problem with this application, I was just trying different things for future projects I have been thinking about.
Thanks again,
Paul |
|
| Back to top |
|
 |
|