std::deque 内存使用

发布于 2024-11-17 13:05:19 字数 237 浏览 3 评论 0原文

我已经实现了一个简单的统计引擎,使用双端队列提供数据队列来返回滚动平均值和方差。

双端队列由多个条目构成,条目数量等于滚动值的数量。

当新值到达时,最旧的值将从前面弹出,并将新值推到后面。

我需要确保这不会在内存中增长,因为它预计会作为后台任务运行很长时间。

双端队列是否在正在使用的堆上分配? 是否有可以用来固定其大小的标志?

我在 RHEL 5.3 上使用 G++ 4.1.2

I have implemented a simple statistical engine to return rolling mean and variance using a deque to provide a data queue.

The deque is constructed with a number of entries equal to the rolling number of values.

When a new value arrives the oldest value is popped of the front and the new one pushed onto the back.

I need to be sure that this is not going to grow in memory as it is expected to run as a background task for a long time.

Does deque allocate on the heap in use?
Are there flags that I can use to fix its size?

I am using G++ 4.1.2 on RHEL 5.3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

箜明 2024-11-24 13:05:19

本质上,任何动态大小的容器都会从堆中分配内存。另一个问题提供了双端队列实现的概述

但在您的特定情况下,队列始终具有相同的大小。如果您遇到双端队列问题,使用 循环缓冲区实现简单的固定大小队列可能会有所帮助 在固定大小的数组上。此实现应该具有从根本上更好的内存行为(因为它从不需要重新分配)。如果不分析数据,很难评估其优势是否值得实施。

Essentially, any dynamically sized container allocates memory from the heap. Another question provides an overview over the implementation of the deque.

But in your particular case, the queue always has the same size. If you hit problems with the deque, it might be beneficial to implement a simple fixed-size queue using a circular buffer on a fixed-sized array. This implementation should have fundamentally better memory behaviour (since it never requires reallocation). Whether its advantage is worth the trouble of implementing is hard to assess without profiling data.

郁金香雨 2024-11-24 13:05:19

提示一下,如果您不需要跟踪这些值,可以使用这个很棒的算法,它非常轻量级(我什至在 8 位微处理器上使用它)并且非常准确。

 class RunningStat
{
public:
    RunningStat() : m_n(0) {}

    void Clear()
    {
        m_n = 0;
    }

    void Push(double x)
    {
        m_n++;

        // See Knuth TAOCP vol 2, 3rd edition, page 232
        if (m_n == 1)
        {
            m_oldM = m_newM = x;
            m_oldS = 0.0;
        }
        else
        {
            m_newM = m_oldM + (x - m_oldM)/m_n;
            m_newS = m_oldS + (x - m_oldM)*(x - m_newM);

            // set up for next iteration
            m_oldM = m_newM; 
            m_oldS = m_newS;
        }
    }

    int NumDataValues() const
    {
        return m_n;
    }

    double Mean() const
    {
        return (m_n > 0) ? m_newM : 0.0;
    }

    double Variance() const
    {
        return ( (m_n > 1) ? m_newS/(m_n - 1) : 0.0 );
    }

    double StandardDeviation() const
    {
        return sqrt( Variance() );
    }

private:
    int m_n;
    double m_oldM, m_newM, m_oldS, m_newS;
};

该算法由 BP Welford 创建,并在 Donald Knuth 的《计算机编程艺术》第 2 卷第 232 页第 3 版中介绍。

http://www.johndcook.com/standard_deviation.html

Just as a tip, if you don't need to keep track of the values there is this great algorithm that is very lightweight (I even use it on 8bit micros) and is accurate.

 class RunningStat
{
public:
    RunningStat() : m_n(0) {}

    void Clear()
    {
        m_n = 0;
    }

    void Push(double x)
    {
        m_n++;

        // See Knuth TAOCP vol 2, 3rd edition, page 232
        if (m_n == 1)
        {
            m_oldM = m_newM = x;
            m_oldS = 0.0;
        }
        else
        {
            m_newM = m_oldM + (x - m_oldM)/m_n;
            m_newS = m_oldS + (x - m_oldM)*(x - m_newM);

            // set up for next iteration
            m_oldM = m_newM; 
            m_oldS = m_newS;
        }
    }

    int NumDataValues() const
    {
        return m_n;
    }

    double Mean() const
    {
        return (m_n > 0) ? m_newM : 0.0;
    }

    double Variance() const
    {
        return ( (m_n > 1) ? m_newS/(m_n - 1) : 0.0 );
    }

    double StandardDeviation() const
    {
        return sqrt( Variance() );
    }

private:
    int m_n;
    double m_oldM, m_newM, m_oldS, m_newS;
};

This algorithm was created by B. P. Welford and is presented in Donald Knuth's Art of Computer Programming, Vol 2, page 232, 3rd edition.

http://www.johndcook.com/standard_deviation.html

不语却知心 2024-11-24 13:05:19

该规范将实施细节留给供应商。然而,由于两端的插入效率很高,因此它很可能在堆上实现为链接结构。话虽如此,当您从堆中弹出某些内容时,它应该被解构,因此您的总内存使用量不应增加。

The specification leaves implementation details to the vendor. However, since insertion at both ends is efficient, it is most likely implemented as a linked structure on the heap. That being said, when you pop something off the heap, it should get deconstructed, so your total memory usage shouldn't climb.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文