std::string::find vs std::find

This is a discussion on std::string::find vs std::find within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Daved I'm a little surprised the sort takes that long. I would think it would be closer ...

  1. #16
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    Quote Originally Posted by Daved View Post
    I'm a little surprised the sort takes that long. I would think it would be closer to the set's load time. Perhaps it is because your data is already mostly sorted?
    I think that for it to take that long he would have to be sorting it after every push_back.

    If you only sort it once, after the data is all loaded in, then perform many binary_searches, then the overhead of the sorting becomes less and less significant to the point that the binary_search will be faster. Still wont beat hashing, but it should be good enough.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  2. #17
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    Hashing would definitely be faster and it's fairly trivial to write a templated hash container. If I need to 'find' items in a collection I rarely, if ever, use vector or list. For very small containers you probably will not notice any issues but you will as the container grows in size. Sorting can also be slow if you don't use it wisely. This is what iMalc is referring to. A brute force approach would be to sort after every change to the collection and that would also be the slowest approach. The less operations you perform on the container the faster it is going to be. If you only sort when you need to or can break the sort down into segments so less items are sorted at any one time then you should get improvements.

    However the only way to know for sure is to try several approaches and see which one works best for your particular application.
    Last edited by VirtualAce; 07-08-2009 at 04:16 PM.

  3. #18
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Some time ago I discovered a performance bug in MingW's implementation of iter_swap (which is used to swap items by the sorting routine among others). If you happen to have this compiler and change the implementation to:

    Code:
      //fixed implementation (must be moved after swap in stl_algobase.h)
      template<typename _ForwardIterator1, typename _ForwardIterator2>
        inline void
        iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
        {
            //concept requirements...
            swap(*__a, *__b);
        }
    this might speed up sorting strings in particular (might become comparable to the set).

    The problem is that the existing implementation failed to take advantage of overloaded swap functions and creates lots of temporaries, just to swap two strings:

    Code:
      template<typename _ForwardIterator1, typename _ForwardIterator2>
        inline void
        iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
        {
            typedef typename iterator_traits<_ForwardIterator1>::value_type
                _ValueType1;
            //concept requirements...
            const _ValueType1 __tmp = *__a;
            *__a = *__b;
            *__b = __tmp;
        }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

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