Results 1 to 5 of 5
I am trying to write a particle simulation. I have a particles class which has member arrays to hold the information for the set of particles.
The problem is that ...
- 07-09-2011 #1Just Joined!
- Join Date
- May 2011
- Posts
- 8
speed of Arrays vs. Vectors (C++)
I am trying to write a particle simulation. I have a particles class which has member arrays to hold the information for the set of particles.
The problem is that I now want to use 3D arrays (or 3D vectors) whose dimensions will be specified through command line arguments when a user runs the program. I have not found a way to initialize a 3D array (in a constructor) with the dimensions given at the command line, so I thought I should switch to using vectors.
Will there be a huge performance decrease if I make this switch? I am very concerned with speed since these vectors will be holding 10,000 -> 1,000,000 elements and I will be calculating different things with them.
Thanks
Disinterred
- 07-10-2011 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
The STL (Standard Template Library) classes are extensively used by major physics research and data analysis applications. My wife is a staff physicist at Fermi National Laboratory (PhD in HEP), working in the computing division to develop and maintain the software that FermiLab and CERN use for particle physics research. I doubt very much that they would hesitate for a second to use vector classes for this sort of purpose. As for performance, resizing them has the biggest impact, so creating them with the sizes you need initially will have a big impact on minimizing negative performance effects.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 07-10-2011 #3Just Joined!
- Join Date
- May 2011
- Posts
- 8
Thank you for your reply Rubberman! I will definitely switch to vectors now
.
- 07-10-2011 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
A lot of work has gone into optimizing the STL for performance purposes. These days, unless I have to write pure C code (kernel modules and such), I stick with STL classes, and have not been disappointed. There are times when a custom class is appropriate and preferable to STL classes, but those are not the most common situation.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 07-12-2011 #5
@Disinterred: Would you mind posting (some) code so I can learn how you work with vectors in three dimensions?
Thanks!


Reply With Quote