943,948 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1403
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 17th, 2008
0

Array question

Expand Post »
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?
C++ Syntax (Toggle Plain Text)
  1. int a=2;
  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)
  1. FunctionWithArray time: 17 ms
  2. FunctionWithDynamicArray time: 8 ms

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int DynamicSize = 2000000;
  6. #define ArraySize 2000000
  7.  
  8. void FunctionWithDynamicArray();
  9. void FunctionWithArray();
  10.  
  11. int main()
  12. {
  13. FunctionWithDynamicArray();
  14. FunctionWithArray();
  15. return 0;
  16. }
  17.  
  18. void FunctionWithDynamicArray()
  19. {
  20. int test[DynamicSize];
  21. for(int i = 0; i < DynamicSize; i ++)
  22. {
  23. test[i] = i;
  24. }
  25. }
  26.  
  27. void FunctionWithArray()
  28. {
  29. int test[ArraySize];
  30. for(int i = 0; i < ArraySize; i ++)
  31. {
  32. test[i] = i;
  33. }
  34. }

Any thoughts on either of these?

Thanks,

Dave
Similar Threads
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Aug 17th, 2008
0

Re: Array question

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 17th, 2008
0

Re: Array question

you're right, I switched the order and in both cases the first one took longer!!

How would you make an array of size determined at run time if you're compiler doesn't allow this?

Thanks,

Dave
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Aug 17th, 2008
0

Re: Array question

int *myArray = new int [ somesize ];
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 17th, 2008
0

Re: Array question

Quote 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.
Using arrays instead of vectors only saves you the overhead of C++'s object model, especially if you need a dynamically sized array. Instead of playing memory games, try using something other than a linear storage structure for your data. Your code might be too slow because you should be using a set instead of a vector, for example.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008
Aug 17th, 2008
0

Re: Array question

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:
C++ Syntax (Toggle Plain Text)
  1. 1. vector[]: ~50 milliseconds
  2. 2. vector/iterator: ~90 milliseconds
  3. 3. array[]: ~25 millisecond
  4. 4. valarray[]: ~26 milliseconds
  5. 5. valarray.sum(): ~24 milliseconds
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.
Last edited by ArkM; Aug 17th, 2008 at 8:38 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 18th, 2008
0

Re: Array question

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?
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Aug 18th, 2008
0

Re: Array question

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.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Aug 18th, 2008
0

Re: Array question

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
C++ Syntax (Toggle Plain Text)
  1. int *myArray = new int [ somesize ];
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
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Aug 18th, 2008
0

Re: Array question

Quote ...
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.
How are you retrieving them? Is it in linear order or some kind of search? A straight traversal from beginning to end or end to beginning is well suited for an array, but you probably won't see a big difference between an array and a vector in that case. The storage overhead for a vector will be overwhelmed right away by the values, and the runtime cache difference between an array and a vector will be nonexistent.

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.

Quote originally posted by daviddoria ...
don't I have to then manually delete the variable when I'm done with it?
Yes, if you manually allocate memory, you have to manually delete it.

Quote originally posted by daviddoria ...
Also, how can you get the length of the array after the fact (ie. the equivalent of MyVector.size() )?
If it's an actual array you can divide the size of the array as a whole by the size of a single element to get the number of elements:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. int a[100];
  6.  
  7. // Prints 100
  8. std::cout << sizeof a / sizeof a[0] << '\n';
  9. }
If all you have is a pointer, you have to rely on passing around the size as a separate entity. Or you can wrap the whole thing in an object so that it has a size() method, just like the vector class.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: "Form1.Form7.resources"
Next Thread in C++ Forum Timeline: I'm drawing a blank





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC