使用的内存:std :: list v.s. std :: forward_list [英] memory used: std::list v.s. std::forward_list

查看:93
本文介绍了使用的内存:std :: list v.s. std :: forward_list的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

< p>因为列表具有比forward_list多一个的指针(先前的指针),所以如果它们
都具有相同数量的元素,即1 << 30,则列表将使用几乎1/3的存储器.对吧?

然后,如果我重复调用越来越大的resize,forward_list必须能够比list大很多.

测试代码:

#include<forward_list>
#include<list>
#include<iostream>
int main(){
    using namespace std;
    typedef list<char> list_t;
    //typedef forward_list<char> list_t;
    list_t l;
    list_t::size_type i = 0;
    try{
        while(1){
            l.resize(i += (1<<20));
            cerr<<i<<" ";
        }
    }
    catch(...){
        cerr<<endl;
    }
    return 0;
}

令我惊讶的是,当进程被杀死时,它们的大小几乎相同... 有人可以解释吗?

解决方案

您应该发现,通过更好的内存嗅探,您最初的假设std::list<T>将消耗三倍的能量是正确的.在Windows计算机上,我使用

在发布版本下运行时的结果:

#define ITERATIONS 128
Bytes used by Linked List: 24576
Bytes used by Forward List: 8192
8192 * 3 = 24576

以下是 cplusplus.com 的引文,甚至说应该有引人注意的地方.两个容器之间的内存差异.

forward_list容器和列表之间的主要设计差异 容器是第一个在内部仅保留到下一个的链接 元素,而后者每个元素保留两个链接:一个指向 下一个元素和上一个元素,使高效 双向迭代,但每个消耗额外的存储空间 元素,并且插入和删除的时间开销略高 元素.因此,forward_list对象比list更有效 对象,尽管它们只能向前迭代.

使用列表中的调整大小功能,就像您在发布的代码中所做的那样,std::list<T>消耗的内存是原来的四倍,因此内存差异更加明显.

Because list have one more pointer(previous pointer) than forward_list, so if they both hold the same number of element, i.e. 1<<30, list will use almost 1/3 more memory. Right?

Then if I repeat calling resize larger and larger, forward_list must be able to resize much larger than list.

Test code:

#include<forward_list>
#include<list>
#include<iostream>
int main(){
    using namespace std;
    typedef list<char> list_t;
    //typedef forward_list<char> list_t;
    list_t l;
    list_t::size_type i = 0;
    try{
        while(1){
            l.resize(i += (1<<20));
            cerr<<i<<" ";
        }
    }
    catch(...){
        cerr<<endl;
    }
    return 0;
}

To my surprise, when the process is killed, they have almost the same size ... Anybody could interpret it?

解决方案

You should find that with better memory sniffing that your initial hypothesis that a std::list<T> will consume three times as much energy is correct. On my Windows machine, I whipped up a quick memory usage program using GetProcessMemoryInfo

Here is the core of my program:

int main()
{
    size_t initMemory = MemoryUsage();
    std::list<unsigned char> linkedList;

    for (int i = 0; i < ITERATIONS; i++)
        linkedList.push_back(i % 256);
    size_t linkedListMemoryUsage = MemoryUsage() - initMemory;

    std::forward_list<unsigned char> forwardList;
    for (int i = 0; i < ITERATIONS; i++)
        forwardList.push_front(i % 256);
    size_t forwardListMemoryUsage = MemoryUsage() - linkedListMemoryUsage - initMemory;

    std::cout << "Bytes used by Linked List: " << linkedListMemoryUsage << std::endl;
    std::cout << "Bytes used by Forward List: " << forwardListMemoryUsage << std::endl;

    return 0;
}

Results when running it under release build:

#define ITERATIONS 128
Bytes used by Linked List: 24576
Bytes used by Forward List: 8192
8192 * 3 = 24576

Here's a quote from cplusplus.com that even says that there should be noticeable memory difference between the two containers.

The main design difference between a forward_list container and a list container is that the first keeps internally only a link to the next element, while the latter keeps two links per element: one pointing to the next element and one to the preceding one, allowing efficient iteration in both directions, but consuming additional storage per element and with a slight higher time overhead inserting and removing elements. forward_list objects are thus more efficient than list objects, although they can only be iterated forwards.

Using the resize function on the lists, as you do in the posted code, the memory difference was even more pronounced with std::list<T> consuming four times as much memory.

这篇关于使用的内存:std :: list v.s. std :: forward_list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆