Vectors vs Arrays
-
Hi all, Apologies if this has already been covered but I could find it anywhere. The problem that I have is that a piece of software that I am working on used a lot of arrays and matrices (i.e. float* and float**), the problem that we where having was that the program was leaking memory like there was no tomorrow (about a meg every 10secs) and the program was crashing at 'innocent' parts of the code. After spending quite a while trying to solve these errors I decided to go at it with a sledge hammer and change all of the arrays and matrices to STL vectors (i.e. vector for the arrays and a vector for the matrices). I've now finished making the changes and the program no longer leaks memory or crashes, however it now runs at about 50% slower. I've changed where the vectors are created so that they are only created once. This has improved things quite a lot however it is still noticibly slower about 10%, does anyone have any other easy optimisation methods that I can implement. I've left the way that the program accesses the data the same using the [] and [][] brackets. Any thoughts or suggestions will be gratefully received! Andy
-
Hi all, Apologies if this has already been covered but I could find it anywhere. The problem that I have is that a piece of software that I am working on used a lot of arrays and matrices (i.e. float* and float**), the problem that we where having was that the program was leaking memory like there was no tomorrow (about a meg every 10secs) and the program was crashing at 'innocent' parts of the code. After spending quite a while trying to solve these errors I decided to go at it with a sledge hammer and change all of the arrays and matrices to STL vectors (i.e. vector for the arrays and a vector for the matrices). I've now finished making the changes and the program no longer leaks memory or crashes, however it now runs at about 50% slower. I've changed where the vectors are created so that they are only created once. This has improved things quite a lot however it is still noticibly slower about 10%, does anyone have any other easy optimisation methods that I can implement. I've left the way that the program accesses the data the same using the [] and [][] brackets. Any thoughts or suggestions will be gratefully received! Andy
Hi, If the type is known (in your case float) you could write your own vector class. Do the maintance (new/delete) in there and use the good old (double if necessary) arrays. Vector classes are always quite slow. If speed is primary you should use arrays. hope this helps... "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
-
Hi, If the type is known (in your case float) you could write your own vector class. Do the maintance (new/delete) in there and use the good old (double if necessary) arrays. Vector classes are always quite slow. If speed is primary you should use arrays. hope this helps... "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
cheers for your reply, I think I have now located where the bottle neck in the code is! basically we copy a BYTE array (about 23000 elements each time!) into another vector (of type unsigned char) about 20-25 times a second which is slowing the whole process down so I'm now currently looking at optimising the copying process
-
cheers for your reply, I think I have now located where the bottle neck in the code is! basically we copy a BYTE array (about 23000 elements each time!) into another vector (of type unsigned char) about 20-25 times a second which is slowing the whole process down so I'm now currently looking at optimising the copying process
One thing to do when copying into vectors is use
reserve()
first if you know how many ites you need to copy in. Saves a lot of time spent allocating and copying progressively bigger blocks of memory. Mike -
Hi all, Apologies if this has already been covered but I could find it anywhere. The problem that I have is that a piece of software that I am working on used a lot of arrays and matrices (i.e. float* and float**), the problem that we where having was that the program was leaking memory like there was no tomorrow (about a meg every 10secs) and the program was crashing at 'innocent' parts of the code. After spending quite a while trying to solve these errors I decided to go at it with a sledge hammer and change all of the arrays and matrices to STL vectors (i.e. vector for the arrays and a vector for the matrices). I've now finished making the changes and the program no longer leaks memory or crashes, however it now runs at about 50% slower. I've changed where the vectors are created so that they are only created once. This has improved things quite a lot however it is still noticibly slower about 10%, does anyone have any other easy optimisation methods that I can implement. I've left the way that the program accesses the data the same using the [] and [][] brackets. Any thoughts or suggestions will be gratefully received! Andy
well, I think that if speed is important, you should go back and figure out why your application is leaking memory ... if using VC++ compile in debug mode, and use the DEBUG_NEW define macro to help you find where exactly is the leak; and/or use BoundChecker or some other 3rd party software helper to find te leaks. you found ( from you other post ) that you copy a lot of arrays, maybe that's where you are leaking memory ... remember that for each
new
there should be adelete
and for eachnew []
there should be adelete []
Maximilien Lincourt Your Head A Splode - Strong Bad
-
Hi all, Apologies if this has already been covered but I could find it anywhere. The problem that I have is that a piece of software that I am working on used a lot of arrays and matrices (i.e. float* and float**), the problem that we where having was that the program was leaking memory like there was no tomorrow (about a meg every 10secs) and the program was crashing at 'innocent' parts of the code. After spending quite a while trying to solve these errors I decided to go at it with a sledge hammer and change all of the arrays and matrices to STL vectors (i.e. vector for the arrays and a vector for the matrices). I've now finished making the changes and the program no longer leaks memory or crashes, however it now runs at about 50% slower. I've changed where the vectors are created so that they are only created once. This has improved things quite a lot however it is still noticibly slower about 10%, does anyone have any other easy optimisation methods that I can implement. I've left the way that the program accesses the data the same using the [] and [][] brackets. Any thoughts or suggestions will be gratefully received! Andy
-
Hi, If the type is known (in your case float) you could write your own vector class. Do the maintance (new/delete) in there and use the good old (double if necessary) arrays. Vector classes are always quite slow. If speed is primary you should use arrays. hope this helps... "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix
V. wrote: If speed is primary you should use arrays. According to Stroustrup, vectors are as efficient as arrays. (See his web site.) So it must be a question of poor usage. Kevin
-
thanks everyone for your suggestions, what I've done is build the release version and it 'appears' to be fast enough for our needs. I'm currently in the process of verifying this through the code. Andy,
According to Stroustrup, vectors are as efficient as arrays. (See his web site.) So it must be a question of poor usage. Kevin