Skip to content

Instantly share code, notes, and snippets.

@duckie
Created June 10, 2014 00:55
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 duckie/7a0db39b3c448b789222 to your computer and use it in GitHub Desktop.
Save duckie/7a0db39b3c448b789222 to your computer and use it in GitHub Desktop.
Small perf comparison.
#include <iostream>
#include <chrono>
#include <vector>
#include <set>
#include <list>
#include <unordered_set>
#include <random>
#include <string>
#include <sstream>
using namespace std;
using namespace chrono;
unsigned constexpr const_str_size(char const *input) { return *input ? 1u + const_str_size(input + 1) : 0; }
class const_string {
const char * data_;
std::size_t size_;
public:
constexpr const_string(const char * data) : data_(data), size_(const_str_size(data)) {}
constexpr char operator[](std::size_t n) const { return n < size_ ? data_[n] : throw std::out_of_range(""); }
constexpr std::size_t size() const { return size_; }
};
class string_generator {
const_string const alnums = "abcdefghijklmnopqrstuvwxyz0123456789.,;:?!*%$@-(){}+[]/\\=";
random_device rd_;
mt19937 gen_;
uniform_int_distribution<size_t> size_gen_ = uniform_int_distribution<size_t>(5, 50);
uniform_int_distribution<unsigned int> char_chooser_ = uniform_int_distribution<unsigned int>(0u, alnums.size() - 1u);
public:
string_generator() : gen_(rd_()) {}
std::string operator() () {
ostringstream output;
size_t size = size_gen_(gen_);
for(size_t index = 0u; index < size; ++index) output << alnums[char_chooser_(gen_)];
return output.str();
}
};
size_t get_ms(time_point<high_resolution_clock> const& start, time_point<high_resolution_clock> const& end) {
return duration_cast<milliseconds>(end-start).count();
}
int main() {
size_t constexpr size = 1e7;
string_generator str_gen;
list<string> c1;
unordered_set<string> c2;
set<string> c3;
time_point<high_resolution_clock> t0 = high_resolution_clock::now();
for(int index = 0; index < size; ++index) {
std::string const value = str_gen();
auto elem = c2.find(value);
if (end(c2) == elem) {
c1.emplace_back(value);
c2.insert(value);
c3.insert(value);
}
}
time_point<high_resolution_clock> t1 = high_resolution_clock::now();
std::cout << "generation took : " << get_ms(t0,t1) << "ms" << std::endl;
std::cout << "list size : " << c1.size() << std::endl;
std::cout << "unordered_set size : " << c2.size() << std::endl;
time_point<high_resolution_clock> t2 = high_resolution_clock::now();
unsigned size_accumulator = 0;
for(auto const& v : c1) size_accumulator += v.size();
time_point<high_resolution_clock> t3 = high_resolution_clock::now();
size_accumulator = 0;
for(auto const& v : c2) size_accumulator += v.size();
time_point<high_resolution_clock> t4 = high_resolution_clock::now();
size_accumulator = 0;
for(auto const& v : c3) size_accumulator += v.size();
time_point<high_resolution_clock> t5 = high_resolution_clock::now();
std::cout << "list : " << get_ms(t2,t3) << "ms" << std::endl;
std::cout << "unordered_set : " << get_ms(t3,t4) << "ms" << std::endl;
std::cout << "set : " << get_ms(t4,t5) << "ms" << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment