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 8212569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:48:05+00:00 2026-06-07T10:48:05+00:00

Recently we encountered a performance problem from a piece of code that generates an

  • 0

Recently we encountered a performance problem from a piece of code that generates an XML. Thought of sharing the experience here. This is slightly long, please bear with me.

We prepare a simple XML with a number of items. Each item can have 5-10 elements. The structure is something like this:

<Root>
    <Item>
        <Element1Key>Element1Val</Element1Key>
        <Element2Key>Element2Val</Element2Key>
        <Element3Key>Element3Val</Element3Key>
        <Element4Key>Element4Val</Element4Key>
        <Element5Key>Element5Val</Element5Key>
    <Item>
    <Item>
        <Element1Key>Element1Val</Element1Key>
        <Element2Key>Element2Val</Element2Key>
        <Element3Key>Element3Val</Element3Key>
        <Element4Key>Element4Val</Element4Key>
        <Element5Key>Element5Val</Element5Key>
    <Item>
</Root>

The code that generates the XML was (in simplified form as global functions) :

void addElement(std::string& aStr_inout, const std::string& aKey_in, const std::string& aValue_in)
{
    aStr_inout += "<";
    aStr_inout += aKey_in;
    aStr_inout += ">";
    aStr_inout += "Elemem1Val";
    aStr_inout += "<";
    aStr_inout += aValue_in;
    aStr_inout += ">";
}

void PrepareXML_Original()
{
    clock_t commence,complete;
    commence=clock();

    std::string anXMLString;
    anXMLString += "<Root>";

    for(int i = 0; i < 200; i++)
    {
        anXMLString += "<Item>";
        addElement(anXMLString, "Elemem1Key", "Elemem1Value");
        addElement(anXMLString, "Elemem2Key", "Elemem2Value");
        addElement(anXMLString, "Elemem3Key", "Elemem3Value");
        addElement(anXMLString, "Elemem4Key", "Elemem4Value");
        addElement(anXMLString, "Elemem5Key", "Elemem5Value");
        anXMLString += "</Item>";


        replaceAll(anXMLString, "&", "&amp;");
        replaceAll(anXMLString, "'", "&apos;");
        replaceAll(anXMLString, "\"", "&quot;");
        replaceAll(anXMLString, "<", "&lt;");
        replaceAll(anXMLString, ">", "&gt;");
    }
    anXMLString += "</Root>";

    complete=clock();
    LONG lTime=(complete-commence);
    std::cout << "Time taken for the operation is :"<< lTime << std::endl;
}

The replaceAll() code will replace the special characters with the encoded form. This is given below.

void replaceAll(std::string& str, const std::string& from, const std::string& to) 
{
    size_t start_pos = 0;
    while((start_pos = str.find(from, start_pos)) != std::string::npos) 
    {
        str.replace(start_pos, from.length(), to);
        start_pos += to.length();
    }
}

In the minimal example, I have encoded 200 items. But, in the actual situation this could be more. The above code took around 20 seconds to create the XML. This was far beyond any acceptable limit. What could be the problem? And how to improve the performance here?

Note : The usage of the string class doesn’t make much difference. I tested same logic with another string implementation from MFC CString and I got the similar(much worse) observation. Also, I don’t want to use any DOM XML parsers here to prepare the XML in a better way. The question is not specific to XML.

  • 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-07T10:48:08+00:00Added an answer on June 7, 2026 at 10:48 am

    If you can estimate length of of result string (anXMLString) before creation of content, then you could allocate enough buffer space for the string.
    When the buffer is big enough, then re-allocation and copying (of target string) won’t happen.

    This way:

    std::string anXMLString;
    anXMLString.reserve( size );
    

    I’m not sure about std::string, does it need to search appending point, or does in keep length of string in memory.

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

Sidebar

Related Questions

I recently encountered some code that gcc would not compile without this arg. I
I recently encountered a situation in some code I am working on that doesn't
I've recently encountered what I think is a false-sharing problem in my application, and
I have recently encountered an issue that is related to code running in the
I've recently encountered a problem, and despite coming across similar questions here alreadt, none
Recently I've encountered a problem. I have an app that talks to the server.
I recently encountered this problem. I found many instances of people asking the question—
Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux.
I recently encountered a problem with my Profile provider: it wouldn't retrieve profiles correctly
I recently encountered an index in a database I maintain that was of the

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.