vectors vs c style arrays

This is a discussion on vectors vs c style arrays within the C++ Programming forums, part of the General Programming Boards category; Everything I have read recommends using stl vectors instead of c style arrays. But when a ran some tests of ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    vectors vs c style arrays

    Everything I have read recommends using stl vectors instead of c style arrays. But when a ran some tests of 100 million accesses of arrays and vectors, I found that the array part finished in 1 second and the vector part took 6 seconds. This was using the [] notation with the vector. It took even longer with the at() notation. Is the difference in performance this large? If this is the case, presumably vectors are not the way to go if the program requires many accesses to an array?
    Thanks
    Mark

  2. #2
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    Did you turn on optimizations?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    38
    I dont know which optimizations to turn on. I am using the g++ compiler on linux.
    Last edited by markucd; 04-19-2006 at 10:04 AM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Can you show your code?

    Are you timing only accesses, or are you timing filling of the vector and array and accessing their elements? Are you comparing the vector to a local array, or a dynamically allocated one?

    In most cases, any performance differences between the two won't be noticable if you are using vector properly.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Compile with the -O2 flag to turn on optimisations.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    38
    I am using statically allocated arrays, and I am only timing the array access part. The O2 optimization flag worked.
    Thanks
    Mark

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    The soon-to-be standard alternative to static arrays is std::tr1::array, which is available from boost right now (perhaps just as boost::array). It might be more appropriate than vector if all you need is a static array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which style of if loops is most common?
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 38
    Last Post: 08-25-2005, 03:18 PM
  2. WS_EX_COMPOSITED style (double buffering) problems
    By JasonD in forum Windows Programming
    Replies: 2
    Last Post: 10-12-2004, 11:21 AM
  3. byte arrays & vectors
    By kasun in forum C++ Programming
    Replies: 1
    Last Post: 02-29-2004, 08:10 AM
  4. arrays or vectors
    By Geo-Fry in forum C++ Programming
    Replies: 26
    Last Post: 04-17-2003, 07:08 PM
  5. arrays and vectors
    By volk in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2003, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21