Skip to content

Instantly share code, notes, and snippets.

@czotti
Last active December 4, 2015 16:45
Show Gist options
  • Save czotti/9722342d84adc428d6a7 to your computer and use it in GitHub Desktop.
Save czotti/9722342d84adc428d6a7 to your computer and use it in GitHub Desktop.
#include <vector>
#include <random>
#include <set>
#include <map>
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <iomanip>
int main(){
typedef std::chrono::nanoseconds scm;
std::vector<int> ech = {10, 20, 50, 100, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 600000, 1000000, 2000000, 5000000, 10000000, 20000000};
// element loop
std::random_device rd;
std::mt19937 g(rd());
for(auto nel: ech){
//generate vector and shuffle
std::vector<int> vec(nel);
int n = {0};
std::generate(vec.begin(),vec.end(), [&n]{return n++;});
//std::shuffle(vec.begin(), vec.end(), g);
// insert same element inside set
std::map<int,int> set;
for(auto el: vec){
set.insert(std::pair<int,int>(el,0));
}
scm localVTime(0);
// Start counting time vector
for(int i=0; i<100; ++i){
auto start = std::chrono::high_resolution_clock::now();
auto res = std::lower_bound(vec.begin(), vec.end(), std::rand()%nel);
auto stop = std::chrono::high_resolution_clock::now();
localVTime += std::chrono::duration_cast<scm>(stop - start);
}
localVTime = localVTime/100;
scm localTime(0);
// Start counting time set
for(int i=0; i<100; ++i){
auto start = std::chrono::high_resolution_clock::now();
auto res = set.find(std::rand()%nel);
auto stop = std::chrono::high_resolution_clock::now();
localTime += std::chrono::duration_cast<scm>(stop - start);
}
localTime = localTime/100;
std::cout << std::setfill(' ') << std::setw(10) << nel << ": "
<< std::setfill(' ') << std::setw(10) << localVTime.count() << ", "
<< std::setfill(' ') << std::setw(10) << localTime.count() << "\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment