2 Replies - 338 Views - Last Post: 18 October 2011 - 11:09 AM Rate Topic: -----

#1 cppdeveloper  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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);
elapsedTime = (t2.QuadPart - t1.QuadPart) * 1000.0 / frequency.QuadPart;
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  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 678
  • View blog
  • Posts: 2,331
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1167
  • View blog
  • Posts: 7,207
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1