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

Discussion in 'C Programming' started by petry, Jul 5, 2009.

  1. petry

    petry

    Joined:
    Jul 5, 2009
    Messages:
    2
    Hi All:

    I have a large number of elements to be stored into vector or basic string(delimited by \n). I need to search for an element and I would like to know what is faster:

    A:
    Code:
    std::string str = "\nElement1\nElement2...element10000\n"
    str.find("\nElement3\n");
    B:
    Code:
    std::vector<string> v;
    v.push_back("Element1"); v.push_back("Element2");  ....
    std::find(v.begin(), v.end(), "Element3") != v.end() )
    I'm only interested in search time, I don't need to access any of the elements for reading, modifying or removing.

    Thanks a lot,
    Petry
    petry, Jul 5, 2009
    #1
    1. Advertising

  2. petry

    petry

    Joined:
    Jul 5, 2009
    Messages:
    2
    I have just tested the difference.

    Filesize: 8mb
    Lines: 700.000
    Average line lenght: 15 characters

    Time to fill:
    vector: 3843ms
    string: 3891ms

    The search time for the last unique element is:
    vector: 218
    string: 172
    petry, Jul 5, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias =?ISO-8859-1?Q?K=E4ppler?=

    std::string::push_back vs. std::string::operator+=

    Matthias =?ISO-8859-1?Q?K=E4ppler?=, Nov 22, 2004, in forum: C++
    Replies:
    2
    Views:
    4,140
    Jonathan Mcdougall
    Nov 23, 2004
  2. Peter Jansson
    Replies:
    5
    Views:
    6,305
    Ivan Vecerina
    Mar 17, 2005
  3. Jim Langston

    std::string = char* + std::string

    Jim Langston, Jun 30, 2005, in forum: C++
    Replies:
    9
    Views:
    376
    Jim Langston
    Jul 2, 2005
  4. Fei Liu
    Replies:
    9
    Views:
    444
    richard@ex-parrot.com
    Apr 1, 2006
  5. Jeffrey Walton
    Replies:
    10
    Views:
    938
    Mathias Gaunard
    Nov 26, 2006
Loading...

Share This Page