test_vbtree/src/main.cpp

174 lines
3.7 KiB
C++
Raw Normal View History

2019-08-02 20:10:27 +00:00
#include "sbt.h"
#include "vbtree.h"
#include <chrono>
#include <fstream>
2019-08-01 10:21:41 +00:00
#include <iostream>
2019-08-02 20:10:27 +00:00
#include <string>
2019-08-01 10:21:41 +00:00
#include <map>
#include <random>
using namespace std;
using chrono::high_resolution_clock;
2019-08-02 20:10:27 +00:00
using std::string;
2019-08-01 10:21:41 +00:00
2019-08-02 20:10:27 +00:00
int IntCompare(int v1, int v2) {
if (v1 > v2) {
2019-08-02 10:32:55 +00:00
return 1;
2019-08-02 20:10:27 +00:00
} else if (v1 < v2) {
2019-08-02 10:32:55 +00:00
return -1;
2019-08-02 20:10:27 +00:00
} else {
2019-08-02 10:32:55 +00:00
return 0;
}
}
2019-08-01 10:21:41 +00:00
2019-08-02 10:32:55 +00:00
const ULONG N = 5000000;
2019-08-01 10:21:41 +00:00
2019-08-02 20:10:27 +00:00
vector<unsigned int> vec;
map<string, void (*)()> funcmap ;
void init() {
const char *fpath = "./vec.dat";
std::ifstream inf(fpath);
if (!inf.is_open()) {
std::ofstream openfile(fpath, ios::binary | ios::trunc);
default_random_engine e;
std::uniform_int_distribution<> dist{0, 1000000000};
for (ULONG i = 0; i < N; i++) {
auto v = dist(e);
openfile << v << " ";
}
openfile.close();
inf.open(fpath);
}
for(;!inf.eof();) {
int v;
inf >> v;
vec.push_back(v);
}
}
void Case1() {
2019-08-02 10:32:55 +00:00
std::map<int, int> m;
default_random_engine e;
std::uniform_int_distribution<> dist{0, 1000000000};
2019-08-02 20:10:27 +00:00
high_resolution_clock::time_point t1 =
high_resolution_clock::now(); //返回时间戳
2019-08-02 10:32:55 +00:00
2019-08-02 20:10:27 +00:00
for (ULONG i = 0; i < N; i++) {
auto v = vec[i];
2019-08-02 10:32:55 +00:00
m[v] = v;
}
2019-08-02 20:10:27 +00:00
high_resolution_clock::time_point t2 =
high_resolution_clock::now(); //返回时间戳
2019-08-02 10:32:55 +00:00
std::cout << (t2 - t1).count() / N << std::endl;
2019-08-02 20:10:27 +00:00
std::cout << "end RBTree Case <Put> Benchmark" << std::endl;
2019-08-02 10:32:55 +00:00
}
2019-08-02 20:10:27 +00:00
void Case1_1() {
2019-08-02 10:32:55 +00:00
std::map<int, int> m;
default_random_engine e;
std::uniform_int_distribution<> dist{0, 1000000000};
2019-08-02 20:10:27 +00:00
for (ULONG i = 0; i < N; i++) {
auto v = vec[i];
2019-08-02 10:32:55 +00:00
m[v] = v;
}
2019-08-02 20:10:27 +00:00
high_resolution_clock::time_point t1 =
high_resolution_clock::now(); //返回时间戳
2019-08-02 10:32:55 +00:00
2019-08-02 20:10:27 +00:00
for (auto iter = vec.begin(); iter != vec.end(); iter++) {
m[*iter];
2019-08-02 10:32:55 +00:00
}
2019-08-02 20:10:27 +00:00
high_resolution_clock::time_point t2 =
high_resolution_clock::now(); //返回时间戳
2019-08-02 10:32:55 +00:00
std::cout << (t2 - t1).count() / N << std::endl;
2019-08-02 20:10:27 +00:00
std::cout << "end RBTree Case <Get> Benchmark" << std::endl;
2019-08-02 10:32:55 +00:00
}
2019-08-02 20:10:27 +00:00
void Case2() {
2019-08-02 10:32:55 +00:00
VBTree<int, int> m(IntCompare);
2019-08-02 20:10:27 +00:00
high_resolution_clock::time_point t1 =
high_resolution_clock::now(); //返回时间戳
for (ULONG i = 0; i < N; i++) {
auto v = vec[i];
2019-08-02 10:32:55 +00:00
m.put(v, v);
}
2019-08-02 20:10:27 +00:00
high_resolution_clock::time_point t2 =
high_resolution_clock::now(); //返回时间戳
2019-08-02 10:32:55 +00:00
std::cout << (t2 - t1).count() / N << std::endl;
2019-08-02 20:10:27 +00:00
std::cout << "end VBTree Case <Put> Benchmark" << std::endl;
2019-08-02 10:32:55 +00:00
}
2019-08-02 20:10:27 +00:00
void Case2_1() {
2019-08-02 10:32:55 +00:00
VBTree<int, int> m(IntCompare);
2019-08-01 10:21:41 +00:00
2019-08-02 10:32:55 +00:00
default_random_engine e;
std::uniform_int_distribution<> dist{0, 1000000000};
2019-08-01 10:21:41 +00:00
2019-08-02 20:10:27 +00:00
for (ULONG i = 0; i < N; i++) {
auto v = vec[i];
2019-08-02 10:32:55 +00:00
m.put(v, v);
}
2019-08-01 10:21:41 +00:00
2019-08-02 20:10:27 +00:00
high_resolution_clock::time_point t1 =
high_resolution_clock::now(); //返回时间戳
2019-08-01 10:21:41 +00:00
2019-08-02 20:10:27 +00:00
for (auto iter = vec.begin(); iter != vec.end(); iter++) {
m.get(*iter);
2019-08-02 10:32:55 +00:00
}
2019-08-02 20:10:27 +00:00
high_resolution_clock::time_point t2 =
high_resolution_clock::now(); //返回时间戳
2019-08-02 10:32:55 +00:00
std::cout << (t2 - t1).count() / N << std::endl;
2019-08-02 20:10:27 +00:00
std::cout << "end VBTree Case <Get> Benchmark" << std::endl;
2019-08-01 10:21:41 +00:00
}
2019-08-02 10:32:55 +00:00
void Case3() {
BinaryTree tree;
default_random_engine e;
std::uniform_int_distribution<> dist{0, 1000000000};
2019-08-02 20:10:27 +00:00
high_resolution_clock::time_point t1 =
high_resolution_clock::now(); //返回时间戳
2019-08-02 10:32:55 +00:00
2019-08-02 20:10:27 +00:00
for (ULONG i = 0; i < N; i++) {
auto v = vec[i];
2019-08-02 10:32:55 +00:00
tree.insert(v);
}
2019-08-02 20:10:27 +00:00
high_resolution_clock::time_point t2 =
high_resolution_clock::now(); //返回时间戳
2019-08-02 10:32:55 +00:00
std::cout << (t2 - t1).count() / N << std::endl;
2019-08-02 20:10:27 +00:00
std::cout << "end SBT Case <Put> Benchmark" << std::endl;
2019-08-02 10:32:55 +00:00
}
2019-08-02 20:10:27 +00:00
int main(int argc, char *argv[]) {
init();
funcmap["1"] = Case1;
funcmap["1_1"] = Case1_1;
funcmap["2"] = Case2;
funcmap["2_1"] = Case2_1;
funcmap["3"] = Case3;
cout << argv[1] << endl;
string fname = argv[1];
funcmap[fname]();
2019-08-01 10:21:41 +00:00
}