# optimization (vectors)

Page 1 of 1

## 8 Replies - 1065 Views - Last Post: 18 June 2011 - 02:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=234605&amp;s=22150dab5add872c0f52ba2f695a8a36&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 g_niro

• New D.I.C Head

Reputation: -2
• Posts: 44
• Joined: 26-January 11

# optimization (vectors)

Posted 05 June 2011 - 03:46 AM

i have a doubt on following case; suppose i want to define a vector of vector to acomadate set of elements and i can add the data and can be used those elemnts to compute something else. then i dont want that vector anymore. then later, suppose if i want to accomadae another set of data as a vector of vector, then i can reuse the previously created variable, then;

(1) if I created my vector of vector as dynamic memory and deleted as

vector<vecctor<double> > *myvector = new vector<vecctor<double> >;
//do push back and use it
delete myvector;
and then reuse again

(2) if I created my vector of vector as simply

vector<vecctor<double> > myvector;
//do push back and use it
myvector.clear();
and then reuse again

but, i guess in both method very few memory is remaining though we have removed it. so, i would like to know what is the efficient method to defin this vector of vector.

(3) if the size of the inside vector is always 2, then is it still efficient to use vector of vector by defining

vector<vector<double> > myvector(my_list.size(), vector<double>(2)) other than another container type

(4) if i use predefined another class to hold inside 2 elements and then take a vector of those object type as (for example XY is the class which can hold 2 elements, may be as an array)

vector<XY>; i hope, please anyone comment me what would be the most efficient method (from 1-4) interms of speed and memory needed. is there any better ways, plz suggest me too. thanks

Is This A Good Question/Topic? 0

## Replies To: optimization (vectors)

### #2 jimblumberg

Reputation: 5424
• Posts: 16,909
• Joined: 25-December 09

## Re: optimization (vectors)

Posted 05 June 2011 - 06:13 AM

First why did you create a new post, this seems to be the same questions you are asking in you other posts.

I don't see why you would use your example #1, that seems to defeat one of the major purposes of vectors, automatic dynamic memory. If your vector<vector> > will always be a vector of two related items then you should consider a vector<yourClass> to keep things simple.

Quote

i hope, please anyone comment me what would be the most efficient method (from 1-4) interms of speed and memory needed.

Until you can prove that you have speed or memory issues you should not be overly concerned with these issues. Your first worry should be with code maintainability, and code clarity. Writing clear easy to maintain code will, in my opinion, usually lead to a faster and less problematic program.

Jim
Was This Post Helpful? 2

### #3 Luckless

• </luck>

Reputation: 293
• Posts: 1,146
• Joined: 31-August 09

## Re: optimization (vectors)

Posted 05 June 2011 - 06:15 AM

as for OO design, designing a class to hold the two values then put that in a one-dimensional array is the way to go. I would definitely use (4). The other ways are poorly structured.
Was This Post Helpful? 0

### #4 g_niro

• New D.I.C Head

Reputation: -2
• Posts: 44
• Joined: 26-January 11

## Re: optimization (vectors)

Posted 05 June 2011 - 01:20 PM

thanks. yes. the question has some relationship to my previous another post. but, for more clarity and to go with other methods which i can use, i posted this. but, the comments on this post is also realy helpful for me to go further with my work. this post will never neglect the person who commented me previously. this is just for my undestanding as i am new to c++. thanks...
Was This Post Helpful? 0

### #5 NickDMax

• Can grep dead trees!

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: optimization (vectors)

Posted 05 June 2011 - 10:10 PM

#1 -- generally speaking we don't do:

vector<whatever>* myvector = new vector<whatever>()

As Jim posted above this kind of defeats the main purpose of vectors. I suppose that there are times when this might happen (like when you need to return a vector from a function -- but new C++0x features such a move semantics and rvalue references would even remove the need for that).

#2 The nice thing about using clear() is that it maintains the current "useable space" in the vector. THat is, if you vector had room for 1000 elements and you cleared it, you will not reallocate memory until you pass that previous size. -- However this can mean that memory is being needlessly held if you never again need that many elements.

#3 Ok. I didn't really get a question there...

#4 A vector of vector<XY> might be slightly more efficient then a vector of vectors because there is no need for the inner allocation. So for example:

```struct Coord {
double x;
double y;
Coord(double xvalue, double yvalue) : x(xvalue), y(yvalue) { }
};

vector<Coord> coordinates(1000, Coord(0, 0));

```

would may very well be better than:
```vector<vector<double> > coordinates(1000, vector(2, 0));
```

So I used this (rather meaningless) test code to see what the difference might be:
```#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <utility>

using namespace std;

struct Coord {
double x;
double y;
Coord(double xvalue, double yvalue) : x(xvalue), y(yvalue) { }
};

const int TestSize = 10000000;

int main() {
srand(time(0));
double now = clock();
vector<Coord> coordinates(TestSize, Coord(0, 0));
cout << "Allocation done: " << clock() - now << endl;
for (int i = 0; i < TestSize; ++i) {
coordinates[rand() % TestSize].x = rand();
coordinates[rand() % TestSize].y = rand();
}
cout << clock() - now << endl;

now = clock();
vector<vector<double> > coordinates2(TestSize, vector<double>(2, 0));
cout << "Allocation done: " << clock() - now << endl;
for (int i = 0; i < TestSize; ++i) {
coordinates2[rand() % TestSize][0] = rand();
coordinates2[rand() % TestSize][1] = rand();
}
cout << clock() - now << endl;

now = clock();
vector<pair<double, double> > coordinates3(TestSize, pair<double, double>(0, 0));
cout << "Allocation done: " << clock() - now << endl;
for (int i = 0; i < TestSize; ++i) {
coordinates3[rand() % TestSize].first = rand();
coordinates3[rand() % TestSize].second = rand();
}
cout << clock() - now << endl;

return 0;
}
```

The result was:
```> "C:\CProjects\Forum Help\vectCoords.exe "
Allocation done: 129
1056
Allocation done: 1052
2002
Allocation done: 153
1058
```

So -- these results are not terribly meaningful and you REALLY should do what makes the most logical sense for your program BUT -- I hope this helps put your thoughts to ease. The vector<struct> was the best, the vector<vector> the worst, and the vector<pair> was comparable to the struct. In fact when I ran it a second time I got:
```> "C:\CProjects\Forum Help\vectCoords.exe "
Allocation done: 139
1075
Allocation done: 1044
2074
Allocation done: 110
1018
```
-- so the first and last cases are probably about the same. the vector<vector> is much slower.
Was This Post Helpful? 2

### #6 g_niro

• New D.I.C Head

Reputation: -2
• Posts: 44
• Joined: 26-January 11

## Re: optimization (vectors)

Posted 08 June 2011 - 05:18 AM

Dear NickDMax;;
thank you very much for your greate explanation and results of testing. many thanks for spending time for this.... really a very good job... thanks...
Was This Post Helpful? 0

### #7 baavgai

• Dreaming Coder

Reputation: 7119
• Posts: 14,837
• Joined: 16-October 07

## Re: optimization (vectors)

Posted 08 June 2011 - 09:47 AM

I like the test. I wanted to try it on my own, with a different style.
```#include <iostream>
#include <iomanip>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <utility>

using namespace std;

struct Coord {
double x;
double y;
Coord(double xvalue, double yvalue) : x(xvalue), y(yvalue) { }
};

void addItem(vector<Coord> &coordinates, long p1, long p2, double x, double y) {
coordinates[p1].x = x;
coordinates[p2].y = y;
}

void addItem(vector< vector<double> > &coordinates, long p1, long p2, double x, double y) {
coordinates[p1][0] = x;
coordinates[p2][1] = y;
}

void addItem(vector< pair<double, double> > &coordinates, long p1, long p2, double x, double y) {
coordinates[p1].first = x;
coordinates[p2].second = y;
}

template<class T>
double allocateTest(const string &name, long testSize, T defaultValue) {
cout << name << endl;
double now = clock();
vector<T> coordinates(testSize, defaultValue);
cout << setw(20) << setprecision(20) << ((long)clock() - now) << endl;
for (long i = 0; i < testSize; i++) {
addItem(coordinates, rand() % testSize, rand() % testSize, rand(), rand());
}
cout << setw(20) << setprecision(20) << ((long)clock() - now) << endl;
cout << endl;
}

int main() {
srand(time(0));
const long TestSize = 10000000;

allocateTest("Coord", TestSize, Coord(0, 0));
allocateTest("vector<double>", TestSize, vector<double>(2, 0));
allocateTest("pair<double, double>", TestSize, pair<double, double>(0, 0));

return 0;
}

```

Results:
```Coord
310000
5110000

vector<double>
1260000
10350000

pair<double, double>
470000
6220000

```

Clearly, I'm far slower than you. But, of course, the code is doing a lot more work. Still, this is kind of real world usage.

I didn't think I needed the random. This was also interesting.

```template<class T>
double allocateTest(const string &name, long testSize, T defaultValue) {
cout << name << endl;
double now = clock();
vector<T> coordinates(testSize, defaultValue);
cout << setw(20) << setprecision(20) << ((long)clock() - now) << endl;
for (long i = 0; i < testSize; i++) {
coordinates[i] = defaultValue;
}
cout << setw(20) << setprecision(20) << ((long)clock() - now) << endl;
cout << endl;
}

```

Results:
```Coord
290000
420000

vector<double>
1360000
1840000

pair<double, double>
470000
600000

```

Here we're just spinning and copying a value. I'm on the side of the struct, regardless. Anonymous pairs of numbers aren't real good for readability.
Was This Post Helpful? 1

### #8 NickDMax

• Can grep dead trees!

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: optimization (vectors)

Posted 08 June 2011 - 12:43 PM

slightly modified to compile on my machine (added #include<stirng> and made allocateTest void return type) these were the results:
```> "C:\CProjects\Forum Help\234605-optimization-vectors_baavgai1.exe "
Coord
155
1080

vector<double>
1056
2048

pair<double, double>
139
1059
```

and for the second test I got:
```> "C:\CProjects\Forum Help\234605-optimization-vectors_baavgai2.exe "
Coord
149
286

vector<double>
1083
1337

pair<double, double>
130
228

```

Either the rand() was really eating up the time, or my compiler optimized things there a bit.
Was This Post Helpful? 1

### #9 g_niro

• New D.I.C Head

Reputation: -2
• Posts: 44
• Joined: 26-January 11

## Re: optimization (vectors)

Posted 18 June 2011 - 02:52 PM

thanks for the cooperation. weldone
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }