SpiderPig Posted January 20, 2023 Share Posted January 20, 2023 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. #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; } Quote Link to comment Share on other sites More sharing options...
SpiderPig Posted January 20, 2023 Author Share Posted January 20, 2023 And of course release is faster still. 1 Quote Link to comment Share on other sites More sharing options...
Josh Posted January 21, 2023 Share Posted January 21, 2023 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. 1 Quote 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 More sharing options...
SpiderPig Posted January 21, 2023 Author Share Posted January 21, 2023 I've read that STL has been slow for years because the debugging side of things is extremely thorough. But who knows what the real cause is. I have a few tricks up my sleeve that could get things faster. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.