Archive: AVS Speedups


19th August 2002 17:44 UTC

AVS Speedups
Hey AVS GMs, I was wondering what common codes speedups can be used to increase framerates? I know a couple, such as doing as many calculations as possible in the per-frame section, and not dividing by a constant (multiply by inverse instead), but I'm sure that there are more out there.

For example, which is faster?

a=if(above(abs(a),1),a-2*sign(a),a);

or

a=if(above(a,1),a-2,a);
a=if(below(a,-1),a+2,a);


19th August 2002 18:49 UTC

Instead of just caching constants, isolate constant expressions and store those:

instead of

frame:
angle=angle+0.01;

point:
foo=sin(angle)*bar;

use:
frame:
angle=angle+0.01;
sa=sin(angle);
point:
foo=sa*bar;

19th August 2002 20:28 UTC

I do not know if this is relevant as AVS has big problems with the assigning of lots of variables but as in any normal programming practice assign a variable instead of doing more calaculations.

Take steve's latest pack number 5 and look at Anemone.
In the SSC part per_pixel part

u=i*n%(n+1)-1;x1=pow(i,.8);
x3=if(equal(u,1),px1,if(equal(u,2),px2,if(equal(u,3),px3,if(equal(u,4),px4,px))))*x1;
y2=if(equal(u,1),py1,if(equal(u,2),py2,if(equal(u,3),py3,if(equal(u,4),py4,py))))*x1;
x1=if(equal(u,1),pz1,if(equal(u,2),pz2,if(equal(u,3),pz3,if(equal(u,4),pz4,pz))))*x1+1.3;
x1=if(below(x1,0.1),0,1/x1);

Now if you have enough variables then I would change thsi to the following.

u=i*n%(n+1)-1;
x1=pow(i,.8);
ue1 = equal(u,1);
ue2= equal(u,2);
ue3 = equal(u,3);
ue4 = equal(u,4);
x3=if(ue1,px1,if(ue2,px2,if(ue3,px3,if(ue4,px4,px))))*x1;
y2=if(ue1,py1,if(ue2,py2,if(ue3,py3,if(ue4,py4,py))))*x1;
x1=if(ue1,pz1,if(ue2,pz2,if(ue3,pz3,if(ue4,pz4,pz))))*x1+1.3;
x1=if(below(x1,0.1),0,1/x1);

This is not the greatest example as it only uses a simple compare but of it was equal(u%4,1) or something it would probably save a lot of time.

It would probably save a little time though with all the extra work stuff in the per_pixel the CPU has to do.

Hope it helps.


19th August 2002 21:35 UTC

Also "if" 's are normally bad news too. Use min and max instead a lot quicker.

Another of UnConeD's lines...Speeder 3d

alpha=if(above(alpha,1),1,if(below(alpha,0),0,alpha));

I think can be improved.

The if(below(alpha,0),0,alpha) can be, I think, just max(alpha,0).

and I think you can do something with the other part too. Need to think about it a bit more.

But these are just examples.


19th August 2002 22:17 UTC

There's also one way that Atero use (And work).
Here's an example line :

(This uses point-by-point assignment)

x1=(if(equal(p,1)+equal(p,4)+equal(p,5)+equal(p,6)+equal(p,9)+equal(p,10)+equal(p,15)+equal(p,16),1,0)-0.5);

Instead of using nested ifs, all the points that have the same positions (When one point need to have more than 2 lines connected to it, like in a cube) are all listed as condition, connected using +, and then the location is given.

(Note : I didn't tell Atero that I post this.. Hopefully he didn't mind :) )


19th August 2002 22:45 UTC

Originally posted by Rovastar

alpha=if(above(alpha,1),1,if(below(alpha,0),0,alpha));

I think can be improved.

The if(below(alpha,0),0,alpha) can be, I think, just max(alpha,0).

and I think you can do something with the other part too. Need to think about it a bit more.
I think the easiest way (with mins and maxes, anyway) is

alpha=min(max(alpha,0),1);


Also, in that version, since alpha is only referenced once, you could actually put the expression that determines alpha in there. So, for Speeder3K it would be


alpha=min(max(1.2-d*.25,0),1);

19th August 2002 22:54 UTC

Dang I guess I totally missed that glaring inefficiency... when you're busy trying to get the whole thing to work, details aren't exactly the first priority ;).


23rd August 2002 04:11 UTC

There's a discussion of AVS speedups in <plug style="shameless"> AVS Primer (available on DeviantART, DeskMod, and Winamp.com) </plug>


23rd August 2002 08:51 UTC

An interesting thing to note is that when using IF statements, boolean logic can be turned into algabra:

A AND B = A * B
A OR B = A + B
(A AND B) OR (C AND D) = A * B + C * D
(A OR B) AND (C OR D) = (A + B) * (C + D)
A XOR B = (A + B) - (A * B)