【问题标题】:Why does accessing my struct slow down my method execution为什么访问我的结构会减慢我的方法执行速度
【发布时间】:2011-11-04 16:01:37
【问题描述】:

我在核心音频回调中非常频繁地调用一个函数以将 eq 应用于我的音频样本。在仪器时间曲线中测量的这种方法中,我得到了非常奇怪的性能结果。

我已经完成了 3 次测试。第一个调用该函数并仅返回一个零值。 Instruments 将其报告为 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 倍,而当我只对变量执行计算时,执行时间要少得多?

【问题讨论】:

  • 您的测试似乎有缺陷。在最后一种情况下,编译器将能够在编译时计算所有四个结果,因此您只需在函数调用中进行 4 次赋值。

标签: c core-audio


【解决方案1】:

我猜编译器只是优化了你的常量计算。

在任何情况下,请记住,如果某个东西仍然足够快,那么它是否占用 40% 并不重要(如果常数计算占用 7%,我希望它是这样)。 =)

【讨论】:

    【解决方案2】:

    因为在您的第一个和第三个代码示例中,没有执行 nothing - 第一个是故意为空的,第三个是由编译器优化的,因为您没有访问任何内存,也没有使用结果的计算(顺便说一下,这些都是编译时常量)。

    【讨论】:

    • 你能想到优化第二个例子吗?
    • 这一切都取决于手头的任务,但总的来说 - 尝试简化你的代数,预先计算你可以离线的所有内容,看看你是否可以使用缩放整数而不是浮点数。
    • 缩放整数在现代 Mac/PC 和最近的 iOS 设备上并不快。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 2019-01-03
    • 2014-07-02
    • 1970-01-01
    • 2021-06-15
    • 2019-05-29
    相关资源
    最近更新 更多