[英]Why does accessing my struct slow down my method execution
我在核心音频回调中经常调用函数,以将eq应用于音频样本。 在仪器时间配置文件中测量的这种方法的性能结果非常奇怪。
我已经完成了3次测试。 第一个调用该函数,仅返回零值。 仪器报告为1% 。
inline AudioUnitSampleType LowPassProcess (struct EQEffect *obj , AudioUnitSampleType input)
{
return 0;
}
第二个测试实际上是通过访问包含eq参数的结构在函数中进行EQ计算。
进行此操作时,仪器报告为* 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;
};
最后,我尝试再次调用该函数,但在这种情况下,不访问EQ结构,但仍执行相同数量的计算。
这样做时,仪器报告为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;
};
所以我的问题是,为什么对结构成员执行计算时,函数执行时间最多增加5倍,而仅对变量执行计算时却花费更少的时间呢?
我猜编译器只会优化您的常量计算。
无论如何,请记住,如果某个东西足够快的话,它占40%并不重要(我希望常量计算占7%会占一定比例)。 =)
因为在第一个和第三个代码示例中没有执行任何操作-第一个有意为空,并且第三个已由编译器优化,因为您没有访问任何内存且未使用计算结果(所有结果都是编译时的)常量)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.