28.04.2016, 18:13
(
Last edited by Crayder; 03/05/2016 at 12:11 AM.
)
In this thread I'll point out some very small optimizations that can make a huge difference in speed in the long run.
Most of these are very minor, negligible speed differences. The point is THERE ARE DIFFERENCES. There are a lot of situations where these would help majorly. Sure these cases are rare, but they do exist and I've encountered them a lot!
Bare in mind, this thread is still no where near being complete.
I bet you didn't know that "+= 1" and "var = var + 1" were both twice as fast as "++", so read on.
(Note: lines preceded by a semi-colon in the assembly are comments.)
____________________________________________
Here is a great example to start us off.
Float vectors: Arrays vs Multiple Variables.
I've seen so many people doing things like this:
Instead of doing this:
What's wrong with that? Well look here:
This compiler turns that into the following assembly output:
While for this:
The compiler does this:
I'm not going to walk you through the assembly, but I think it's obvious which one is better. It doesn't matter if you chain them or not, the arrays will always be slower. Just to prove my point here is the timing of each of these:
____________________________________________
Now for the rest of them.
To be continued...
(so please don't bother replying yet)
Most of these are very minor, negligible speed differences. The point is THERE ARE DIFFERENCES. There are a lot of situations where these would help majorly. Sure these cases are rare, but they do exist and I've encountered them a lot!
Bare in mind, this thread is still no where near being complete.
I bet you didn't know that "+= 1" and "var = var + 1" were both twice as fast as "++", so read on.
(Note: lines preceded by a semi-colon in the assembly are comments.)
____________________________________________
Here is a great example to start us off.
Float vectors: Arrays vs Multiple Variables.
I've seen so many people doing things like this:
pawn Code:
new Float:t[3];
pawn Code:
new Float:tX, Float:tY, Float:tZ
pawn Code:
tX = tY = tZ = 1;
Code:
const.pri 1 stor.s.pri fffffff4 stor.s.pri fffffff8 stor.s.pri fffffffc
pawn Code:
t[0] = t[1] = t[2] = 1;
Code:
addr.pri ffffffe8 push.pri addr.pri ffffffe8 add.c 4 push.pri addr.pri ffffffe8 add.c 8 move.alt const.pri 1 stor.i pop.alt stor.i pop.alt stor.i
Quote:
Timing "accessing and setting singles"... Mean = 136.00ns Mode = 135.00ns Median = 136.00ns Range = 4.00ns Timing "accessing and setting array"... Mean = 233.00ns Mode = 232.00ns Median = 233.00ns Range = 3.00ns |
Now for the rest of them.
- Variable definitions: Chained Definitions vs Defining Each
What people are doing:pawn Code:new tmp1;
new tmp2;
new tmp3;
new tmp4;
new tmp5;pawn Code:new tmp1, tmp2, tmp3, tmp4, tmp5;pawn Code:break
push.c 0
break
push.c 0
break
push.c 0
break
push.c 0
break
push.c 0pawn Code:push.c 0
push.c 0
push.c 0
push.c 0
push.c 0Quote:
Timing "new chain"...
Mean = 114.00ns
Mode = 114.00ns
Median = 114.00ns
Range = 2.00ns
Timing "new each"...
Mean = 221.00ns
Mode = 222.00ns
Median = 222.00ns
Range = 3.00ns
- Setting Variables: Chain Assignments vs Assigning Each
What people are doing:pawn Code:tmp1 = 17;
tmp2 = 17;
tmp3 = 17;
tmp4 = 17;
tmp5 = 17;pawn Code:tmp1 = tmp2 = tmp3 = tmp4 = tmp5 = 17;pawn Code:break
const.pri 11
stor.s.pri fffffffc
break
const.pri 11
stor.s.pri fffffff8
break
const.pri 11
stor.s.pri fffffff4
break
const.pri 11
stor.s.pri fffffff0
break
const.pri 11
stor.s.pri ffffffecpawn Code:const.pri 11
stor.s.pri ffffffec
stor.s.pri fffffff0
stor.s.pri fffffff4
stor.s.pri fffffff8
stor.s.pri fffffffcQuote:
Timing "set chain"...
Mean = 121.00ns
Mode = 118.00ns
Median = 119.00ns
Range = 18.00ns
Timing "set each"...
Mean = 219.00ns
Mode = 217.00ns
Median = 217.00ns
Range = 9.00ns
- If Lines: Chained vs Many
What people are doing:pawn Code:if(tmp1 == 1)
if(tmp2 == 1)
if(tmp3 == 1)
if(tmp4 == 1)
if(tmp5 == 1)
{}pawn Code:if(tmp1 == 1 && tmp2 == 1 && tmp3 == 1 && tmp4 == 1 && tmp5 == 1) {}pawn Code:break
load.s.pri fffffffc
eq.c.pri 1
jzer 3
break
load.s.pri fffffff8
eq.c.pri 1
jzer 4
break
load.s.pri fffffff4
eq.c.pri 1
jzer 5
break
load.s.pri fffffff0
eq.c.pri 1
jzer 6
break
load.s.pri ffffffec
eq.c.pri 1
jzer 7pawn Code:break
load.s.pri fffffffc
eq.c.pri 1
jzer 1
load.s.pri fffffff8
eq.c.pri 1
jzer 1
load.s.pri fffffff4
eq.c.pri 1
jzer 1
load.s.pri fffffff0
eq.c.pri 1
jzer 1
load.s.pri ffffffec
eq.c.pri 1
jzer 1Quote:
Timing "if chain"...
Mean = 164.00ns
Mode = 163.00ns
Median = 164.00ns
Range = 3.00ns
Timing "if lot"...
Mean = 230.00ns
Mode = 228.00ns
Median = 228.00ns
Range = 16.00ns
- Value Testing: Switch vs "If =="
What people are doing:pawn Code:if(tmp == 2 ) {}
else if(tmp == 5 ) {}
else if(tmp == 8 ) {}
else if(tmp == 11) {}
else if(tmp == 14) {}
else if(tmp == 17) {}
else if(tmp == 20) {}
else if(tmp == 23) {}
else if(tmp == 26) {}
else if(tmp == 29) {}
else {}pawn Code:switch(tmp) {
case 2 : {}
case 5 : {}
case 8 : {}
case 11: {}
case 14: {}
case 17: {}
case 20: {}
case 23: {}
case 26: {}
case 29: {}
default: {}
}pawn Code:break
load.pri 0
eq.c.pri 2
jzer 0
jump 1
l.0
break
load.pri 0
eq.c.pri 5
jzer 2
jump 3
l.2
break
load.pri 0
eq.c.pri 8
jzer 4
jump 5
l.4
break
load.pri 0
eq.c.pri b
jzer 6
jump 7
l.6
break
load.pri 0
eq.c.pri e
jzer 8
jump 9
l.8
break
load.pri 0
eq.c.pri 11
jzer a
jump b
l.a
break
load.pri 0
eq.c.pri 14
jzer c
jump d
l.c
break
load.pri 0
eq.c.pri 17
jzer e
jump f
l.e
break
load.pri 0
eq.c.pri 1a
jzer 10
jump 11
l.10
break
load.pri 0
eq.c.pri 1d
jzer 12
jump 13
l.12
l.13
l.11
l.f
l.d
l.b
l.9
l.7
l.5
l.3
l.1pawn Code:break
load.pri 0
switch 0
l.2
jump 1
l.3
jump 1
l.4
jump 1
l.5
jump 1
l.6
jump 1
l.7
jump 1
l.8
jump 1
l.9
jump 1
l.a
jump 1
l.b
jump 1
l.c
jump 1
l.0
casetbl
case a c
case 2 2
case 5 3
case 8 4
case b 5
case e 6
case 11 7
case 14 8
case 17 9
case 1a a
case 1d b
l.1Quote:
Timing "if-elseif-else"...
Mean = 408.00ns
Mode = 407.00ns
Median = 407.00ns
Range = 19.00ns
Timing "switch"...
Mean = 100.00ns
Mode = 97.00ns
Median = 99.00ns
Range = 12.00ns
To be continued...
(so please don't bother replying yet)