2

我正在尝试使用 Xcode 工具来寻找方法来加快我的应用程序的速度,使其足以在旧设备上运行良好。大部分时间都花在了音频回调上,具体来说:

显示百分比的屏幕截图

void Analyzer::mergeWithOld(tones_t& tones) const {
    tones.sort();
    tones_t::iterator it = tones.begin();
    // Iterate over old tones
    for (tones_t::const_iterator oldit = m_tones.begin(); oldit != m_tones.end(); ++oldit) {
        // Try to find a matching new tone
        while (it != tones.end() && *it < *oldit) ++it;
        // If match found
        if (it != tones.end() && *it == *oldit) {
            // Merge the old tone into the new tone
            it->age = oldit->age + 1;
            it->stabledb = 0.8 * oldit->stabledb + 0.2 * it->db;
            it->freq = 0.5 * oldit->freq + 0.5 * it->freq;
        } else if (oldit->db > -80.0) {
            // Insert a decayed version of the old tone into new tones
            Tone& t = *tones.insert(it, *oldit);
            t.db -= 5.0;
            t.stabledb -= 0.1;
        }
    }
}

我感觉有点像一条狗,终于抓住了一只松鼠,然后意识到他不知道下一步该做什么。我可以加快速度吗?如果可以,我该怎么做?

编辑:当然——tones_t 是 typedef std::list<Tone> tones_t;

Tone 是一个结构:

struct Tone {
    static const std::size_t MAXHARM = 48; ///< The maximum number of harmonics tracked
    static const std::size_t MINAGE = TONE_AGE_REQUIRED; // The minimum age required for a tone to be output
    double freq; ///< Frequency (Hz)
    double db; ///< Level (dB)
    double stabledb; ///< Stable level, useful for graphics rendering
    double harmonics[MAXHARM]; ///< Harmonics' levels
    std::size_t age; ///< How many times the tone has been detected in row

  double highestFreq;
  double lowestFreq;
  int function;
  float timeStamp;

    Tone(); 
    void print() const; ///< Prints Tone to std::cout
    bool operator==(double f) const; ///< Compare for rough frequency match
    /// Less-than compare by levels (instead of frequencies like operator< does)
    static bool dbCompare(Tone const& l, Tone const& r) { 
    return l.db < r.db;
  }
};
4

1 回答 1

0

优化是一件复杂的事情。您可能需要尝试几种方法。

1:将m_tones 和tones 合并到一个新列表中,然后将该列表分配回m_tones。请务必预先设置新列表的容量。

这会将两个列表副本添加到混合中,但会消除所有插入。您必须进行测试以查看它是否更快。

2:为不同的结构转储列表。您可以将 m_tones 存储为 std::set 而不是列表吗?

当您需要以有序方式获取 m_tones 时,您将需要调用 std::sort,但如果您不需要有序迭代或仅不经常需要它,那么它可能会更快。

这些只是关于如何以不同方式思考问题的想法,您必须测试测试测试以查看哪个选项具有最佳性能。

于 2012-05-09T15:25:47.143 回答