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

  • Home
  • SEARCH
  • 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 8748657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:33:51+00:00 2026-06-13T12:33:51+00:00

Someone want to explain me which code is faster ? and what is the

  • 0

Someone want to explain me which code is faster ? and what is the best way to optimise a string assignment ?

code 1 :

std::string result;

int main()
{
   for(int i=0;i<1000;i++)
   {
   /*some code*/
   result = stringVar;
   /* some code using result */
   }
}

code 2 :

int main()
{
   for(int i=0;i<1000;i++)
   {
   /*some code*/
   std::string result = stringVar;
   /* some code using result */
   }
}

And to assigne value :

std::string var;

var.assign("value");
//or
var="value";

And it’s possible to release memory used by the value before to add a new one ?

Thank if you can help me to understand that 🙂

  • 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-06-13T12:33:52+00:00Added an answer on June 13, 2026 at 12:33 pm

    In the case of:

    for (...)
        std::string result = blah;
    

    the compiler must construct and destruct result each time through the for loop, probably requiring heap allocation and deallocation calls.

    In the case of:

    std::string result;
    for (...)
        result = blah;
    

    the string implementation might be able to optimize some heap allocation and deallocation away by only reallocation when blah is to big to fit in result’s current buffer.

    var=x and var.assign(x) should result in the same code; I would not expect a substantial difference either way.

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

Sidebar

Related Questions

Could someone please explain when would I want to use delegation instead of inheritance?
Let me explain. Suppose I want to teach Python to someone who only speaks
i want someone to include a JS file of me which does something neat.
Can someone explain how to create a .php page which auto-updates with values from
I want someone to explain some points in BlausC's amazing answer in this question
I have the following code which does (the first part of) what I want
I want to match a separate word which starts with # character. enter #code
For our assignment we need to write code for a neural network. The way
The code dynamically creates a listview which works but i want to make it
I am making a small blog for someone and they want it in 2

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.