I’m calling a function quite frequently in a core audio callback to apply eq to my audio samples. I’m having very strange performance results in this method measured in instruments time profiles.
I’ve done 3 tests. The first calls the function and just returns a zero value.
Instruments reports this as 1%.
inline AudioUnitSampleType LowPassProcess (struct EQEffect *obj , AudioUnitSampleType input)
{
return 0;
}
The second test is actually doing EQ calculations in the function by accessing a struct containing eq parameters.
When doing this instruments reports it as *40%* !
struct globaldata
{
float cutoff;
float fs;
float f;
float q;
float scale;
AudioUnitSampleType low;
AudioUnitSampleType high;
AudioUnitSampleType band;
AudioUnitSampleType notch;
};
struct globaldata global;
inline AudioUnitSampleType LowPassProcess (struct EQEffect *obj , AudioUnitSampleType input)
{
global.low= global.low + (global.f * global.band);
global.high= global.scale * input -global.low - global.q * global.band;
global.band = global.f * global.high +global.band;
global.notch = global.high + global.low;
return global.low;
};
Finally I tried calling the function again but in this case not accessing the EQ struct but still performing the same number of calculations.
When doing this instruments reports it as 7 %
struct globaldata
{
float cutoff;
float fs;
float f;
float q;
float scale;
AudioUnitSampleType low;
AudioUnitSampleType high;
AudioUnitSampleType band;
AudioUnitSampleType notch;
};
struct globaldata global;
inline AudioUnitSampleType LowPassProcess (struct EQEffect *obj , AudioUnitSampleType input)
{
float x =10+(50*8);
float y = ((10 *5) -50)- (6*40);
float z=10 *(6+9);
float j=60+0;
return 0;
};
So my question is why does my function spend up to 5 times longer to execute when I perform calculations on struct members and take far less time when I simply just perform calculations on variables?
I’m guessing the compiler just optimizes away your constant calculations.
In any case, remember that it doesn’t matter if something takes 40% if it’s still fast enough (and I would expect it to be if the constant calculations take 7%). =)