Last active
March 4, 2021 14:20
-
-
Save rdebroiz/296feaa0ab4f9a2888914d4b4be60727 to your computer and use it in GitHub Desktop.
bench tree<char> with stl algorithms
This file contains hidden or 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 <iomanip> | |
| #include <chrono> | |
| #include <numeric> | |
| #include <cmath> | |
| #include <tree.hh> | |
| using namespace std; | |
| pair<double, double> meand_and_sigma(const vector<double>& v) { | |
| double sum = accumulate(v.begin(), v.end(), 0.0); | |
| double mean = sum / v.size(); | |
| double sq_sum = inner_product(v.begin(), v.end(), v.begin(), 0.0); | |
| double sig = sqrt(sq_sum / v.size() - mean * mean); | |
| return {mean, sig}; | |
| } | |
| int main(int argc, const char** argv) { | |
| cout << "Given container (C) filled all char between min=-128 and max=127 ([min, max])" << endl | |
| << "For each char (x) in [min, max] perform 100 lookup of x in C" << endl << endl; | |
| cout << "C is tree<char> [head->c1, head->-c2, head->c3...]: " << endl; | |
| tree<char> tt; | |
| char max = numeric_limits<char>::max(); | |
| char min = numeric_limits<char>::min(); | |
| tt.head->data = max; | |
| tt.feet->data = max; | |
| char c = min; | |
| auto it = tt.insert(tt.begin(), c); | |
| while (c < max) { | |
| ++c; | |
| it = tt.insert_after(it, c); | |
| } | |
| // iter over all siblings. | |
| vector<double> durations_iter; | |
| for(char v = min; v < max; ++v) { | |
| for(int i = 0; i < 100; i++) { | |
| auto start = chrono::high_resolution_clock::now(); | |
| tree<char>::sibling_iterator w = tt.begin(); | |
| while (tt.is_valid(w) && *w < v) ++w; | |
| auto fnd = w; | |
| bool res = tt.is_valid(fnd) && *fnd == v; | |
| auto stop = chrono::high_resolution_clock::now(); | |
| double dur = chrono::duration<double, nano>(stop - start).count(); | |
| durations_iter.push_back(dur); | |
| } | |
| } | |
| auto stats = meand_and_sigma(durations_iter); | |
| cout << "[iterator] duration (ns): mean=" << stats.first << ", sigma=" << stats.second << endl; | |
| vector<double> durations_lb; | |
| for(char v = min; v < max; ++v) { | |
| for(int i = 0; i < 100; i++) { | |
| tree<char>::sibling_iterator w = tt.begin(); | |
| auto start = chrono::high_resolution_clock::now(); | |
| auto fnd = lower_bound(w, w.end(), v); | |
| bool res = tt.is_valid(fnd) && *fnd == v; | |
| auto stop = chrono::high_resolution_clock::now(); | |
| double dur = chrono::duration<double, nano>(stop - start).count(); | |
| durations_lb.push_back(dur); | |
| } | |
| } | |
| stats = meand_and_sigma(durations_lb); | |
| cout << "[stl::lower_bound] duration (ns): mean=" << stats.first << ", sigma=" << stats.second << endl; | |
| cout << endl << "C is vector<char>:" << endl; | |
| // compare with vectors. | |
| vector<char> vec; | |
| for(char x = min; x < max; ++x) { | |
| vec.push_back(x); | |
| } | |
| vector<double> v_durations_op; | |
| for(char v = min; v < max; ++v) { | |
| for(int i = 0; i < 100; i++) { | |
| auto start = chrono::high_resolution_clock::now(); | |
| char x = 0; | |
| while (vec[x] < c && x < vec.size()) ++x; | |
| bool res = vec[x] == c; | |
| auto stop = chrono::high_resolution_clock::now(); | |
| double dur = chrono::duration<double, nano>(stop - start).count(); | |
| v_durations_op.push_back(dur); | |
| } | |
| } | |
| stats = meand_and_sigma(v_durations_op); | |
| cout << "[operator[]] duration (ns): mean=" << stats.first << ", sigma=" << stats.second << endl; | |
| vector<double> v_durations_iter; | |
| for(char v = min; v < max; ++v) { | |
| for(int i = 0; i < 100; i++) { | |
| auto start = chrono::high_resolution_clock::now(); | |
| auto w = vec.begin(); | |
| while (w != vec.end() && *w < v) ++w; | |
| auto fnd = w; | |
| bool res = fnd != vec.end() && *fnd == v; | |
| auto stop = chrono::high_resolution_clock::now(); | |
| double dur = chrono::duration<double, nano>(stop - start).count(); | |
| v_durations_iter.push_back(dur); | |
| } | |
| } | |
| stats = meand_and_sigma(v_durations_iter); | |
| cout << "[iterator] duration (ns): mean=" << stats.first << ", sigma=" << stats.second << endl; | |
| vector<double> v_durations_lb; | |
| for(char v = min; v < max; ++v) { | |
| for(int i = 0; i < 100; i++) { | |
| auto start = chrono::high_resolution_clock::now(); | |
| auto fnd = lower_bound(vec.begin(), vec.end(), v); | |
| bool res = fnd != vec.end() && *fnd == v; | |
| auto stop = chrono::high_resolution_clock::now(); | |
| double dur = chrono::duration<double, nano>(stop - start).count(); | |
| v_durations_lb.push_back(dur); | |
| } | |
| } | |
| stats = meand_and_sigma(v_durations_lb); | |
| cout << "[stl::lower_bound] duration (ns): mean=" << stats.first << ", sigma=" << stats.second << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment