View Single Post
Old 07-23-2009, 02:50 AM   #10
Jeffos
Mortal
 
Jeffos's Avatar
 
Join Date: Dec 2008
Location: France
Posts: 1,969
Default

Yes, great tips here!
So here are some (more basic :-) things I have noted. HTH... Here I focus on:
Quote:
Originally Posted by liteon View Post
- other code optimization techniques. - what is faster?
How to know what is faster (i.e. how to monitor the CPU) ?
We cannot trust Reaper's performance meter (it seems it does not take some things into account, such as the @gfx thread). So, what I do: I set up a test project, I clean it as much as possible (usually one media item + the js). Then, I monitor reaper with Win's performance meter (cool for threads but I also simply use the task manager when I'm in an rush) with DISABLED JS, then I just enable it. The CPU use is the diff (note: diff done in the same conditions - just enabling the JS - as play/routing/etc.. have an impact), dif made with hidden and shown GUI.
I'd like to know how other js coders monitor CPU/RAM !?

Where to code ?
This one is really basic - I know - but it must be said! I often see that and it leads to the best optimizations => put the maximum of code in the @block part (as far as possible: typically periodic processing NOT needed for each sample), and, of course, the less in the @sample.
Then, you can even lower the processing frequency, e.g.:
Code:
@block
cpt+=samplesblock;
(cpt - lastCpt) > updatefreq ?
(
   lastCpt=cpt;
   // do some heavy stuff
);
where "updatefreq" is in samples *
[edit] *if computed: only when needed! i.e. in the @init or @slider parts.

Branching
Huge impact on CPU.
=> avoid them as far as possible!

=> use logical operator precedence
Code:
cond2 = cond1 || (floor(rand(spl0) * $pi * 1000) % 2);
cond2 = cond1 && (floor(rand(spl0) * $pi * 1000) % 2);
will be faster than:
Code:
cond2 = (floor(rand(spl0) * $pi * 1000) % 2) || cond1;
cond2 = (floor(rand(spl0) * $pi * 1000) % 2) && cond1;
here, floor() & rand() are used for test purpose only

=> some code branching tips
The 2 following code lines do the same thing (where "cond1" is a "boolean"):
Code:
//1)
spi1 = condi1 * slider1 * spl0;
//2)
condi1 ? spi1 = slider1 * spl0 : spi1 = 0;
According to some (old) Justin's post, I was thinking that 1) was the fastest one. In fact, CPU monitoring shows that that fastest one is 2) (Reaper v3.06pre7).
I really don't understand why! If a dev read this post, can he just confirm/say why (very briefly) ?

=> setting a default STATIC value according to a condition
Code:
a=0; cond1 ? a = floor(rand(spl0) * $pi * 1000);
is faster than:
Code:
cond1 ? a = floor(rand(spl0) * $pi * 1000) : a=0;
Midi processing
=> counting samples (basic thing, but I've also seen this possible optimization in a bunch of JS!). We often have to evaluate time elapsed between midi events, for this we have to count samples.
Code:
@block
cpt+=samplesblock; // +use midircv()'s 1st param for sample accurate processing
is faster than:
Code:
@sample
cpt+=1;
[Edit] even more true since v3.06!

coming back later...

Last edited by Jeffos; 07-23-2009 at 01:33 PM. Reason: english wording + better with "compilable" code !
Jeffos is offline   Reply With Quote