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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:26:20+00:00 2026-06-06T13:26:20+00:00

Question Is it true that C-style strings operations, on average, execute 5 times slower

  • 0

Question

Is it true that C-style strings operations, on average, execute 5 times slower than library string class operations, as C++ Primer, 4th Edition would have me believe?

Why ask?

Because when I actually performance test, it turns out that C-style strings are about 50% faster for a particular example (one used in the book).


Setup

I am reading C++ Primer, 4th Edition, which (on page 138) lists this code:

//  C-style character string implementation
const char *pc = "a very long literal string";
const size_t  len = strlen(pc +1);    //  space to allocate

//  performance test on string allocation and copy
for (size_t ix = 0; ix != 1000000; ++ix) {
    char *pc2 = new char[len + 1];  //  allocate the space
    strcpy(pc2, pc);                //  do the copy
    if (strcmp(pc2, pc))            //  use the new string
        ;    //  do nothing
    delete [] pc2;                  //  free the memory
}

//  string implementation
string str("a very long literal string");

//  performance test on string allocation and copy
for(int ix = 0; ix != 1000000; ++ix) {
    string str2 = str;  //  do the copy, automatically allocated
    if (str != str2)    //  use the new string
        ;   //  do nothing
}    //  str2 is automatically freed

Now bear in mind that I’m aware of that strlen(pc +1) on line 2, and that the first for uses size_t but doesn’t subscript the array so it might as well have been int, but this is exactly how it is written down in the book.

When I test this code (with strlen(pc) + 1, which I presume was intended), my results are that the first block executes about 50% faster than the second block, which leads to conclusion that C-style strings are faster than library string class for this particular example.

However, I bet I’m missing something (probably obvious), because of what is written in the book (page 139) relating to the code above:

As it happens, on average, the string class implementation executes
considerably faster than the C-style string functions. The relative
average execution times on our more than five-year-old PC are as
follows:

 user    0.47  # string class 
 user    2.55  # C-style character string

So which one is it? Should I have used a longer string literal? Maybe it was because they used the GNU C Compiler and I used the Microsoft one? Is it because I have a faster computer?

Or is the book just wrong on this one?

Edit

Microsoft (R) 32-bit C/C++ Optimizing Compiler version 16.00.40219.01 for 80×86

  • 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-06T13:26:21+00:00Added an answer on June 6, 2026 at 1:26 pm

    Your conclusion that C style strings are faster with this example with your compiler & machine, is almost certainly because – one must presume – you

    • forgot to turn on optimization,
    • forgot to make the string length “unknown” to the compiler (this is tricky) so as to prevent it from optmizing away strlen calls, and
    • forgot and turn off safety range checking (if applicable) which would slow down std::string.

    Here’s the code I tested with:

    #include <assert.h>
    #include <iostream>
    #include <time.h>
    #include <string>
    #include <string.h>
    using namespace std;
    
    extern void doNothing( char const* );
    
    class StopWatch
    {
    private:
        clock_t     start_;
        clock_t     end_;
        bool        isRunning_;
    public:
        void start()
        {
            assert( !isRunning_ );
            start_ = clock();
            end_ = 0;
            isRunning_ = true;
        }
    
        void stop()
        {
            if( isRunning_ )
            {
                end_ = clock();
                isRunning_ = false;
            }
        }
    
        double seconds() const
        {
            return double( end_ - start_ )/CLOCKS_PER_SEC;
        }
    
        StopWatch(): start_(), end_(), isRunning_() {}
    };
    
    inline void testCStr( int const argc, char const* const argv0 )
    {
        //  C-style character string implementation
        //const char *pc = "a very long literal string";
        const char *pc = (argc == 10000? argv0 : "a very long literal string");
        //const size_t  len = strlen(pc +1);    //  space to allocate
        const size_t  len = strlen(pc)+1;    //  space to allocate
    
        //  performance test on string allocation and copy
        for (size_t ix = 0; ix != 1000000; ++ix) {
            char *pc2 = new char[len + 1];  //  allocate the space
            strcpy(pc2, pc);                //  do the copy
            if (strcmp(pc2, pc))            //  use the new string
                //;   //  do nothing
                doNothing( pc2 );
            delete [] pc2;                  //  free the memory
        }
    }
    
    inline void testCppStr( int const argc, char const* const argv0 )
    {
        //  string implementation
        //string str("a very long literal string");
        string str( argc == 10000? argv0 : "a very long literal string" );
    
        //  performance test on string allocation and copy
        for(int ix = 0; ix != 1000000; ++ix) {
            string str2 = str;  //  do the copy, automatically allocated
            if (str != str2)    //  use the new string
                //;   //  do nothing
                doNothing( &str2[0] );
        }    //  str2 is automatically freed
    }
    
    int main( int argc, char* argv[] )
    {
        StopWatch   timer;
    
        timer.start();  testCStr( argc, argv[0] );  timer.stop();
        cout << "C strings: " << timer.seconds() << " seconds." << endl;
    
        timer.start();  testCppStr( argc, argv[0] );  timer.stop();
        cout << "C++ strings: " << timer.seconds() << " seconds." << endl;
    }
    

    Typical result:

    [d:\dev\test]
    > g++ foo.cpp doNothing.cpp -O2
    
    [d:\dev\test]
    > a
    C strings: 0.417 seconds.
    C++ strings: 0.084 seconds.
    
    [d:\dev\test]
    > a
    C strings: 0.398 seconds.
    C++ strings: 0.082 seconds.
    
    [d:\dev\test]
    > a
    C strings: 0.4 seconds.
    C++ strings: 0.083 seconds.
    
    [d:\dev\test]
    > _
    

    The said, C++ strings are not generally the fastest possible implementation of strings.

    Generally, immutable strings (reference counted) beat C++ strings by a good margin, and, surprising to me when I learned that, a string implementation that simply copies the string data is faster still, when it uses an appropriate, fast custom allocator. However, don’t ask me how to implement the latter. I only saw the code and test results in another forum, which someone graciously provided after I’d pointed out the general superiority of immutable strings in a discussion with STL and there was some disagreement. 😉

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

Sidebar

Related Questions

The string in question (read from a file): if (true) then { _this =
Rails Newb here asking his first question.... I'm building a personal blog-style website that
everybody! I know, that probably the question was asked many times, but anyway.. The
Question about GridView sorting in VB.NET: I have a GridView with AutoGenerateColumns = True
I have a simple question: if I use URLConnection.setUseCaches(true) when loading something from web,
This is somehow related to my question Why is ''>0 True in Python? In
I have a simple question about the barplot function. I have a TRUE/FALSE table
Question about subclassing in matlab, under the new class system. I've got class A
Question 1: Is is possible to throw an exception that will not be caught
[Note: This question had the original title " C (ish) style union in C#

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.