Efficiency when working with gen~ expr object
Hello all,
I have a quick question regarding best practices for working with the "expr" object in gen. Is there a "best practice" when it comes to formulas within gen? Is there any efficiency gained or lost with writing one long expression versus breaking it up into separate expressions that connect to one another?
I have made a quick and simple expression with two instances of gen~. In the example on the left I am using expr to mix between two sine waves. The sine waves and their respective mix values are all calculated with the same expr object.
In the second example the two sine waves are calculated with separate expr objects and then the result is mixed using the "mix" object.
The result is the same but is one approach more efficient?
Thank you in advance for the input,
S
make 200 copies of each and compare?
I doubt there will be any difference in performance between writing things in one line vs. writing them as a series of single operation statements. The compiler is going to re-arrange things several times as it optimizes to machine code anyway. Better to write things in a way that makes them as clear to you as possible and not worry about efficiency.
The efficiency of some code on modern desktop/laptop machines is pretty unpredictable in general, and Roman is entirely right -- the best way to know is to create a patch that has a lot of copies and test the peformance of that [why lots of copies? you need to hit enough CPU load to actually get a meaningful measurement]. Also make sure that these copies are actually being used -- e.g. summing & routing them all to an extra outlet -- otherwise the compiler might just remove them if they have no effect.
See the "performance" tab of the gen~ help patcher to see how to get more accurate CPU load measurements of a single gen~ object.
And remember the golden rule -- 1) make it work, 2) make it right, 3) only then try to make it fast
Better to write things in a way that makes them as clear to you as possible
It makes me think about what someone once told me:
We write code mainly for two reasons:
a) to give instructions to the machine
b) to communicate our idea to fellow programmers - so don't try to be fancy, try to be clear
I my personal experience, more often than not, this fellow programmer is myself some years after I wrote the code :)
Thank you for all the replies on this! I appreciate the insights. I did try to make 200 copies of the patch using mc.gen~. I’m not sure that’s the way of going about it. Results were not very conclusive. CPU percentage for both was roughly the same though it seemed to fluctuate a bit with both versions.
The point about making patches clear for myself and others makes a lot of sense. I often struggle with my own patches that I haven’t used in a while to try and figure out what is going on. Even when I comment patches it never seems to be enough.
thanks again!
S