Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   is vec.reserve(unsigned int) better than vec(unsigned int)? (http://www.velocityreviews.com/forums/t537094-is-vec-reserve-unsigned-int-better-than-vec-unsigned-int.html)

 er 09-13-2007 09:50 PM

is vec.reserve(unsigned int) better than vec(unsigned int)?

hi,

of A and B below i have a feeling A is more efficient although i'm
used to doing A unless i can't... could someone kindly confirm/refute?

//A
vector<T> vec;
vec.reserve(n);//only allocates mem.
transform(...,back_inserter(vec));
//B
vector<T> vec(n);//uses T's default constructor to fill the vector
transform(...,vec.begin());

 Victor Bazarov 09-13-2007 10:00 PM

Re: is vec.reserve(unsigned int) better than vec(unsigned int)?

er wrote:
> of A and B below i have a feeling A is more efficient although i'm
> used to doing A unless i can't... could someone kindly confirm/refute?
>
> //A
> vector<T> vec;
> vec.reserve(n);//only allocates mem.
> transform(...,back_inserter(vec));
> //B
> vector<T> vec(n);//uses T's default constructor to fill the vector
> transform(...,vec.begin());

It's impossible to tell. Measure, don't "have a feeling".

V
--

 digz 09-13-2007 11:24 PM

Re: is vec.reserve(unsigned int) better than vec(unsigned int)?

On Sep 13, 6:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> er wrote:
> > of A and B below i have a feeling A is more efficient although i'm
> > used to doing A unless i can't... could someone kindly confirm/refute?

>
> > //A
> > vector<T> vec;
> > vec.reserve(n);//only allocates mem.
> > transform(...,back_inserter(vec));
> > //B
> > vector<T> vec(n);//uses T's default constructor to fill the vector
> > transform(...,vec.begin());

>
> It's impossible to tell. Measure, don't "have a feeling".
>
> V
> --

with reserve you are guaranteed atleast n , with vec(n) u just hv n
places and nothing more

 Andre Kostur 09-13-2007 11:59 PM

Re: is vec.reserve(unsigned int) better than vec(unsigned int)?

digz <Digvijoy.C@gmail.com> wrote in news:1189725878.992881.254880

> On Sep 13, 6:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>> er wrote:
>> > of A and B below i have a feeling A is more efficient although i'm
>> > used to doing A unless i can't... could someone kindly confirm/refute?

>>
>> > //A
>> > vector<T> vec;
>> > vec.reserve(n);//only allocates mem.
>> > transform(...,back_inserter(vec));
>> > //B
>> > vector<T> vec(n);//uses T's default constructor to fill the vector
>> > transform(...,vec.begin());

>>
>> It's impossible to tell. Measure, don't "have a feeling".

>
> with reserve you are guaranteed atleast n , with vec(n) u just hv n
> places and nothing more

Really? Where in the Standard does it specify that after vec(n) that
vec.capacity() == n ? (And get your keyboard replaced... it is
apparently dropping letters.)

 er 09-14-2007 12:03 AM

Re: is vec.reserve(unsigned int) better than vec(unsigned int)?

On Sep 13, 7:24 pm, digz <Digvijo...@gmail.com> wrote:
> On Sep 13, 6:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
>
>
>
> > er wrote:
> > > of A and B below i have a feeling A is more efficient although i'm
> > > used to doing A unless i can't... could someone kindly confirm/refute?

>
> > > //A
> > > vector<T> vec;
> > > vec.reserve(n);//only allocates mem.
> > > transform(...,back_inserter(vec));
> > > //B
> > > vector<T> vec(n);//uses T's default constructor to fill the vector
> > > transform(...,vec.begin());

>
> > It's impossible to tell. Measure, don't "have a feeling".

>
> > V
> > --
> > I do not respond to top-posted replies, please don't ask

>
> with reserve you are guaranteed atleast n , with vec(n) u just hv n
> places and nothing more

thanks.

 digz 09-14-2007 05:23 AM

Re: is vec.reserve(unsigned int) better than vec(unsigned int)?

On Sep 13, 7:59 pm, Andre Kostur <nntps...@kostur.net> wrote:
> digz <Digvijo...@gmail.com> wrote in news:1189725878.992881.254880
>
>
>
> > On Sep 13, 6:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
> >> er wrote:
> >> > of A and B below i have a feeling A is more efficient although i'm
> >> > used to doing A unless i can't... could someone kindly confirm/refute?

>
> >> > //A
> >> > vector<T> vec;
> >> > vec.reserve(n);//only allocates mem.
> >> > transform(...,back_inserter(vec));
> >> > //B
> >> > vector<T> vec(n);//uses T's default constructor to fill the vector
> >> > transform(...,vec.begin());

>
> >> It's impossible to tell. Measure, don't "have a feeling".

>
> > with reserve you are guaranteed atleast n , with vec(n) u just hv n
> > places and nothing more

>
> Really? Where in the Standard does it specify that after vec(n) that
> vec.capacity() == n ? (And get your keyboard replaced... it is
> apparently dropping letters.)

Andre you made me rethink ( this is from g++-3.4.4 source)
but I still feel the same about vector v(n)..may be g++ is not
standard compliant ?
Correct me if I am wrong

particularly this line :
this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;

-----
explicit
vector(size_type __n)
: _Base(__n, allocator_type())
{ this->_M_impl._M_finish = std::uninitialized_fill_n(this-
>_M_impl._M_start,

__n, value_type()); }
_Vector_base(size_t __n, const allocator_type& __a)
: _M_impl(__a)
{
this->_M_impl._M_start = this->_M_allocate(__n);
this->_M_impl._M_finish = this->_M_impl._M_start;
this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
}

 Andre Kostur 09-14-2007 06:20 AM

Re: is vec.reserve(unsigned int) better than vec(unsigned int)?

digz <Digvijoy.C@gmail.com> wrote in

> On Sep 13, 7:59 pm, Andre Kostur <nntps...@kostur.net> wrote:
>> digz <Digvijo...@gmail.com> wrote in news:1189725878.992881.254880
>>
>>
>>
>> > On Sep 13, 6:00 pm, "Victor Bazarov" <v.Abaza...@comAcast.net>
>> > wrote:
>> >> er wrote:
>> >> > of A and B below i have a feeling A is more efficient although
>> >> > i'm used to doing A unless i can't... could someone kindly
>> >> > confirm/refute?

>>
>> >> > //A
>> >> > vector<T> vec;
>> >> > vec.reserve(n);//only allocates mem.
>> >> > transform(...,back_inserter(vec));
>> >> > //B
>> >> > vector<T> vec(n);//uses T's default constructor to fill the
>> >> > vector transform(...,vec.begin());

>>
>> >> It's impossible to tell. Measure, don't "have a feeling".

>>
>> > with reserve you are guaranteed atleast n , with vec(n) u just hv n
>> > places and nothing more

>>
>> Really? Where in the Standard does it specify that after vec(n) that
>> vec.capacity() == n ? (And get your keyboard replaced... it is
>> apparently dropping letters.)

>
> Andre you made me rethink ( this is from g++-3.4.4 source)
> but I still feel the same about vector v(n)..may be g++ is not
> standard compliant ?
> Correct me if I am wrong
>
> particularly this line :
> this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
>
> -----
> explicit
> vector(size_type __n)
> : _Base(__n, allocator_type())
> { this->_M_impl._M_finish = std::uninitialized_fill_n(this-
>>_M_impl._M_start,

> __n, value_type()); }
> _Vector_base(size_t __n, const allocator_type& __a)
> : _M_impl(__a)
> {
> this->_M_impl._M_start = this->_M_allocate(__n);
> this->_M_impl._M_finish = this->_M_impl._M_start;
> this->_M_impl._M_end_of_storage = this->_M_impl._M_start + __n;
> }
>
>

This doesn't invalidate the question. AFAIK the standard doesn't say
anything about the capacity() of vector after "vector v(n);" Which
basically leaves it a capacity() >= size(). capacity() == size() is a
valid implementation, but it's not a requirement.

 All times are GMT. The time now is 06:31 PM.