Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > will this initialization be time demanding?
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 21st April 22:08
lianjian
External User
 
Posts: 1
Default will this initialization be time demanding?



Hi:
Here is a double array and its initialization:

// Method I
vector<vector<int> > p; // [numobjs][numnodes]
for(int i=0;i<numobjs;i++)
{
vector<int> q(numnodes);
p.push_back(q);
}

Usually, numnodes will be very large. Will this initialization be
very time-consuming? If so, I have to use something like:

// Method II
vector<vector<int> *) p;
for(int i=0;i<numobjs;i++)
{
vector<int> *q = new vector<int>(numnodes);
p.push_back(q);
}

If Method I is not that time-consuming, I prefer it because it is
intuitively straightforward.

Thanks for your help.

Regards

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
  Reply With Quote


  sponsored links


2 21st April 22:09
bo persson
External User
 
Posts: 1
Default will this initialization be time demanding?



"vizlab" <lianjian@caip.rutgers.edu> skrev i meddelandet
news:f406ff9.0307051102.27baf6b7@posting.google.co m...


There is no use in allocating the vector object dynamically, becase it
is just a small book keeping object. The storage for its content will
be dynamically allocated anyway.

Method III is to tell the vector exactly what you want, and let it do
the initialization. I believe this creates fewer temporary objects, as
the same empty row is copied to all the others:


vector<vector<int> > p(numobjs, vector<int>(numnodes));


Bo Persson
bop2@telia.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666