[英]Why does accessing my struct slow down my method execution
I'm calling a function quite frequently in a core audio callback to apply eq to my audio samples. 我在核心音频回调中经常调用函数,以将eq应用于音频样本。 I'm having very strange performance results in this method measured in instruments time profiles.
在仪器时间配置文件中测量的这种方法的性能结果非常奇怪。
I've done 3 tests. 我已经完成了3次测试。 The first calls the function and just returns a zero value.
第一个调用该函数,仅返回零值。 Instruments reports this as 1% .
仪器报告为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. 第二个测试实际上是通过访问包含eq参数的结构在函数中进行EQ计算。
When doing this instruments reports it as * 40 % * ! 进行此操作时,仪器报告为* 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. 最后,我尝试再次调用该函数,但在这种情况下,不访问EQ结构,但仍执行相同数量的计算。
When doing this instruments reports it as 7 % 这样做时,仪器报告为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? 所以我的问题是,为什么对结构成员执行计算时,函数执行时间最多增加5倍,而仅对变量执行计算时却花费更少的时间呢?
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%). 无论如何,请记住,如果某个东西足够快的话,它占40%并不重要(我希望常量计算占7%会占一定比例)。 =)
=)
因为在第一个和第三个代码示例中没有执行任何操作-第一个有意为空,并且第三个已由编译器优化,因为您没有访问任何内存且未使用计算结果(所有结果都是编译时的)常量)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.