Skip to content

Instantly share code, notes, and snippets.

@uucidl
Last active September 27, 2015 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uucidl/4c1cf628fd47a3b3b924 to your computer and use it in GitHub Desktop.
Save uucidl/4c1cf628fd47a3b3b924 to your computer and use it in GitHub Desktop.
STL WTF

Results

All tested on OSX Yosemite with Apple LLVM version 7.0.0 (clang-700.0.72)

Debug Mode

Debug mode is important. It’s the performance you get when you’re developing and debugging after all.

/usr/bin/clang++ [/usr/bin/clang++, -std=c++14, -g, -isystem, /Users/nicolas/code/third-party/SGI-STL/modules/EASTL/include, /Users/nicolas/code/third-party/SGI-STL/tests/test_vector.cpp, -o, /Users/nicolas/code/third-party/SGI-STL/test_vector]
SGI STL vector resize test, elapsed: 5.1863
plain array resize test, elapsed: 0.554433
std::vector resize test, elapsed: 42.9156
eastl::vector resize test, elapsed: 18.4838

Release Mode

Building /Users/nicolas/code/third-party/SGI-STL/tests/test_vector.cpp
/usr/bin/clang++ [/usr/bin/clang++, -std=c++14, -g, -O3, -isystem, /Users/nicolas/code/third-party/SGI-STL/modules/EASTL/include, /Users/nicolas/code/third-party/SGI-STL/tests/test_vector.cpp, -o, /Users/nicolas/code/third-party/SGI-STL/test_vector]
SGI STL vector resize test, elapsed: 0.980416
plain array resize test, elapsed: 0.546984
std::vector resize test, elapsed: 4.13195
eastl::vector resize test, elapsed: 0.969096

Source Code

#include "../sgi_stl/vector"
#include <EASTL/vector.h>

#include <chrono>
#include <iostream>
#include <vector>

int main(int argc, char** argv)
{
        using namespace std::chrono;
        using std::cout;
        using std::endl;

        auto LARGE_SIZE = 8*1024*1024;
        auto SMALL_SIZE = 20000;
        auto TEST_COUNT = 100000;

        auto stl_v = vector<int*>();
        stl_v.reserve(LARGE_SIZE);
        {
                auto start_point = high_resolution_clock::now();
                int test_count = TEST_COUNT;
                while (test_count--) {
                        stl_v.resize(SMALL_SIZE, nullptr);
                        stl_v.resize(0);
                }
                auto end_point = high_resolution_clock::now();
                duration<double> elapsed_s = end_point - start_point;
                cout << "SGI STL vector resize test, elapsed: " << elapsed_s.count() << endl;
        }

        auto vp = &stl_v.front();
        auto vp_end = vp;
        {
                auto start_point = high_resolution_clock::now();
                int test_count = TEST_COUNT;
                while(test_count--) {
                        // resize to SMALL_SIZE
                        memset(vp_end, 0, (SMALL_SIZE - (vp_end - vp))*sizeof(*vp));
                        vp_end += SMALL_SIZE;

                        // resize back to zero
                        vp_end = vp;
                }
                auto end_point = high_resolution_clock::now();
                duration<double> elapsed_s = end_point - start_point;
                cout << "plain array resize test, elapsed: " << elapsed_s.count() << endl;
        }

        auto std_v = std::vector<int*>();
        std_v.reserve(LARGE_SIZE);
        {
                auto start_point = high_resolution_clock::now();
                int test_count = TEST_COUNT;
                while (test_count--) {
                        std_v.resize(SMALL_SIZE, nullptr);
                        std_v.resize(0);
                }
                auto end_point = high_resolution_clock::now();
                duration<double> elapsed_s = end_point - start_point;
                cout << "std::vector resize test, elapsed: " << elapsed_s.count() << endl;
        }

        auto eastl_v = eastl::vector<int*>();
        eastl_v.reserve(LARGE_SIZE);
        {
                auto start_point = high_resolution_clock::now();
                int test_count = TEST_COUNT;
                while (test_count--) {
                        eastl_v.resize(SMALL_SIZE, nullptr);
                        eastl_v.resize(0);
                }
                auto end_point = high_resolution_clock::now();
                duration<double> elapsed_s = end_point - start_point;
                cout << "eastl::vector resize test, elapsed: " << elapsed_s.count() << endl;
        }
}

// EASTL expects us to define these, see allocator.h line 194
void* operator new[](size_t size, const char* pName, int flags,
                     unsigned debugFlags, const char* file, int line)
{
  return malloc(size);
}
void* operator new[](size_t size, size_t alignment, size_t alignmentOffset,
    const char* pName, int flags, unsigned debugFlags, const char* file, int line)
{
  // this allocator doesn't support alignment
  EASTL_ASSERT(alignment <= 8);
  return malloc(size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment