Jump to content

C++ Vector Speeds


SpiderPig
 Share

Recommended Posts

A quick example of what's fast and what is not with vectors.  I wanted to test what was the fastest; push_back() vs emplace_back() and erase from the beginning or erase from the end.  I think its obvious why removing an element from the end is better from the beginning as I believe there is no shuffling of data around when you erase from the end.  As I found out recently the capacity doesn't actually change when you use erase.  But that's a good thing.  I don't fully understand emplace-back(), something to do with not creating intermediate data before adding to the container?  I wonder if there's a faster why still...  I love speeding up code.

VectorSpeeds.png.34ccb667a6899ba09a8cb288a28abf8a.png

#include "UltraEngine.h"
#include "ComponentSystem.h"

using namespace UltraEngine;

struct TestObject : public Object {
    bool enabled = true;
};

int main(int argc, const char* argv[])
{
    auto sample_size = 10;

    auto start_time = std::chrono::high_resolution_clock::now();
    auto size = 10000;

    {
        long long create_time = 0, erase_time = 0;
        for (int i = 0; i < sample_size; i++) {
            vector<shared_ptr<TestObject>> objects;
            objects.reserve(size);
            for (int i = 0; i < size; i++) {
                objects.push_back(make_shared<TestObject>());
            }
            create_time += (std::chrono::high_resolution_clock::now() - start_time).count();

            start_time = std::chrono::high_resolution_clock::now();
            while (objects.size() != 0) {
                auto obj = objects[0];

                objects.erase(objects.begin());
            }
            erase_time += (std::chrono::high_resolution_clock::now() - start_time).count();

        }

        Print("#1 : Create Time : " + String(((double)create_time / (double)sample_size) / 1000000.0) + " (ms)");
        Print("#1 : Erase Time : " + String(((double)erase_time / (double)sample_size) / 1000000.0) + " (ms)");
    }

    {
        long long create_time = 0, erase_time = 0;
        for (int i = 0; i < sample_size; i++) {
            vector<shared_ptr<TestObject>> objects;
            objects.reserve(size);
            for (int i = 0; i < size; i++) {
                objects.emplace_back(make_shared<TestObject>());
            }
            create_time += (std::chrono::high_resolution_clock::now() - start_time).count();

            start_time = std::chrono::high_resolution_clock::now();
            while (objects.size() != 0) {
                auto obj = objects[objects.size() - 1];

                objects.erase(objects.end() - 1);
            }
            erase_time += (std::chrono::high_resolution_clock::now() - start_time).count();

        }

        Print("#2 : Create Time : " + String(((double)create_time / (double)sample_size) / 1000000.0) + " (ms)");
        Print("#2 : Erase Time : " + String(((double)erase_time / (double)sample_size) / 1000000.0) + " (ms)");
    }

    while (true) {}
    return 0;
}

 

Link to comment
Share on other sites

For some reason STL can be quite slow in debug mode. I actually wrote my own sorting routine to overcome some of this in some intensive code.

Writing a sort routine is hard, but I remembered how from my Blitz3D days because that language did not support anything like that. It took me back to my old "brute force" programming days where I would just write code until it worked.

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...