使用的内存:std :: list v.s. std :: forward_list [英] memory used: std::list v.s. std::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;
}
令我惊讶的是,当进程被杀死时,它们的大小几乎相同... 有人可以解释吗?
您应该发现,通过更好的内存嗅探,您最初的假设 在发布版本下运行时的结果: 以下是 cplusplus.com 的引文,甚至说应该有引人注意的地方.两个容器之间的内存差异. forward_list容器和列表之间的主要设计差异
容器是第一个在内部仅保留到下一个的链接
元素,而后者每个元素保留两个链接:一个指向
下一个元素和上一个元素,使高效
双向迭代,但每个消耗额外的存储空间
元素,并且插入和删除的时间开销略高
元素.因此,forward_list对象比list更有效
对象,尽管它们只能向前迭代. 使用列表中的调整大小功能,就像您在发布的代码中所做的那样, 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: 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 Here is the core of my program: Results when running it under release build: 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 v.s. std :: forward_list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!std::list<T>
将消耗三倍的能量是正确的.在Windows计算机上,我使用#define ITERATIONS 128
Bytes used by Linked List: 24576
Bytes used by Forward List: 8192
8192 * 3 = 24576
std::list<T>
消耗的内存是原来的四倍,因此内存差异更加明显.#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>
will consume three times as much energy is correct. On my Windows machine, I whipped up a quick memory usage program using GetProcessMemoryInfoint 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;
}
#define ITERATIONS 128
Bytes used by Linked List: 24576
Bytes used by Forward List: 8192
8192 * 3 = 24576
std::list<T>
consuming four times as much memory.