Reducing CPU load from tanh~ with lookup~

geddonn's icon

Hi guys,

I'm trying to save a bit of CPU on my main performance patch.
I use tanh~ a lot on the outputs of audio modules as a kind of cheap limiter that gives a slightly nicer distortion than the using clip~.

I came across this post that says to use lookup~ instead for lighter CPU usage: https://cycling74.com/forums/valve-distortion/

I've been doing a few tests and using Max's built in 'Show CPU usage' function I have found that tanh~ seems to be much cheaper on CPU than standard MSP lookup~ or indeed Gen~ lookup~ with linear interpolation.
Here's my readout:
-tanh~ 16.46%
-lookup~ 20.59%
-gen~ 37.22%

Have I done something wrong? Should I be looking elsewhere to lower my CPU usage and leave the tanh~'s alone?

Max Patch
Copy patch and select New From Clipboard in Max.

Here's the patch:

Peter McCulloch's icon

Not sure what's happening; could be that C'74 came up with some optimization. How'd you benchmark it? (a lot of copies?)

Something that I often go after as low-hanging fruit for optimization is line~. Most usages of line~ can be down-sampled by a factor of 16 with relatively negligible consequences. (Inside a poly~, and be sure to turn resampling off)

geddonn's icon

Thanks for your reply. The benchmarking process was simply using the 'Show CPU usage' from the 'view' menu.
If you engage it, then disengage it you get a printout in the Max window of the average CPU usage and then a list of all of the MSP objects and what percentage of the average they take up.

downsampling line~ seems interesting. Would the footprint of poly~ not limit the difference though?

I'll give it a go though most of my DSP is being migrated to Gen~ so I generally smooth values with the history/mix combination now.

Cheers.

geddonn's icon

Just tried out the downsampling line~ trick and it appears I was right, the footprint of poly~ actually makes it take up more CPU.

The show CPU usage readout gave me an average of 0.8% CPU usage with poly~ taking up 27.54% of that and line taking only 5.32%.

Is this right or is the Show CPU usage function not giving me a proper readout?

Max Patch
Copy patch and select New From Clipboard in Max.

Here's the poly patch:

Max Patch
Copy patch and select New From Clipboard in Max.

Here's the master patch:

geddonn's icon

Having said that, I've just tested with four of each in a patch.

With the poly~ method, the first poly~ costs lots but then each instance of the poly~ thereafter seems to cost less.
Four instances of the poly, cycle~, live.gain~, *~ and ezdac costs 1%CPU

Also using the line~ method seems to make the live.gain~ cost more?
Four instances of line~, cycle~, live.gain~, *~ and ezdac costs roughly 1.3%CPU.

Does that sound right? Am I benchmarking this stuff wrong?

Peter McCulloch's icon

I'd say beware the micro-benchmark. Most of the benchmarking patches I've seen use a lot of copies. (50-100 at least!)

Away from machine so can't check right now, but are you using @resample 0 for poly~?

You can also use x / (1+0.28*x^2) as a replacement for tanh. (Need to clip input, though, as it goes back towards 0!)

Augustine Bannatyne - Leudar's icon

You can use the multithreading option for Poly too (see help file)