vector and for-loop

K

Kitty

Given the following code:

vector<Obj> v;
.....
for(i=0;i<v.size();i++)
v.some_data=0;

Is it possible to further simplify and improve the speed of the above code?
Thanks.
 
M

Mike Wahler

Kitty said:
It should be:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;



Kitty said:
Given the following code:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;

Is it possible to further simplify and improve the speed of the above
code? Thanks.


The below is imo 'simpler', but may or may not be 'faster',
the only way to know is to time it.

#include <vector>

class Obj
{
int some_data;
public:
Obj() : some_data(0)
{
}
};

int main()
{
const std::vector<Obj>::size_type sz(100); /* arbitrary size */
std::vector<Obj> v(sz); /* all elements are default-initialized */
return 0
}

-Mike
 
A

Andrew Koenig

Kitty said:
Given the following code:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;

Is it possible to further simplify and improve the speed of the above
code?

The first question is why you want to initialize a bunch of vector elements
in the first place.

Why aren't you waiting until you actually need the elements and creating
them then? That way, you don't have to construct elements that you don't
need.

The second question is how much time this code takes in its current form?
Hours? Minutes? Seconds? Milliseconds? Without at least an estimate, you
(and we) have no clue as to whether it's worth any effort at all to make it
faster.
 
D

Duane

The first question is why you want to initialize a bunch of vector elements
in the first place.

Why aren't you waiting until you actually need the elements and creating
them then? That way, you don't have to construct elements that you don't
need.

It looks like he wants to set a member of the object held in the vector
to 0, not the vector element itself. Who knows, maybe this is some
sort of counter etc. Having the object set the member to 0 at initialization
should do that or maybe I'm missing something...
 
K

Kitty

I have to call this code many times. So faster method is required.



Kitty said:
It should be:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;



Kitty said:
Given the following code:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;

Is it possible to further simplify and improve the speed of the above
code? Thanks.
 
T

Thomas Matthews

Kitty said:
It should be:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;



Kitty said:
Given the following code:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;

Is it possible to further simplify and improve the speed of the above
code? Thanks.


vector<Obj> v;
....
quantity = v.size();
for (i = 0; i < quantity; ++i)
v.some_data = 0;

Some compilers may figure out that there is no need to keep calling
the function v.size(), so the above may not save much.

As others have stated, why are these fields being initialized to
zero so often? Removing this requirement will speed up the code
and reduce the code size.


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
D

Dietmar Kuehl

Kitty said:
vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;


Unless the "..." indicates something which fills the vector with
objects, the above loop, of course, does nothing. Assuming that
the "..." actually fill the 'std::vector<...>', why doesn't it
immediately set up appropriate values for the member 'some_data'?
Have you measured that this loop is actually a bottleneck?

That said, note that use of 'std::fill()' does not directly work
because you just want to set one member while 'std::fill()'
assumes that you set the complete object. An alternative would
be use of a property map based implementation of the standard
algorithms or an appropiate iterator wrapper. The algorithm may
take advantage of various optimizations like loop-unrolling a la
Duff's Device:

| std::vector<Obj>::iterator beg = v.begin(), it = v.end();
| switch (v.size() % 4)
| do
| {
| (--it)->some_data = 0;
| case 3: (--it)->some_data = 0;
| case 2: (--it)->some_data = 0;
| case 1: (--it)->some_data = 0;
| }
| while (beg != it);
 
C

chris

Dietmar said:
Kitty said:
vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;



Unless the "..." indicates something which fills the vector with
objects, the above loop, of course, does nothing. Assuming that
the "..." actually fill the 'std::vector<...>', why doesn't it
immediately set up appropriate values for the member 'some_data'?
Have you measured that this loop is actually a bottleneck?

That said, note that use of 'std::fill()' does not directly work
because you just want to set one member while 'std::fill()'
assumes that you set the complete object. An alternative would
be use of a property map based implementation of the standard
algorithms or an appropiate iterator wrapper. The algorithm may
take advantage of various optimizations like loop-unrolling a la
Duff's Device:

| std::vector<Obj>::iterator beg = v.begin(), it = v.end();
| switch (v.size() % 4)
| do
| {
| (--it)->some_data = 0;
| case 3: (--it)->some_data = 0;
| case 2: (--it)->some_data = 0;
| case 1: (--it)->some_data = 0;
| }
| while (beg != it);


Just one little thing, because I see "Duff's device" and similar things
so often.

It's much better to write it as:

std::vector<Obj>::iterator beg = v.begin(), it = v.end();
switch (v.size() % 4)
{
case 3: (--it)->some_data = 0;
case 2: (--it)->some_data = 0;
case 1: (--it)->some_data = 0;
}

while(beg != it)
{
(--it)->some_data = 0;
(--it)->some_data = 0;
(--it)->some_data = 0;
(--it)->some_data = 0;
}

(warning: I haven't carefully checked that this is right.. but the basic
outline is correct). Many compilers will have difficulty (and might not
bother trying) optimising the original version, as it has no idea which
line the case statement will jump to. Writing this way allows the
compiler to fully optimise the main unrolled part (in this case it might
not matter so much. In some cases it can make a big difference).

Chris
 
A

Andrew Koenig

Kitty said:
I have to call this code many times. So faster method is required.

Why? Does this code really represent a significant part of the execution
time of your entire program? If so, it suggests that you aren't doing much
with these array elements other than zeroing them. In that case, you could
probably do better still by not creating them in the first place.
 
P

Patrick Kowalzick

vector said:
...
quantity = v.size();
for (i = 0; i < quantity; ++i)
v.some_data = 0;


Personally I like more the not-polluting version:

for (size_t i = 0, quantity = v.size(); i < quantity; ++i)
v.some_data = 0;

but last time I profiled it comparing against

for (unsigned int i = 0; i < v.size(); ++i)
v.some_data = 0;

there was no speed difference when gcc optimization level 3 was used, so I
normally take the latter one. But as I use as well C# I think about changing
my habits for the first one.

Patrick
 
A

alexmdac

void ZeroSomeData( Obj &o )
{ o.some_data = 0; }
// ...

std::for_each( v.begin(), v.end(), ZeroSomeData );
 
M

msalters

Kitty said:
It should be:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;



"Kitty" <No spam> ¼¶¼g©ó¶l¥ó·s»D:[email protected]...
Given the following code:

vector<Obj> v;
....
for(i=0;i<v.size();i++)
v.some_data=0;


This may be faster (use a profiler)

std::vector<Obj>::iterator i, end = v.end();
for ( i = v.begin(); i!= end; ++i )
i->somedata = 0;
 
R

Richard Herring

In message said:
void ZeroSomeData( Obj &o )
{ o.some_data = 0; }
// ...

std::for_each( v.begin(), v.end(), ZeroSomeData );

struct ZeroSomeData {
void operator()(Obj & o) const { o.some_data = 0; }
};

std::for_each(v.begin(), v.end(), ZeroSomeData());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,579
Latest member
CharaS3188

Latest Threads

Top