2

as suggested by this post: Split a string in C++?

there are many ways to split a string, but whose performance is best ?

is there any benchmark on this test ?

Community
  • 1
  • 1
  • What do you mean by "split a string"? Do you need two separate C strings? Do you need two separate string objects? If not, just use pointers to the beginning and (one-past-the-)end of each segment. – James McNellis Feb 20 '12 at 04:12
  • 4
    Did you have to rhyme? Could you not let it rest? – user541686 Feb 20 '12 at 04:12
  • 2
    Answering this question is an interesting quest! – Aman Aggarwal Feb 20 '12 at 04:19
  • 4
    Use your own data to test. Probably simplest code is best. – John Zwinck Feb 20 '12 at 04:26
  • Sorry 'bout that, I digressed... I hope people don't protest >_ – user541686 Feb 20 '12 at 05:38
  • ¤ In the end you will just have to **MEASURE**. Oops, last time I said that a silly moderator deleted the answer (perhaps he disagreed, in which case he shouldn't delete but post a comment or his own answer, or, well, it makes no sense but I think I know which moderator that was). That's the main gist of it. However, to maximize your chance of a Very Fast Implementation™ you should use a substring creation operation that has *constant time*. One way to do that is to ensure that the original string needs to exist and not be modified over the lifetime of the substring references. Cheers & hth., – Cheers and hth. - Alf Feb 20 '12 at 07:54

1 Answers1

3

To maximize your chance of a Very Fast Implementation™ you should use a substring creation operation that has constant time. One way to do that is to ensure that the original string exists and is not modified over the lifetime of the substring references. You can then represent each substring as e.g. two pointers, or as a pointer and a length, or as whatever suits the particular context.

For an unusual context example that allows a perhaps surprising substring representation, when the original string is discardable and the substrings can be C style zero-terminated strings,then you can replace substring delimiters in the original string with null bytes, and then a substring can be represented as e.g. a single pointer.

Anyway, in the end you will just have to MEASURE.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331