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?










share|improve this question






















  • 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















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?










share|improve this question






















  • 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













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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


















  • 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
















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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?