性能测试完整版本
This commit is contained in:
parent
64e0c379a5
commit
d7eea236ed
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,4 +1,7 @@
|
|||
.vscode
|
||||
bin
|
||||
main
|
||||
.attach_*
|
||||
*.o
|
||||
*.dat
|
||||
*.log
|
||||
|
|
28
.vscode/launch.json
vendored
Normal file
28
.vscode/launch.json
vendored
Normal 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
40
.vscode/settings.json
vendored
Normal 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
12
.vscode/tasks.json
vendored
Normal 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
5
run.sh
Normal file
|
@ -0,0 +1,5 @@
|
|||
./bin/main 1
|
||||
./bin/main 1_1
|
||||
./bin/main 2
|
||||
./bin/main 2_1
|
||||
./bin/main 3
|
185
src/main.cpp
185
src/main.cpp
|
@ -1,126 +1,140 @@
|
|||
#include <iostream>
|
||||
#include <map>
|
||||
#include <chrono>
|
||||
#include <random>
|
||||
#include "vbtree.h"
|
||||
#include "sbt.h"
|
||||
|
||||
#include "vbtree.h"
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <random>
|
||||
|
||||
using namespace std;
|
||||
using chrono::high_resolution_clock;
|
||||
using std::string;
|
||||
|
||||
int IntCompare(int v1, int v2)
|
||||
{
|
||||
if (v1 > v2)
|
||||
{
|
||||
int IntCompare(int v1, int v2) {
|
||||
if (v1 > v2) {
|
||||
return 1;
|
||||
}
|
||||
else if (v1 < v2)
|
||||
{
|
||||
} else if (v1 < v2) {
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
const ULONG N = 5000000;
|
||||
|
||||
void Case1()
|
||||
{
|
||||
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() {
|
||||
std::map<int, int> m;
|
||||
|
||||
default_random_engine e;
|
||||
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++)
|
||||
{
|
||||
auto v = dist(e);
|
||||
for (ULONG i = 0; i < N; i++) {
|
||||
auto v = vec[i];
|
||||
m[v] = 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 << "end RBTree Case Benchmark" << std::endl;
|
||||
std::cout << "end RBTree Case <Put> Benchmark" << std::endl;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Case1_1()
|
||||
{
|
||||
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);
|
||||
for (ULONG i = 0; i < N; i++) {
|
||||
auto v = vec[i];
|
||||
m[v] = v;
|
||||
vec.push_back(v);
|
||||
}
|
||||
|
||||
high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳
|
||||
high_resolution_clock::time_point t1 =
|
||||
high_resolution_clock::now(); //返回时间戳
|
||||
|
||||
for(auto iter = vec.begin(); iter != vec.end() ; iter++) {
|
||||
m[*iter];
|
||||
for (auto iter = vec.begin(); iter != vec.end(); 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 << "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);
|
||||
|
||||
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 = dist(e);
|
||||
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 Benchmark" << std::endl;
|
||||
}
|
||||
high_resolution_clock::time_point t1 =
|
||||
high_resolution_clock::now(); //返回时间戳
|
||||
|
||||
void Case2_1()
|
||||
{
|
||||
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);
|
||||
for (auto iter = vec.begin(); iter != vec.end(); iter++) {
|
||||
m.get(*iter);
|
||||
}
|
||||
|
||||
high_resolution_clock::time_point t1 = high_resolution_clock::now(); //返回时间戳
|
||||
|
||||
for(auto iter = vec.begin(); iter != vec.end() ; 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 << "end VBTree Case Benchmark" << std::endl;
|
||||
std::cout << "end VBTree Case <Get> Benchmark" << std::endl;
|
||||
}
|
||||
|
||||
void Case3() {
|
||||
|
@ -129,21 +143,32 @@ void Case3() {
|
|||
default_random_engine e;
|
||||
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++)
|
||||
{
|
||||
auto v = dist(e);
|
||||
for (ULONG i = 0; i < N; i++) {
|
||||
auto v = vec[i];
|
||||
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 << "end SBT Case Benchmark" << std::endl;
|
||||
std::cout << "end SBT Case <Put> Benchmark" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Case2_1();
|
||||
// Case1();
|
||||
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]();
|
||||
}
|
23
src/vbtree.h
23
src/vbtree.h
|
@ -401,7 +401,6 @@ public:
|
|||
return;
|
||||
}
|
||||
|
||||
std::vector<TreeNode*> vsize;
|
||||
|
||||
for (TreeNode *cur = this->root;;)
|
||||
{
|
||||
|
@ -416,8 +415,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
// cur->size++;
|
||||
vsize.push_back(cur);
|
||||
cur->size++;
|
||||
|
||||
int c = this->compare(key, cur->key);
|
||||
if (c < 0)
|
||||
|
@ -428,10 +426,6 @@ public:
|
|||
node->key = key;
|
||||
node->value = value;
|
||||
|
||||
for (auto iter = vsize.begin(); iter != vsize.end();iter++) {
|
||||
(*iter)->size ++ ;
|
||||
}
|
||||
|
||||
cur->children[0] = node;
|
||||
node->parent = cur;
|
||||
|
||||
|
@ -450,7 +444,7 @@ public:
|
|||
}
|
||||
cur = cur->children[0];
|
||||
}
|
||||
else if (c > 0)
|
||||
else
|
||||
{
|
||||
if (cur->children[1] == NULL)
|
||||
{
|
||||
|
@ -458,10 +452,6 @@ public:
|
|||
node->key = key;
|
||||
node->value = value;
|
||||
|
||||
for (auto iter = vsize.begin(); iter != vsize.end();iter++) {
|
||||
(*iter)->size ++ ;
|
||||
}
|
||||
|
||||
cur->children[1] = node;
|
||||
node->parent = cur;
|
||||
|
||||
|
@ -480,13 +470,10 @@ public:
|
|||
}
|
||||
cur = cur->children[1];
|
||||
}
|
||||
else
|
||||
{
|
||||
cur->key = key;
|
||||
cur->value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "错误" << endl;
|
||||
return ;
|
||||
};
|
||||
|
||||
/* data */
|
||||
|
|
Loading…
Reference in New Issue
Block a user