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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:38:29+00:00 2026-06-10T08:38:29+00:00

TL/DR : How can a large std::vector<std::string> achieve such fast deallocation when compiled with

  • 0

TL/DR: How can a large std::vector<std::string> achieve such fast deallocation when compiled with release settings on Visual Studio 2012 RC?

I have written a class strung that behaves similarly to std::string as an exercise, implementing basic copy- and move semantics.

class strung
{
private:
    size_t length_;
    char* data_;

public:
    // -------- Constructors --------

    strung() : length_(0), data_(nullptr) {};

    strung(const char* c_str)
    {
        length_ = strlen(c_str);
        data_ = new char[length_];
        ::std::copy(c_str, c_str + length_, data_);
    };

    inline explicit strung(size_t length) : length_(length)
    {
        data_ = new char[length_];
    };

    strung(size_t length, char value) : length_(length)
    {
        data_ = new char[length_];
        ::std::fill(data_, data_ + length_, value);
    };

    // -------- Copy/move-constructors --------

    strung(const strung& old)
    {
        data_ = new char[old.length_];
        ::std::copy(old.data_, old.data_ + old.length_, data_);
        length_ = old.length_;
    };

    strung(strung&& old)
    {
        data_ = old.data_;
        length_ = old.length_;
        // Even though it is a rvalue, its destructor will still be called,
        // so we would like to prevent our data from being freed.
        old.data_ = nullptr;
    };

    // -------- Assignment operators --------

    inline strung & operator =(const strung& old)
    {
        if (this != &old)
        {
            delete[] data_;
            data_ = new char[old.length_];
            ::std::copy(old.data_, old.data_ + old.length_, data_);
            length_ = old.length_;
        }
        return *this;
    };

    strung & operator =(strung&& old)
    {
        if (this != &old)
        {
            delete[] data_;
            data_ = old.data_;
            length_ = old.length_;
            old.data_ = nullptr;
        }
        return *this;
    };

    // -------- Array operators (no bounds checking by design) --------

    inline char& operator[](size_t pos)
    {
        return data_[pos];
    };

    inline const char& operator[](size_t pos) const
    {
        return data_[pos];
    };

    // -------- Insertion operator for `ostream`s --------

    inline friend ::std::ostream &operator<<(::std::ostream &out, const strung& source)
    {
        out.write(source.data_, source.length_);
        return out;
    };

    // -------- Various functions --------

    inline const size_t length() const
    {
        return length_;
    }

    // -------- Poor man's iterators --------

    char* begin()
    {
        return data_;
    };

    char* end()
    {
        return data_ + length_;
    };

    // -------- Destructor --------

    inline ~strung()
    {
        delete[] data_;
    };
}; 

I tried comparing the performance of std::string and strung using this code:

double time(const std::function<void(void)> &func)
{
    using namespace std::chrono;
    auto t1 = high_resolution_clock::now(); 
    func();
    auto total = duration_cast<nanoseconds>(high_resolution_clock::now()-t1);
    return static_cast<double>(total.count()) / 1000000.;
}

template<typename T>
void test(const int num)
{
    double allocation_time, full_time;

    full_time = time([&] {
        std::vector<T> container;

        allocation_time = time([&] {
            container.reserve(num);

            for (int i=0; i < num; i++)
            {
                container.emplace_back(rand() % 10 + 1,'\0');

                for (char &chr : container.back())
                    chr = ('A' + rand() % ('Z' - 'A' + 1) );
            }
        });
    });

    std::cout << "Full time: " <<  full_time << " miliseconds" << std::endl 
        << "Allocation time: " << allocation_time << " miliseconds" << std::endl 
        << "Deallocation time: " << full_time - allocation_time << " miliseconds" << std::endl;
}

int main()
{
    std::cout << "-------- std::string --------" << std::endl;
    test<std::string>(500000);
    std::cout << "-------- strung --------" << std::endl;
    test<strung>(500000);
    return EXIT_SUCCESS;
}

And these were the results:

Debug (x86-64)

-------- std::string --------
Full time: 51050.9 miliseconds
Allocation time: 1853.11 miliseconds
Deallocation time: 49197.8 miliseconds
-------- strung --------
Full time: 52404 miliseconds
Allocation time: 4886.28 miliseconds
Deallocation time: 47517.7 miliseconds

Release (x86-64):

-------- std::string --------
Full time: 113.007 miliseconds
Allocation time: 107.006 miliseconds
Deallocation time: 6.0004 miliseconds
-------- strung --------
Full time: 47771.7 miliseconds
Allocation time: 356.02 miliseconds
Deallocation time: 47415.7 miliseconds

Allocation speeds are understandable, since I didn’t really do much optimization on the class, but deallocation speeds are more intriguing.

Testing on Debug settings indicates that deallocation is similarly complex for both std::string and strung (though still very slow), but testing on Release settings makes deallocation for std::string very very fast, while strung stays exactly the same. What does std::string do to achieve such fast deallocation, considering that strung‘s destructor is almost trivial.

At first I thought that std::string is optimized into a nop, so deallocation is not performed at all, but when I removed strung‘s destructor, the latter was still much faster, so this is probably not a case.

I would like my deallocation to be fast, so what am I to do achieve similar deallocation speeds?

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

    Microsoft’s std::string implementation uses something called “small string optimization”. What this means is that std::string actually contains a 15-character string (a char[16]). If it is given a string shorter than 16 characters, then it stores it in that internal memory. So there is no dynamic memory allocation done in these cases.

    Your strung always dynamically allocates the string. Which means that its destructor will always deallocate it. std::string, if small enough, will do neither.

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

Sidebar

Related Questions

header.h #include <iostream> #include <vector> class CombatLine{ std::stringstream Line; std::vector<std::string> TokenLine; void SetLine(std::string s){
Background I have a container class which uses vector<std::string> internally. I have provided a
I have a WCF service that can return large amount of data depending on
I have a form with a textarea that can contain large amounts of content
Im currently working on a site that you can upload large files.. on local
I have a server application that, in rare occasions, can allocate large chunks of
I have situation where a user can manipulate a large set of data (presented
I need a c# number something that can handle very large numbers but also
I have a multi-line text view that can get quite large. When the user
I'm looking to create an interface where the user can navigate through large volumes

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.