0

I was wondering wether sstream is faster than just a for loop for processing a string? say for example we have a string that we can't to separate just the words:

std::string somestring = "My dear aunt sally went to the market and couldn't find what she was looking for";

would a string stream be faster? Its definitely prettier.

std::string temp;
stringstream input(somestring);
while(input >> temp){
  std::cout << temp;
}

or utilizing a regular old for loop and a buffer?

std::string buffer; //word buffer
    for(int i= 0; i < somestring.size(); ++i){
        if(somestring.at(i) == 32 && buffer == ""){
            continue;
        }
        if(somestring.at(i) == 32 || somestring.at(i) == '\n'){
            std::cout << buffer;
            buffer.clear();
            continue;
        }
        buffer += somestring.at(i);
    }
    std::cout << buffer;

tldr: I guess i'm mainly wondering if sstream is more optimized and faster at processing a string than just a regular for loop?

EDIT: Edited the forloop

jrubix
  • 67
  • 1
  • 8
  • ***sstream vs for loop speed for processing a string*** It probably does not make much of a difference for an optimized / release build. – drescherjm Feb 24 '21 at 19:31
  • You could time each with the same string. Make sure the string is at least a few KB. – drescherjm Feb 24 '21 at 19:34
  • What is your actual end goal? Knowing that, we should be able to help you to get there. – NathanOliver Feb 24 '21 at 19:35
  • This can't really be answered - it depends too much on compilers, systems, and the real problem being solved. With anything performance related, you generally have to measure it yourself. I would use the cleanest code that works until you measure a performance problem. – Dean Johnson Feb 24 '21 at 19:36
  • 2
    http://quick-bench.com/ can help you answer these kinds of questions. – 0x5453 Feb 24 '21 at 20:08
  • @MooingDuck what the string stream or loop? both run as desired? – jrubix Feb 24 '21 at 21:08
  • 2
    https://quick-bench.com/q/c6_rhFAXrGCgsX-c9CYhIVNqk0w shows the loop to be faster (assuming I did it right) – Jerry Jeremiah Feb 24 '21 at 21:09
  • 2
    @jrubix They both loop, but the two have different outputs for the same inputs sometimes, which means one has a bug. This is generally why we say to use standard libraries, because they are known to work. Code you write yourself is _far_ more likely to contain a bug. – Mooing Duck Feb 24 '21 at 21:28
  • @JerryJeremiah oh interesting! thank you! I was having trouble figuring out how to set it up – jrubix Feb 24 '21 at 21:28
  • @MooingDuck ah I see – jrubix Feb 24 '21 at 21:29
  • @jrubix http://coliru.stacked-crooked.com/a/5ffbee5f56895b84 Sure, you can fix the bugs that you find, but you'll almost always miss some. The standard library will always have fewer bugs. Performance doesn't matter when the code is wrong. – Mooing Duck Feb 24 '21 at 21:30

1 Answers1

1

You tagged your question with performance, but you didn't specify the details of "processing a string".

Do you need to copy the words to a different storage? Or just to identify them? In both of your examples, copying will take most of the time.

Needless to say, you should not have std::cout in performance-critical code :)

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27