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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:18:28+00:00 2026-06-08T08:18:28+00:00

I have a question concerning the difference between a normal C++ string and a

  • 0

I have a question concerning the difference between a “normal” C++ string and a string of unsigned characters.
When generating some pseudorandom strings of chars and of unsigned chars, I noticed a huge performance difference between the time the code would need to build a normal string and one composed of unsigned chars.

The code I used:

#include <tr1/random>
#include <string>


using namespace std;
using namespace tr1;

typedef basic_string<unsigned char > ustring;

string generateString(){
    string retStr;
    char a;

    for(unsigned int i = 0; i < 1000; i++){             
        a = rand();
        retStr+=a;            
    }
    return retStr;
}

ustring generateUString(){
    ustring retStr;
    unsigned char a;

    for(unsigned int i = 0; i < 1000; i++){
        a = rand();
        retStr+=a;
    }
    return retStr;
}


int main(int argc, char* args[]){

    srand(0);

    string thing;
    ustring uthing;

    for(unsigned int i = 1; i< 100000; i++){ 
        //thing = generateString(); // this needs 2 second to execute
        uthing = generateUString(); //  and this 13

    }

    return 0;
}

So basically, the code needs 2 seconds to execute generateString() 100 000 times, while it needs 13 seconds to execute generateUString() 100 000 times.

What exactly is the reason for this? I guess it’s the += operator, since the difference melted away when I cut the corresponding lines (actually, generateUstring() seems to be faster then, I guess because the modulo arithmetic is easier in that case).

But why then is it so much faster to append a char to a string than to append a unsigned char to a string of unsigned chars? And should I hence avoid strings of unsigned chars?

  • 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-08T08:18:30+00:00Added an answer on June 8, 2026 at 8:18 am

    The reason is probably that basic_string<char> is explicitly instantiated in libstdc++.so which (by default) is compiled with -O2. So if you don’t compile your program with optimization the basic_string<unsigned char> operations will be un-optimized, but all the basic_string<char> operations that aren’t inlined will use the optimized code in libstdc++.so.

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

Sidebar

Related Questions

i have a small question concerning away3D. I have some cubes with the exact
I have question concerning a function I created. I would like to show the
I have a question concerning active record association, referring to this part of the
I have a question concerning subtypes of built-in types and their constructors. I want
I have a question concerning Core Data and how, if at all, Entities get
I have a question concerning number conversion i JS. I have a number like
I have a simple question concerning Javascript. I am trying to print in a
I have a strange question concerning subroutines: As I'm creating a minimal language and
I have a really basic question concerning unix timestamp and mysql date. I'm trying
I have a question about the singleton pattern. I saw two cases concerning 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.