指针向量的效率。 [英] efficiency of vector of pointers.

查看:60
本文介绍了指针向量的效率。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道为什么制作一个指针向量效率低于对象向量的价值?举个简单的例子:


int num = 20;

vector< int * v_int_ptr;

v_int_ptr.reserve(num) ;


for(int i = 0; i< num; i ++)

{

int * my_int_ptr = new int ;

* my_int_ptr = i;

v_int_ptr.push_back(my_int_ptr);

}


当num为

大时,需要很长时间并耗尽几乎所有的内存。但是,在功能上等同:


int num = 20;

vector< intv;

v.reserve(num);

for(int i = 0; i< num; i ++)

{

double my_int = i;

v.push_back(myint);

}


在与前者相同的条件下运行得非常快。顺便说一下,我想要避免使用数组。

最后,在这里解除内存的最佳方法是什么?是否需要

来迭代删除每个块的向量?


提前谢谢,

SMP

Does anyone know why making a vector of pointers is so much less efficient
than a vector of objects? For a simple example:

int num = 20;
vector<int*v_int_ptr;
v_int_ptr.reserve(num);

for(int i = 0; i < num; i++)
{
int* my_int_ptr = new int;
*my_int_ptr = i;
v_int_ptr.push_back(my_int_ptr);
}

takes a long time and uses up pretty much all the memory when num is
large. However, the functionally equivalent:

int num = 20;
vector<intv;
v.reserve(num);
for(int i = 0; i < num; i++)
{
double my_int = i;
v.push_back(myint);
}

runs very fast under the same conditions that bogged down the former. And
by the way, I''m trying to avoid using arrays.
Finally, what is the best way to deallocate the memory here? Does one need
to iterate through the vector deleting each block in turn?

Thanks in advance,
SMP

推荐答案

10月19日晚上7:26,smp< spear ... @ student.ucr.eduwrote:
On Oct 19, 7:26 pm, smp <spear...@student.ucr.eduwrote:

有谁知道为什么制作一个指针向量的效率要低得多?b $ b比一个对象向量?举个简单的例子:


int num = 20;

vector< int * v_int_ptr;

v_int_ptr.reserve(num) ;


for(int i = 0; i< num; i ++)

{

int * my_int_ptr = new int ;

* my_int_ptr = i;

v_int_ptr.push_back(my_int_ptr);

}


当num为

大时,需要很长时间并耗尽几乎所有的内存。但是,在功能上等同:


int num = 20;

vector< intv;

v.reserve(num);

for(int i = 0; i< num; i ++)

{

double my_int = i;

v.push_back(myint);

}


在与前者相同的条件下运行得非常快。顺便说一下,我想要避免使用数组。

最后,在这里解除内存的最佳方法是什么?是否需要

来迭代通过向量依次删除每个块?


提前致谢,

SMP
Does anyone know why making a vector of pointers is so much less efficient
than a vector of objects? For a simple example:

int num = 20;
vector<int*v_int_ptr;
v_int_ptr.reserve(num);

for(int i = 0; i < num; i++)
{
int* my_int_ptr = new int;
*my_int_ptr = i;
v_int_ptr.push_back(my_int_ptr);
}

takes a long time and uses up pretty much all the memory when num is
large. However, the functionally equivalent:

int num = 20;
vector<intv;
v.reserve(num);
for(int i = 0; i < num; i++)
{
double my_int = i;
v.push_back(myint);
}

runs very fast under the same conditions that bogged down the former. And
by the way, I''m trying to avoid using arrays.
Finally, what is the best way to deallocate the memory here? Does one need
to iterate through the vector deleting each block in turn?

Thanks in advance,
SMP



new()和delete()函数需要很多实时。根据你的编译器
,指针大小可能很大,因此,内存也很大。要

deallocate,请使用v.clear()。

new() and delete() functions take a lot of real time. Depending on
your compiler, pointer sizes may be big, hence, a memory hog too. To
deallocate, use v.clear().


smp写道:
smp wrote:

有没有人知道为什么制作一个指针向量比对象向量更少?b $ b?举个简单的例子:


int num = 20;

vector< int * v_int_ptr;

v_int_ptr.reserve(num) ;


for(int i = 0; i< num; i ++)

{

int * my_int_ptr = new int ;

* my_int_ptr = i;

v_int_ptr.push_back(my_int_ptr);

}


当num为

大时,需要很长时间并耗尽几乎所有的内存。但是,在功能上等同:


int num = 20;

vector< intv;

v.reserve(num);

for(int i = 0; i< num; i ++)

{

double my_int = i;

v.push_back(myint);

}


在与前者相同的条件下运行得非常快。

顺便说一句,我正在努力避免使用数组。
Does anyone know why making a vector of pointers is so much less
efficient than a vector of objects? For a simple example:

int num = 20;
vector<int*v_int_ptr;
v_int_ptr.reserve(num);

for(int i = 0; i < num; i++)
{
int* my_int_ptr = new int;
*my_int_ptr = i;
v_int_ptr.push_back(my_int_ptr);
}

takes a long time and uses up pretty much all the memory when num is
large. However, the functionally equivalent:

int num = 20;
vector<intv;
v.reserve(num);
for(int i = 0; i < num; i++)
{
double my_int = i;
v.push_back(myint);
}

runs very fast under the same conditions that bogged down the former.
And by the way, I''m trying to avoid using arrays.



那么你期待什么?第一个例子每次都调用new

循环。这不是矢量中的内容,它是如何创建的.b
。现在考虑这样一种情况:存储在向量中的对象很难复制,并将其与指向

对象的指针向量进行比较。

Well what do you expect? The first example calls new each time round
the loop. It''s not what goes in the vector, its how that object is
created. Now consider the case where the object stored in the vector is
expensive to copy and compare that with a vector of pointers to said
objects.


最后,在这里释放内存的最佳方法是什么?一个

是否需要遍历向量依次删除每个块?
Finally, what is the best way to deallocate the memory here? Does one
need to iterate through the vector deleting each block in turn?



在第一种情况下,是的,每个新的需要一个删除规则适用。

这是智能指针类型经常存储在标准

容器中的一个原因。

-

Ian Collins。

In the first case, yes, the every new requires a delete rule applies.
This is one reason smart pointer types are often stored in standard
containers.

--
Ian Collins.


Zilla写道:
Zilla wrote:

10月19日,7:26 pm,smp< spear ... @ student.ucr.eduwrote:
On Oct 19, 7:26 pm, smp <spear...@student.ucr.eduwrote:

>有没有人知道为什么制作一个指针矢量是如此少
比对象矢量有效?举个简单的例子:

int num = 20;
vector< int * v_int_ptr;
v_int_ptr.reserve(num);

for(int i = 0; i< num; i ++)
{* / int * my_int_ptr = new int;
* my_int_ptr = i;
v_int_ptr.push_back(my_int_ptr);

int num = 20;
vector< intv;
v.reserve(num);
for(int i = 0; i < num; i ++)
{my /int = i;
v.push_back(myint);
}

在相同的情况下运行得非常快困扰前者的条件。顺便说一句,我正在努力避免使用数组。
最后,在这里释放内存的最佳方法是什么?是否需要依次遍历向量来删除每个块?

提前致谢,
SMP
>Does anyone know why making a vector of pointers is so much less
efficient than a vector of objects? For a simple example:

int num = 20;
vector<int*v_int_ptr;
v_int_ptr.reserve(num);

for(int i = 0; i < num; i++)
{
int* my_int_ptr = new int;
*my_int_ptr = i;
v_int_ptr.push_back(my_int_ptr);
}

takes a long time and uses up pretty much all the memory when num is
large. However, the functionally equivalent:

int num = 20;
vector<intv;
v.reserve(num);
for(int i = 0; i < num; i++)
{
double my_int = i;
v.push_back(myint);
}

runs very fast under the same conditions that bogged down the former. And
by the way, I''m trying to avoid using arrays.
Finally, what is the best way to deallocate the memory here? Does one
need to iterate through the vector deleting each block in turn?

Thanks in advance,
SMP



new()和delete()函数需要很多实时。根据你的编译器
,指针大小可能很大,因此,内存也很大。要取消分配
,请使用v.clear()。


new() and delete() functions take a lot of real time. Depending on
your compiler, pointer sizes may be big, hence, a memory hog too. To
deallocate, use v.clear().



v.clear()将_not_释放向量的元素。它将只是
为每个存储的int *调用普通的析构函数。因此,调用

v.clear()而不通过v并在所有指针上调用delete

将大量泄漏内存。

此外,v.clear()不保证释放由向量v分配的任何内存。特别是,v.capacity()可能会也可能不会更改。

Best


Kai-Uwe Bux

v.clear() will _not_ deallocate the elements of the vector. It will just
call the trivial destructor for each int* stored. Consequently, a call to
v.clear() without going through v and calling delete on all the pointers
will leak memory big time.

Moreover, v.clear() is not guaranteed to deallocate any memory allocated by
the vector v. In particular, v.capacity() may or may not change.
Best

Kai-Uwe Bux


这篇关于指针向量的效率。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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