Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

your beloved c++ stl class vector...

93 views
Skip to first unread message

Rosario19

unread,
Oct 26, 2015, 5:51:25 AM10/26/15
to
c++ stl vector--- how to make difficult what is easy

constructor: call call call call call never end

than
vector<int> b;

where is b.len the len of vector as u32
and
b.size the size of the vector as u32
[effective mem position already allocated]

do you know that in
for(;i<b.size(); ++i)
that call the function b.size() in the loop instead of
doing something more simple as i<b.size
or better i<b.len
?



Rosario19

unread,
Oct 26, 2015, 6:55:12 AM10/26/15
to
On Mon, 26 Oct 2015 10:51:12 +0100, Rosario19 <R...@invalid.invalid>
wrote:

>c++ stl vector--- how to make difficult what is easy
>
>constructor: call call call call call never end
>
>than
>vector<int> b;
>
>where is b.len the len of vector as u32
> and
> b.size the size of the vector as u32
> [effective mem position already allocated]

?
pheraps i understand...

they are not there because someone think programmer can change these
value and what can change these value is only class vectror

fear pure fear

and even if the programmer change that number?
it is supposed that for doing that operation know all the
consegueces...

Rosario19

unread,
Oct 26, 2015, 7:00:40 AM10/26/15
to
On Mon, 26 Oct 2015 11:55:01 +0100, Rosario19 wrote:

>On Mon, 26 Oct 2015 10:51:12 +0100, Rosario19 <R...@invalid.invalid>
>wrote:
>
>>c++ stl vector--- how to make difficult what is easy
>>
>>constructor: call call call call call never end
>>
>>than
>>vector<int> b;
>>
>>where is b.len the len of vector as u32
>> and
>> b.size the size of the vector as u32
>> [effective mem position already allocated]
>
>?
>pheraps i understand...
>
>they are not there because someone think programmer can change these
>value and what can change these value is only class vectror
>
>fear pure fear

in all my classes i use [pheraps not are too many and are small]
i never make the above error in all my life
at last believe that
and all type in my classes are public
and in every part of the code i can change them
[admit rarely chage them out the classes function]

bartekltg

unread,
Oct 26, 2015, 9:25:48 AM10/26/15
to
On 26.10.2015 10:51, Rosario19 wrote:
> c++ stl vector--- how to make difficult what is easy
>
> constructor: call call call call call never end
>
> than
> vector<int> b;
>
> where is b.len the len of vector as u32
> and
> b.size the size of the vector as u32
> [effective mem position already allocated]
>
> do you know that in
> for(;i<b.size(); ++i)
> that call the function b.size() in the loop instead of

No, it does not.

Look at the output assembler code. The size is copied to a register.

And even if it would not, the size is just a integer inside
the container, {return _size;} just like you described below.

> doing something more simple as i<b.size
> or better i<b.len

This is the beauty of c++. It work just like that (or even better),
but we still have the abstraction, all containers have similar
interface. With a decent optimalization level
size(){
return _size; }
is translated do inlined 'show _size', and in loop, it is optimized
further, so _size is copied to a register to eliminate memory access.


But is ti good you talk about this, beginners often are afraid
of this and write awful code to 'be faster'.

bast
bartekltg




Bo Persson

unread,
Oct 26, 2015, 12:09:33 PM10/26/15
to
What?!

b.size() is inlined and the result is i < b.m_size



Bo Persson


Öö Tiib

unread,
Oct 27, 2015, 7:30:50 AM10/27/15
to
On Monday, 26 October 2015 13:00:40 UTC+2, Rosario19 wrote:
> On Mon, 26 Oct 2015 11:55:01 +0100, Rosario19 wrote:
>
> >On Mon, 26 Oct 2015 10:51:12 +0100, Rosario19 <R...@invalid.invalid>
> >wrote:
> >
> >>c++ stl vector--- how to make difficult what is easy
> >>
> >>constructor: call call call call call never end
> >>
> >>than
> >>vector<int> b;
> >>
> >>where is b.len the len of vector as u32
> >> and
> >> b.size the size of the vector as u32
> >> [effective mem position already allocated]
> >
> >?
> >pheraps i understand...
> >
> >they are not there because someone think programmer can change these
> >value and what can change these value is only class vectror
> >
> >fear pure fear
>
> in all my classes i use [pheraps not are too many and are small]
> i never make the above error in all my life
> at last believe that
> and all type in my classes are public
> and in every part of the code i can change them
> [admit rarely chage them out the classes function]

By my measurements the C++ standard libraries provide excellent
performance. Non-naive usage of those is quite hard to beat with
explicit C code.

All C++ standard library containers have 'size()' member
function in interface so we do not need to dig in their implementation
details every time. We can replace one container with other with
minimum changes needed and that saves time. Compiler will inline
and optimize these well. Manual optimizations are almost never needed.

Let's go to details? Implementation of 'vector::size()' may be something
like that:

return static_cast<size_type>( end_ - begin_ );

Likelihood of 'size()' being calculated instead of stored is because
majority of algorithms and range based 'for' use 'begin' and 'end'
more often than 'size' and storing all 3 is waste of space.

The 'size' of 'std::string' can be taken from some data member,
but when it uses small string optimization then it may be
conditionally stored as 7 bits and when not then in 32. Again it is
not your business, you are supposed to call 'size()'.

So it is you who are in error here. In general better do some real
benchmarks and post results (don't do these with debug build like
noob). Groundless uneducated whining is uninteresting.

woodb...@gmail.com

unread,
Oct 27, 2015, 1:36:37 PM10/27/15
to
I'm not sure if foward_list has a size member function:

http://www.cplusplus.com/reference/forward_list/forward_list/


Brian
Ebenezer Enterprises - We few, we happy few; we band of brothers.
http://webEbenezer.net

bartekltg

unread,
Oct 27, 2015, 3:25:46 PM10/27/15
to
You are right, but also you do not use [] operator on forward_list
(list doesn't have that operator:) and rather base for loop would
on iterators.


bartekltg




0 new messages