Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Vectors vs Arrays

Vectors vs Arrays

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiographicsperformancehelp
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mcsherry
    wrote on last edited by
    #1

    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

    V M M 3 Replies Last reply
    0
    • M mcsherry

      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

      V Offline
      V Offline
      V 0
      wrote on last edited by
      #2

      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

      M K 2 Replies Last reply
      0
      • V V 0

        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

        M Offline
        M Offline
        mcsherry
        wrote on last edited by
        #3

        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

        M 1 Reply Last reply
        0
        • M mcsherry

          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

          M Offline
          M Offline
          Mike Beckerleg
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • M mcsherry

            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

            M Offline
            M Offline
            Maximilien
            wrote on last edited by
            #5

            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 a delete and for each new [] there should be a delete []


            Maximilien Lincourt Your Head A Splode - Strong Bad

            1 Reply Last reply
            0
            • M mcsherry

              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

              M Offline
              M Offline
              mcsherry
              wrote on last edited by
              #6

              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,

              K 1 Reply Last reply
              0
              • V V 0

                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

                K Offline
                K Offline
                Kevin McFarlane
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • M mcsherry

                  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,

                  K Offline
                  K Offline
                  Kevin McFarlane
                  wrote on last edited by
                  #8

                  According to Stroustrup, vectors are as efficient as arrays. (See his web site.) So it must be a question of poor usage. Kevin

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups