Performance analysis of trimming a string and then returning it vs returning substr of the string
up vote
-5
down vote
favorite
I have a very long string which I want my function to return, but I want my function to return the string WITHOUT the first character. So I've come up with 2 ways to do this:
A) Erase the first character of str, then return str. This is done in a total of 2 statements.
B) Return str.substr(1). This is done in a single statement.
Which one of these 2 is speed wise more efficient, and why exactly would one be faster than the other? Also, is there an even more efficient method for the same task?
c++ performance
|
show 1 more comment
up vote
-5
down vote
favorite
I have a very long string which I want my function to return, but I want my function to return the string WITHOUT the first character. So I've come up with 2 ways to do this:
A) Erase the first character of str, then return str. This is done in a total of 2 statements.
B) Return str.substr(1). This is done in a single statement.
Which one of these 2 is speed wise more efficient, and why exactly would one be faster than the other? Also, is there an even more efficient method for the same task?
c++ performance
If you are concerned about performance of an operation, measure it yourself.
– Neil Butterworth
Nov 13 at 22:36
There is a 'why' in there, read it yourself
– Emirhan G.
Nov 13 at 22:37
1
Why does your function even store the first character (and not skip it in the course of generating/reading in the string)?
– Stephan Lechner
Nov 13 at 22:40
1
Have you looked atstd::string_view?
– Paul Sanders
Nov 13 at 22:41
1
The two comments above are probably much faster ways to do this, but in general when faced with multiple options and speed matters, try them all and profile. While it almost always makes perfect sense after you figure it out, you will sometimes find yourself surprised by what turned out to be faster. This is a classic example.
– user4581301
Nov 13 at 22:46
|
show 1 more comment
up vote
-5
down vote
favorite
up vote
-5
down vote
favorite
I have a very long string which I want my function to return, but I want my function to return the string WITHOUT the first character. So I've come up with 2 ways to do this:
A) Erase the first character of str, then return str. This is done in a total of 2 statements.
B) Return str.substr(1). This is done in a single statement.
Which one of these 2 is speed wise more efficient, and why exactly would one be faster than the other? Also, is there an even more efficient method for the same task?
c++ performance
I have a very long string which I want my function to return, but I want my function to return the string WITHOUT the first character. So I've come up with 2 ways to do this:
A) Erase the first character of str, then return str. This is done in a total of 2 statements.
B) Return str.substr(1). This is done in a single statement.
Which one of these 2 is speed wise more efficient, and why exactly would one be faster than the other? Also, is there an even more efficient method for the same task?
c++ performance
c++ performance
asked Nov 13 at 22:35
Emirhan G.
15
15
If you are concerned about performance of an operation, measure it yourself.
– Neil Butterworth
Nov 13 at 22:36
There is a 'why' in there, read it yourself
– Emirhan G.
Nov 13 at 22:37
1
Why does your function even store the first character (and not skip it in the course of generating/reading in the string)?
– Stephan Lechner
Nov 13 at 22:40
1
Have you looked atstd::string_view?
– Paul Sanders
Nov 13 at 22:41
1
The two comments above are probably much faster ways to do this, but in general when faced with multiple options and speed matters, try them all and profile. While it almost always makes perfect sense after you figure it out, you will sometimes find yourself surprised by what turned out to be faster. This is a classic example.
– user4581301
Nov 13 at 22:46
|
show 1 more comment
If you are concerned about performance of an operation, measure it yourself.
– Neil Butterworth
Nov 13 at 22:36
There is a 'why' in there, read it yourself
– Emirhan G.
Nov 13 at 22:37
1
Why does your function even store the first character (and not skip it in the course of generating/reading in the string)?
– Stephan Lechner
Nov 13 at 22:40
1
Have you looked atstd::string_view?
– Paul Sanders
Nov 13 at 22:41
1
The two comments above are probably much faster ways to do this, but in general when faced with multiple options and speed matters, try them all and profile. While it almost always makes perfect sense after you figure it out, you will sometimes find yourself surprised by what turned out to be faster. This is a classic example.
– user4581301
Nov 13 at 22:46
If you are concerned about performance of an operation, measure it yourself.
– Neil Butterworth
Nov 13 at 22:36
If you are concerned about performance of an operation, measure it yourself.
– Neil Butterworth
Nov 13 at 22:36
There is a 'why' in there, read it yourself
– Emirhan G.
Nov 13 at 22:37
There is a 'why' in there, read it yourself
– Emirhan G.
Nov 13 at 22:37
1
1
Why does your function even store the first character (and not skip it in the course of generating/reading in the string)?
– Stephan Lechner
Nov 13 at 22:40
Why does your function even store the first character (and not skip it in the course of generating/reading in the string)?
– Stephan Lechner
Nov 13 at 22:40
1
1
Have you looked at
std::string_view?– Paul Sanders
Nov 13 at 22:41
Have you looked at
std::string_view?– Paul Sanders
Nov 13 at 22:41
1
1
The two comments above are probably much faster ways to do this, but in general when faced with multiple options and speed matters, try them all and profile. While it almost always makes perfect sense after you figure it out, you will sometimes find yourself surprised by what turned out to be faster. This is a classic example.
– user4581301
Nov 13 at 22:46
The two comments above are probably much faster ways to do this, but in general when faced with multiple options and speed matters, try them all and profile. While it almost always makes perfect sense after you figure it out, you will sometimes find yourself surprised by what turned out to be faster. This is a classic example.
– user4581301
Nov 13 at 22:46
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290535%2fperformance-analysis-of-trimming-a-string-and-then-returning-it-vs-returning-sub%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If you are concerned about performance of an operation, measure it yourself.
– Neil Butterworth
Nov 13 at 22:36
There is a 'why' in there, read it yourself
– Emirhan G.
Nov 13 at 22:37
1
Why does your function even store the first character (and not skip it in the course of generating/reading in the string)?
– Stephan Lechner
Nov 13 at 22:40
1
Have you looked at
std::string_view?– Paul Sanders
Nov 13 at 22:41
1
The two comments above are probably much faster ways to do this, but in general when faced with multiple options and speed matters, try them all and profile. While it almost always makes perfect sense after you figure it out, you will sometimes find yourself surprised by what turned out to be faster. This is a classic example.
– user4581301
Nov 13 at 22:46