#include #include #include #include template float check_performance(Func test_function) { auto start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < 1000; ++i) { test_function(); } auto end = std::chrono::high_resolution_clock::now(); return std::chrono::duration_cast(end - start).count() / 1000.f; } int main() { float piVectorPerformance = check_performance([](){ PIVector piVector; for (int i = 0; i < 1000; ++i) { piVector.push_back(i); } }); piCout << "piVector without preallocation:" << piVectorPerformance << "ms"; float stdVectorPerformance = check_performance([](){ std::vector stdVector; for (int i = 0; i < 1000; ++i) { stdVector.push_back(i); } }); piCout << "stdVector without preallocation:" << stdVectorPerformance << "ms"; std::unique_ptr > stdVector(new std::vector(1000)); stdVectorPerformance = check_performance(std::bind([](std::unique_ptr >& stdVector){ for (int i = 0; i < 1000; ++i) { stdVector->at(i) = i; } }, std::move(stdVector))); piCout << "stdVector with preallocation:" << stdVectorPerformance << "ms"; std::unique_ptr array(new int[1000]); float arrayPerformance = check_performance(std::bind([](std::unique_ptr& array){ for (int i = 0; i < 1000; ++i) { array.get()[i] = i; } }, std::move(array))); piCout << "array:" << arrayPerformance << "ms"; return 0; }