| | |
Array question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 628
Reputation:
Solved Threads: 46
I have been using vector in c++ lately, but I needed to speed up my code a bit so someone recommended I use regular arrays. My question is 2 part -
1) I thought you couldn't do this!!?? For some reason it is working with no problem?
2) I used a timer (from the VUL library of VXL) to time the following two functions. The only difference is one of the arrays is allocated using a global int, and the other is allocated using a #define. It actually turns out the the global int is twice as fast!!?? I thought they should be about the same (if the int even worked at all! (see question 1))
Any thoughts on either of these?
Thanks,
Dave
1) I thought you couldn't do this!!?? For some reason it is working with no problem?
C++ Syntax (Toggle Plain Text)
int a=2; int test[a];
2) I used a timer (from the VUL library of VXL) to time the following two functions. The only difference is one of the arrays is allocated using a global int, and the other is allocated using a #define. It actually turns out the the global int is twice as fast!!?? I thought they should be about the same (if the int even worked at all! (see question 1))
C++ Syntax (Toggle Plain Text)
FunctionWithArray time: 17 ms FunctionWithDynamicArray time: 8 ms
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int DynamicSize = 2000000; #define ArraySize 2000000 void FunctionWithDynamicArray(); void FunctionWithArray(); int main() { FunctionWithDynamicArray(); FunctionWithArray(); return 0; } void FunctionWithDynamicArray() { int test[DynamicSize]; for(int i = 0; i < DynamicSize; i ++) { test[i] = i; } } void FunctionWithArray() { int test[ArraySize]; for(int i = 0; i < ArraySize; i ++) { test[i] = i; } }
Any thoughts on either of these?
Thanks,
Dave
> 1) I thought you couldn't do this!!?? For some reason it is working with no problem?
Depends on your compiler.
Some compiler extensions permit this, so you just get lucky.
Enable whatever "strict ANSI" flags for a better idea of what is allowed and what isn't
2 million int's = 8MB of stack space.
Since stack space is allocated on demand, the first one to be called has all the overheads of allocating yet another page of stack every 4K.
But the second one - now that's already got the stack allocated from the first one, so it just blitzes through.
Swap the calls over, you'll see the reverse.
Or they could have been the same for a completely difference reason. Namely that the array is not used, and the compiler could have optimised the code out.
Depends on your compiler.
Some compiler extensions permit this, so you just get lucky.
Enable whatever "strict ANSI" flags for a better idea of what is allowed and what isn't
2 million int's = 8MB of stack space.
Since stack space is allocated on demand, the first one to be called has all the overheads of allocating yet another page of stack every 4K.
But the second one - now that's already got the stack allocated from the first one, so it just blitzes through.
Swap the calls over, you'll see the reverse.
Or they could have been the same for a completely difference reason. Namely that the array is not used, and the compiler could have optimised the code out.
•
•
•
•
Originally Posted by daviddoria
I have been using vector in c++ lately, but I needed to speed up my code a bit so someone recommended I use regular arrays.
If at first you don't succeed, keep on sucking until you do succeed.
Never use std::vector for numbers crunching (in scientific or engineering calculations). This STL container never intended for fast calculations. If you don't want to take much trouble over new/delete, use std::valarray class.
For example, times to calculate sum of 10000000 double type elements:
AMD 5000+,VC++ 9 (Plauger's STL implementation), release mode, optimization for speed.
The vector was previously resized and initialized - no "overhead of C++'s object model" in the test loop. Timing precision was equal to ~1 microsecond.
For example, times to calculate sum of 10000000 double type elements:
C++ Syntax (Toggle Plain Text)
1. vector[]: ~50 milliseconds 2. vector/iterator: ~90 milliseconds 3. array[]: ~25 millisecond 4. valarray[]: ~26 milliseconds 5. valarray.sum(): ~24 milliseconds
The vector was previously resized and initialized - no "overhead of C++'s object model" in the test loop. Timing precision was equal to ~1 microsecond.
Last edited by ArkM; Aug 17th, 2008 at 8:38 pm.
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
I agree with Radical Edward - you may save some time switching from vectors to arrays (maybe half the time), but you may be able to structure your data so that you can perform your most often called operations in log(n) time instead of linear time. In this case a linear solution that took 1 second might take 10ms - a much larger improvement. What exactly are you storing and what kind of operations are you doing that is taking your program too long?
Alas, there are lots of problems (in computational physics, for exampe) where you never achieve O(log n) or more realistic O(n*log n) computational complexity. For example, you never multiply two matrices with better than O(n**2) operations (all existing algorithms have slightly better than O(n**3) ). There are lots of gas and fluids mechanics problems where computational complexity O(n**4) or even more...
Last edited by ArkM; Aug 18th, 2008 at 2:09 am.
•
•
Join Date: Feb 2008
Posts: 628
Reputation:
Solved Threads: 46
I am not really doing operations on the vector, I am just storing values and retrieving them later. The problem is there are just lots and lots of them. I'll look into valarray's, but for now, if I use a standard array
don't I have to then manually delete the variable when I'm done with it?
Also, how can you get the length of the array after the fact (ie. the equivalent of MyVector.size() )?
Thanks,
Dave
C++ Syntax (Toggle Plain Text)
int *myArray = new int [ somesize ];
Also, how can you get the length of the array after the fact (ie. the equivalent of MyVector.size() )?
Thanks,
Dave
•
•
•
•
I am not really doing operations on the vector, I am just storing values and retrieving them later. The problem is there are just lots and lots of them.
If you're searching for values, the time you spend paging cache lines in and out is probably going to be significant with lots and lots of values, so a structure designed for searching like a set or hash_set should be your first step in optimizing the performance.
•
•
•
•
Originally Posted by daviddoria
don't I have to then manually delete the variable when I'm done with it?
•
•
•
•
Originally Posted by daviddoria
Also, how can you get the length of the array after the fact (ie. the equivalent of MyVector.size() )?
C++ Syntax (Toggle Plain Text)
#include <iostream> int main() { int a[100]; // Prints 100 std::cout << sizeof a / sizeof a[0] << '\n'; }
If at first you don't succeed, keep on sucking until you do succeed.
![]() |
Similar Threads
- C++ Array Question (C++)
- 2 D array question (C++)
- Strings in array [I NEED HELP] C lang. (C)
- Class with dynamic array how? (C++)
- File array question (C++)
- Array question (C++)
- array question (C++)
Other Threads in the C++ Forum
- Previous Thread: "Form1.Form7.resources"
- Next Thread: I'm drawing a blank
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






