Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7007027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:33:14+00:00 2026-05-27T21:33:14+00:00

I am trying to parse a c++ string on every ‘^’ character into a

  • 0

I am trying to parse a c++ string on every ‘^’ character into a vector tokens. I have always used the boost::split method, but I am now writing performance critical code and would like to know which one gives better performance.

For example:

string message = "A^B^C^D";
vector<string> tokens;
boost::split(tokens, message, boost::is_any_of("^"));

vs.

boost::char_separator<char> sep("^");
boost::tokenizer<boost::char_separator<char> > tokens(text, sep);

Which one would give better performance and why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:33:15+00:00Added an answer on May 27, 2026 at 9:33 pm

    The best choice depends on a few factors. If you’re only needing to scan the tokens once, then the boost::tokenizer is a good choice in both runtime and space performance (those vectors of tokens can take up a lot of space, depending on input data.)

    If you’re going to be scanning the tokens often, or need a vector with efficient random access, then the boost::split into a vector may be the better option.

    For example, in your “A^B^C^…^Z” input string where the tokens are 1-byte in length, the boost::split/vector<string> method will consume at least 2*N-1 bytes. With the way strings are stored in most STL implementations you can figure it taking more than 8x that count. Storing these strings in a vector is costly in terms of memory and time.

    I ran a quick test on my machine and a similar pattern with 10 million tokens looked like this:

    • boost::split = 2.5s and ~620MB
    • boost::tokenizer = 0.9s and 0MB

    If you’re just doing a one-time scan of the tokens, then clearly the tokenizer is better.
    But, if you’re shredding into a structure that you want to reuse during the lifetime of your application, then having a vector of tokens may be preferred.

    If you want to go the vector route, then I’d recommend not using a vector<string>, but a vector of string::iterators instead. Just shred into a pair of iterators and keep around your big string of tokens for reference. For example:

    using namespace std;
    vector<pair<string::const_iterator,string::const_iterator> > tokens;
    boost::split(tokens, s, boost::is_any_of("^"));
    for(auto beg=tokens.begin(); beg!=tokens.end();++beg){
       cout << string(beg->first,beg->second) << endl;
    }
    

    This improved version takes 1.6s and 390MB on the same server and test. And, best of all the memory overhead of this vector is linear with the number of tokens — not dependent in any way on the length of tokens, whereas a std::vector<string> stores each token.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse a timestamp into a human-readable date string, however, I
I am trying to delete every character from the beginning of my string, that
Im trying to parse the string located in /proc/stat in a linux filesystem using
I'm trying to parse a string that was generated by an NSDateFormatter using another
I'm trying to parse a string in a specific format and I'm really surprised
I am trying to parse a string of HTML tag attributes in php. There
Trying to parse an SQL string and pull out the parameters. Ex: select *
I'm trying to parse a simple string in C++. I know the string contains
I'm trying to parse this XML string: <?xml version=1.0 encoding=UTF-8 standalone=no?> <response type=success> <lots>
I'm trying to parse an international datetime string similar to: 24-okt-08 21:09:06 CEST So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.