# Multi Dimensional Vectors

Page 1 of 1

## 2 Replies - 670 Views - Last Post: 18 October 2011 - 11:09 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=251846&amp;s=8838a31896eede90fee41ee2cb8ade9f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cppdeveloper

Reputation: 2
• Posts: 22
• Joined: 27-August 11

# Multi Dimensional Vectors

Posted 18 October 2011 - 10:07 AM

The Multidimensioanl array i declared takes around 500 microseconds to execute whereas a single dimensional vector takes around 5 microseconds, the time difference is over 100 times and is also too costly considering i declare many of these inside a loop. I used to use multidimensioanl arrays but while they were much faster memory allocation became a problem.

```#include <iostream>
#include <vector>
#include <windows.h>
#include <time.h>

using namespace std;

int main()
{
LARGE_INTEGER frequency;
LARGE_INTEGER t1, t2;
double elapsedTime;

QueryPerformanceFrequency(&frequency);
QueryPerformanceCounter(&t1);

vector< vector<int> > ith (750,35);
//vector <int> ich (750);

QueryPerformanceCounter(&t2);
cout << elapsedTime << " ms.\n";

cin.get();
return 0;
}

```

and if the size needs to be increased i use
```ith[i][j]=0;   // how i store values
ith.resize (ith.size()+1, vector<int>(ith[0].size(),0));
ich.resize (ich.size()+1);

```

Is it better to not declare a size and use push_back ? i checked this roughly and you have to declare two additional vectors and assign thier values to the multidimensional one but this is even more time consuming

How do i optmimize my code ?

This post has been edited by cppdeveloper: 18 October 2011 - 10:18 AM

Is This A Good Question/Topic? 0

## Replies To: Multi Dimensional Vectors

### #2 vividexstance

• Tiocfaidh ár lá

Reputation: 792
• Posts: 2,873
• Joined: 31-December 10

## Re: Multi Dimensional Vectors

Posted 18 October 2011 - 11:06 AM

If you know the maximum size you will need ahead of time, then just use that size in the constructor or call the "reserve(size_t newCapacity)" function on the vector. Whenever the vector runs out of storage, it needs to allocate more storage, copy all of the data over to this new storage, and then delete the old storage. So if you know the max size, setting the initial storage size in the constructor can really help eliminate the need to allocate storage and copy over the elements.

### #3 BetaWar

• #include "soul.h"

Reputation: 1513
• Posts: 8,290
• Joined: 07-September 06

## Re: Multi Dimensional Vectors

Posted 18 October 2011 - 11:09 AM

I believe that the multi-dimensional array is pretty much the optimized code... (at least if you are doing things correct) You could try using a single dimensional array with all the elements you need in it (so, if you wanted a 2x5 multi-dimensional array you would do a 2*5 = 10 array) and then do a bit of magic to access indexes like a multi-dimensional array.

One thing is for sure though - using a vector with push_back is NOT the optimal way of doing things. Vectors allocate as little space as they can get away with when created, because you are simply adding items to the back you may go over the number of items that the vector can hold at which point it needs to reallocate ALL the items in the vector to a new place in memory.

You want the array or vector to know exactly how many items it will have before using it if you can. It will increase speed.