Created
October 14, 2020 20:31
-
-
Save Mytherin/0cf6f498556d8ad6df1a754ff6d48d2d to your computer and use it in GitHub Desktop.
Benchmark of insertion performance of std::list vs std::vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <list> | |
#include <vector> | |
#include <chrono> | |
#include <iostream> | |
using namespace std; | |
// compile: clang++ -std=c++11 -O3 benchlist.cpp | |
// List duration: 809ms | |
// Vector duration: 147ms | |
int INSERT_COUNT = 10000000; | |
int main() { | |
list<int> l; | |
vector<int> v; | |
auto start =chrono::system_clock::now(); | |
for(int i = 0; i < INSERT_COUNT; i++) { | |
l.push_back(i); | |
} | |
auto end = chrono::system_clock::now(); | |
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | |
std::cout << "List duration: " << elapsed.count() << "ms\n"; | |
start =chrono::system_clock::now(); | |
for(int i = 0; i < INSERT_COUNT; i++) { | |
v.push_back(i); | |
} | |
end = chrono::system_clock::now(); | |
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start); | |
std::cout << "Vector duration: " << elapsed.count() << "ms\n"; | |
if (v.size() != l.size()) { | |
std::cout << "EEEK"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment