性能测试完整版本

This commit is contained in:
eson 2019-08-03 04:10:27 +08:00
parent 64e0c379a5
commit d7eea236ed
7 changed files with 198 additions and 98 deletions

3
.gitignore vendored
View File

@ -1,4 +1,7 @@
.vscode .vscode
bin
main main
.attach_* .attach_*
*.o *.o
*.dat
*.log

28
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,28 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/main",
"args": ["1"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"preLaunchTask": "make",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

40
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,40 @@
{
"files.associations": {
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"*.tcc": "cpp",
"chrono": "cpp",
"cstdint": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"numeric": "cpp",
"optional": "cpp",
"ostream": "cpp",
"ratio": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"type_traits": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"utility": "cpp"
}
}

12
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,12 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "make",
"type": "shell",
"command": "make"
}
]
}

5
run.sh Normal file
View File

@ -0,0 +1,5 @@
./bin/main 1
./bin/main 1_1
./bin/main 2
./bin/main 2_1
./bin/main 3

View File

@ -1,126 +1,140 @@
#include <iostream>
#include <map>
#include <chrono>
#include <random>
#include "vbtree.h"
#include "sbt.h" #include "sbt.h"
#include "vbtree.h"
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <map>
#include <random>
using namespace std; using namespace std;
using chrono::high_resolution_clock; using chrono::high_resolution_clock;
using std::string;
int IntCompare(int v1, int v2) int IntCompare(int v1, int v2) {
{ if (v1 > v2) {
if (v1 > v2)
{
return 1; return 1;
} } else if (v1 < v2) {
else if (v1 < v2)
{
return -1; return -1;
} } else {
else
{
return 0; return 0;
} }
} }
const ULONG N = 5000000; const ULONG N = 5000000;
void Case1() vector<unsigned int> vec;
{ map<string, void (*)()> funcmap ;
std::map<int, int> m;
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; default_random_engine e;
std::uniform_int_distribution<> dist{0, 1000000000}; std::uniform_int_distribution<> dist{0, 1000000000};
for (ULONG i = 0; i < N; i++) {
high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳
for (ULONG i = 0; i < N; i++)
{
auto v = dist(e); auto v = dist(e);
m[v] = v; openfile << v << " ";
} }
high_resolution_clock::time_point t2 = high_resolution_clock::now(); //返回时间戳 openfile.close();
inf.open(fpath);
std::cout << (t2 - t1).count() / N << std::endl; }
std::cout << "end RBTree Case Benchmark" << std::endl; for(;!inf.eof();) {
} int v;
inf >> v;
void Case1_1()
{
std::map<int, int> m;
default_random_engine e;
std::uniform_int_distribution<> dist{0, 1000000000};
vector<int> vec;
for (ULONG i = 0; i < N; i++)
{
auto v = dist(e);
m[v] = v;
vec.push_back(v); vec.push_back(v);
} }
}
high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳 void Case1() {
std::map<int, int> m;
for(auto iter = vec.begin(); iter != vec.end() ; iter++) { default_random_engine e;
std::uniform_int_distribution<> dist{0, 1000000000};
high_resolution_clock::time_point t1 =
high_resolution_clock::now(); //返回时间戳
for (ULONG i = 0; i < N; i++) {
auto v = vec[i];
m[v] = v;
}
high_resolution_clock::time_point t2 =
high_resolution_clock::now(); //返回时间戳
std::cout << (t2 - t1).count() / N << std::endl;
std::cout << "end RBTree Case <Put> Benchmark" << std::endl;
}
void Case1_1() {
std::map<int, int> m;
default_random_engine e;
std::uniform_int_distribution<> dist{0, 1000000000};
for (ULONG i = 0; i < N; i++) {
auto v = vec[i];
m[v] = v;
}
high_resolution_clock::time_point t1 =
high_resolution_clock::now(); //返回时间戳
for (auto iter = vec.begin(); iter != vec.end(); iter++) {
m[*iter]; m[*iter];
} }
high_resolution_clock::time_point t2 = high_resolution_clock::now(); //返回时间戳 high_resolution_clock::time_point t2 =
high_resolution_clock::now(); //返回时间戳
std::cout << (t2 - t1).count() / N << std::endl; std::cout << (t2 - t1).count() / N << std::endl;
std::cout << "end RBTree Case Benchmark" << std::endl; std::cout << "end RBTree Case <Get> Benchmark" << std::endl;
} }
void Case2() void Case2() {
{ VBTree<int, int> m(IntCompare);
high_resolution_clock::time_point t1 =
high_resolution_clock::now(); //返回时间戳
for (ULONG i = 0; i < N; i++) {
auto v = vec[i];
m.put(v, v);
}
high_resolution_clock::time_point t2 =
high_resolution_clock::now(); //返回时间戳
std::cout << (t2 - t1).count() / N << std::endl;
std::cout << "end VBTree Case <Put> Benchmark" << std::endl;
}
void Case2_1() {
VBTree<int, int> m(IntCompare); VBTree<int, int> m(IntCompare);
default_random_engine e; default_random_engine e;
std::uniform_int_distribution<> dist{0, 1000000000}; std::uniform_int_distribution<> dist{0, 1000000000};
high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳 for (ULONG i = 0; i < N; i++) {
auto v = vec[i];
for (ULONG i = 0; i < N; i++)
{
auto v = dist(e);
m.put(v, v); m.put(v, v);
} }
high_resolution_clock::time_point t2 = high_resolution_clock::now(); //返回时间戳
std::cout << (t2 - t1).count() / N << std::endl; high_resolution_clock::time_point t1 =
std::cout << "end VBTree Case Benchmark" << std::endl; high_resolution_clock::now(); //返回时间戳
}
void Case2_1() for (auto iter = vec.begin(); iter != vec.end(); iter++) {
{
VBTree<int, int> m(IntCompare);
default_random_engine e;
std::uniform_int_distribution<> dist{0, 1000000000};
vector<int> vec;
for (ULONG i = 0; i < N; i++)
{
auto v = dist(e);
m.put(v, v);
vec.push_back(v);
}
high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳
for(auto iter = vec.begin(); iter != vec.end() ; iter++) {
m.get(*iter); m.get(*iter);
} }
high_resolution_clock::time_point t2 = high_resolution_clock::now(); //返回时间戳 high_resolution_clock::time_point t2 =
high_resolution_clock::now(); //返回时间戳
std::cout << (t2 - t1).count() / N << std::endl; std::cout << (t2 - t1).count() / N << std::endl;
std::cout << "end VBTree Case Benchmark" << std::endl; std::cout << "end VBTree Case <Get> Benchmark" << std::endl;
} }
void Case3() { void Case3() {
@ -129,21 +143,32 @@ void Case3() {
default_random_engine e; default_random_engine e;
std::uniform_int_distribution<> dist{0, 1000000000}; std::uniform_int_distribution<> dist{0, 1000000000};
high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳 high_resolution_clock::time_point t1 =
high_resolution_clock::now(); //返回时间戳
for (ULONG i = 0; i < N; i++) for (ULONG i = 0; i < N; i++) {
{ auto v = vec[i];
auto v = dist(e);
tree.insert(v); tree.insert(v);
} }
high_resolution_clock::time_point t2 = high_resolution_clock::now(); //返回时间戳 high_resolution_clock::time_point t2 =
high_resolution_clock::now(); //返回时间戳
std::cout << (t2 - t1).count() / N << std::endl; std::cout << (t2 - t1).count() / N << std::endl;
std::cout << "end SBT Case Benchmark" << std::endl; std::cout << "end SBT Case <Put> Benchmark" << std::endl;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[]) {
{ init();
Case2_1();
// Case1(); 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]();
} }

View File

@ -401,7 +401,6 @@ public:
return; return;
} }
std::vector<TreeNode*> vsize;
for (TreeNode *cur = this->root;;) for (TreeNode *cur = this->root;;)
{ {
@ -416,8 +415,7 @@ public:
} }
} }
// cur->size++; cur->size++;
vsize.push_back(cur);
int c = this->compare(key, cur->key); int c = this->compare(key, cur->key);
if (c < 0) if (c < 0)
@ -428,10 +426,6 @@ public:
node->key = key; node->key = key;
node->value = value; node->value = value;
for (auto iter = vsize.begin(); iter != vsize.end();iter++) {
(*iter)->size ++ ;
}
cur->children[0] = node; cur->children[0] = node;
node->parent = cur; node->parent = cur;
@ -450,7 +444,7 @@ public:
} }
cur = cur->children[0]; cur = cur->children[0];
} }
else if (c > 0) else
{ {
if (cur->children[1] == NULL) if (cur->children[1] == NULL)
{ {
@ -458,10 +452,6 @@ public:
node->key = key; node->key = key;
node->value = value; node->value = value;
for (auto iter = vsize.begin(); iter != vsize.end();iter++) {
(*iter)->size ++ ;
}
cur->children[1] = node; cur->children[1] = node;
node->parent = cur; node->parent = cur;
@ -480,13 +470,10 @@ public:
} }
cur = cur->children[1]; cur = cur->children[1];
} }
else
{
cur->key = key;
cur->value = value;
return;
}
} }
cout << "错误" << endl;
return ;
}; };
/* data */ /* data */