CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23
  1. #16
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Selfappending vector

    Right, that is actually even better.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  2. #17
    Join Date
    Mar 2010
    Posts
    16

    Re: Selfappending vector

    Quote Originally Posted by cilu View Post
    You could also do this using a second, temporary vector:
    I'm sorry but... thats just awfull, slow and sluggish.

  3. #18
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Selfappending vector

    Quote Originally Posted by po0ky View Post
    I'm sorry but... thats just awfull, slow and sluggish.
    Did you time it?

    And no, you can't tell by eyeballing STL code and determine it is slow and sluggish -- too many have made that mistake, and then are shocked that the code is not sluggish at all (once a release, optimized build is produced).

    Edit: For Visual C++, you have to set as your preprocessor _SECURE_SCL=0 to make sure iterators are not checked in the release build. Once you do that and produce a release build, then do timing tests. Don't bother posting debug build timing tests, no one will look at them, as they're meaningless.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 30th, 2010 at 05:47 PM.

  4. #19
    Join Date
    Mar 2010
    Posts
    16

    Re: Selfappending vector

    Quote Originally Posted by Paul McKenzie View Post
    Did you time it?
    How did you know that?

    And no, you can't tell by eyeballing STL code and determine it is slow and sluggish -- too many have made that mistake, and then are shocked that the code is not sluggish at all (once a release, optimized build is produced).
    It's as if you are looking over my shoulder.

    Edit: For Visual C++, you have to set as your preprocessor _SECURE_SCL=0 to make sure iterators are not checked in the release build. Once you do that and produce a release build, then do timing tests. Don't bother posting debug build timing tests, no one will look at them, as they're meaningless.
    o_0... I gues i'm still a real noob...

    Well I did the timing again, and my god you are right. Cilu's method is faster.

    I wonder if there is an even faster way?

  5. #20
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Selfappending vector

    Quote Originally Posted by po0ky View Post
    It's as if you are looking over my shoulder.
    No, not really. A lot of times we get posts from persons who either are just (as I said) eyeballing STL code, and assume "it's slow" because it uses a function object, or a class, or doesn't use pointers all over the place, whatever.

    Or we get persons who time debug builds and claim "STL is 10 times slower than when I use pointers". Of course after telling the person to build a release build using the preprocessor _SECURE_SCL=0, then their non-STL code is no faster, and most times, slower than the STL version.

    Many programmers believe that if they use a lot of pointers and/or dynamically allocate memory themselves using new[]/delete[] all over the place, that their code is faster than using STL containers and algorithms correctly. The problem is that an overuse of pointers causes the compiler's optimizer to not attempt to optimize the code, due to excessive pointer aliasing by the programmer. Excessive pointer aliasing renders an optimizer practically useless.

    With STL, the compiler is well aware of the constructs used, and can optimize the code efficiently (inlining, for example). That's one underlying cause of why "pointered-up" code and STL code that uses little to no pointers seem to be on a par when it comes to speed, with the STL code many times beating the non-STL code.
    Well I did the timing again, and my god you are right. Cilu's method is faster.
    Not surprising.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 30th, 2010 at 10:34 PM.

  6. #21
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Selfappending vector

    well it doesn't surprise me it's faster. But for other reasons.

    The original implementation does a potential resize (alloc, copy, dealloc) on each insert.
    Additionally, it creates a temporary string on each insert.

    The temporary vector solution of Cilu pulls ahead because it avoids the above overhead, but does so at the cost of an additional chunk of memory.
    Don't have acces to a compiler atm, , but I'd expect the following to be even faster:
    Code:
    int iSize = vec.size();
    vec.resize(iSize*2-1);
    for (int i=1; i<iSize;i++)
        vec[iSize-1+i]=vec[iSize-1-i];

  7. #22
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Selfappending vector

    Quote Originally Posted by OReubens
    The original implementation does a potential resize (alloc, copy, dealloc) on each insert.
    The same amortised expansion happens when std::back_inserter is used though, unless po0ky tested with my suggested modification instead, in which case it might be more like your suggestion. A variant of your suggestion:
    Code:
    const std::vector<std::string>::size_type old_size = strvec.size();
    strvec.resize(old_size * 2 - 1);
    const std::vector<std::string>::iterator old_end = strvec.begin() + old_size;
    std::reverse_copy(strvec.begin(), old_end - 1, old_end);
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #23
    Join Date
    Mar 2010
    Posts
    16

    Re: Selfappending vector

    Quote Originally Posted by laserlight View Post
    A variant of your suggestion:
    Code:
    const std::vector<std::string>::size_type old_size = strvec.size();
    strvec.resize(old_size * 2 - 1);
    const std::vector<std::string>::iterator old_end = strvec.begin() + old_size;
    std::reverse_copy(strvec.begin(), old_end - 1, old_end);
    I think we have a winner. This could probably only be faster if the size of the resulting vector would be hard coded.

    I have read somewhere that lists are faster in sequential processing, but even with lists I couldn't make this run faster.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured