473,394 Members | 1,765 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

vector and for-loop

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.
Jul 22 '05 #1
14 1862
It should be:

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

"Kitty" <No spam> ¼¶¼g©ó¶l¥ó·s»D:41**********@rain.i-cable.com...
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.

Jul 22 '05 #2

"Kitty" <No spam> wrote in message news:41**********@rain.i-cable.com...
It should be:

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

"Kitty" <No spam> ¼¶¼g©ó¶l¥ó·s»D:41**********@rain.i-cable.com...
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

Jul 22 '05 #3
"Kitty" <No spam> wrote in message news:41**********@rain.i-cable.com...
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.
Jul 22 '05 #4

"Kitty" <No spam> wrote in message news:41**********@rain.i-cable.com...
It should be:

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


std::fill(v.begin(), v.end(), 0);
Jul 22 '05 #5

"Andrew Koenig" <ar*@acm.org> wrote in message
news:iv**********************@bgtnsc04-news.ops.worldnet.att.net...
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...


Jul 22 '05 #6
I have to call this code many times. So faster method is required.

"Kitty" <No spam> ¼¶¼g©ó¶l¥ó·s»D:41**********@rain.i-cable.com...
It should be:

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

"Kitty" <No spam> ¼¶¼g©ó¶l¥ó·s»D:41**********@rain.i-cable.com...
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.


Jul 22 '05 #7
Kitty wrote:
It should be:

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

"Kitty" <No spam> ¼¶¼g©ó¶l¥ó·s»D:41**********@rain.i-cable.com...
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[i].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

Jul 22 '05 #8
Kitty wrote:
vector<Obj> v;
....
for(i=0;i<v.size();i++)
v[i].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);
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 22 '05 #9
Dietmar Kuehl wrote:
Kitty wrote:
vector<Obj> v;
....
for(i=0;i<v.size();i++)
v[i].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
Jul 22 '05 #10
"Kitty" <No spam> wrote in message news:41**********@rain.i-cable.com...
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.
Jul 22 '05 #11
> vector<Obj> v;
...
quantity = v.size();
for (i = 0; i < quantity; ++i)
v[i].some_data = 0;


Personally I like more the not-polluting version:

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

but last time I profiled it comparing against

for (unsigned int i = 0; i < v.size(); ++i)
v[i].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
Jul 22 '05 #12
void ZeroSomeData( Obj &o )
{ o.some_data = 0; }
// ...

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

Jul 22 '05 #13

Kitty wrote:
It should be:

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

"Kitty" <No spam>

¼¶¼g©ó¶l¥ó·s»D:41**********@rain.i-cable.com...
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;

Jul 22 '05 #14
In message <11*********************@c13g2000cwb.googlegroups. com>,
al******@hotmail.com writes
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());

--
Richard Herring
Jul 22 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alan Benn | last post by:
(VC6) When I use the STL <vector> template as follows: #include <vector> .... vector<CString> m_nameList; // Names of the chips I get these compiler warnings : C:\Program...
2
by: sci | last post by:
class A; void function(vector<A>* l) { .... } main(){ vector<A> aAList; function(&aAList);
3
by: Duncan | last post by:
I need to populate a vector with the following struct details:\ struct group { string groupname; int gid; list<string> members; }; the text file which this is to read from is in the...
8
by: Daniel L Elliott | last post by:
Hello, I am feeling very confused right now. The following code gives me an error: X.h: template <typename T> ILuint startImage(vector<vector<T> > imageData); X.cpp:
2
by: joseantonio | last post by:
Hi all, I am novice in C++, (basically, novice using OOP). I am developing an application where I use a vector 3D defined as vector< vector< vector<double> > > Ez(NFILAS, vector<...
6
by: hannibal200480 | last post by:
Hi everyone, Here about what it is. vector<vector<Point3D> > pVector0; pVector0 is a vector of vector of 3D points. pVector0 is a vector of 3D points.
9
by: Amadeus W. M. | last post by:
I have a vector from which I want to erase elements between iterators i,j. If i<j, everything works as expected, but if j>i, an insertion is actually performed. Example: vector<double> x(10);...
16
by: Martin Jørgensen | last post by:
Hi, I get this using g++: main.cpp:9: error: new types may not be defined in a return type main.cpp:9: note: (perhaps a semicolon is missing after the definition of 'vector') main.cpp:9:...
32
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: ...
9
by: xman | last post by:
The codes below basically builds a binary tree. It runs well on Intel compiler. However, when I use gcc 4.2.0, the assignment to b.right causes segmentation fault. Tracing with valgrind reveals...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.