google / google/leveldb

Question about speed difference of writing repeated datas and writing unique datas

Open
#841 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
39.4k
Forks
8.2k
PR merge metrics
No merged PRs in 30d

Description

I am writing a simple demo to test leveldb, but I find that writing repeated keys with fixed values is slower that writing unique keys with same values. The result conflicts with my expectation, since I thought there will be more adjustments for test case with unique keys.

The configurations are as follows.
* Windows 10 with Visual Studio 2019
* CMake version 3.19.0-rc2, demo build with Release mode and C++ 17 standard

The test code are as follows and I make efforts to not introduce extra differences.

```cpp
#include
#include
#include
#include
#include

using namespace std;
using namespace std::chrono;

long long TestSameRepeat() {
const int entry_count = 10, rep_count = 1000000;
string *keys = new string[entry_count], *vals = new string[entry_count];
for (int i = 0; i < entry_count; ++i) {
char tmp[10] = "\0";
sprintf(tmp, "%08d", i);
keys[i] = "key" + string(tmp);
vals[i] = "valxxx";
}
leveldb::DB *db;
leveldb::Options opts;
opts.create_if_missing = true;
leveldb::Status sta = leveldb::DB::Open(
opts, (filesystem::current_path() / "test_rep_db").string(), &db);
if (!sta.ok()) {
cout << sta.ToString() << endl;
}
// start
system_clock::time_point begin_tm = system_clock::now();
for (int i = 0; i < rep_count; ++i) {
db->Put(leveldb::WriteOptions(), keys[i % entry_count],
vals[i % entry_count]);
}
system_clock::time_point end_tm = system_clock::now();
chrono::milliseconds dura =
chrono::duration_cast(end_tm - begin_tm);
cout << "Elapsed " << dura.count() << " ms" << endl;
delete db;
delete[] keys;
delete[] vals;
return dura.count();
}

long long TestNoRepeat() {
const int entry_count = 1000000;
string *keys = new string[entry_count], *vals = new string[entry_count];
for (int i = 0; i < entry_count; ++i) {
char tmp[10] = "\0";
sprintf(tmp, "%08d", i);
keys[i] = "key" + string(tmp);
vals[i] = "valxxx";
}
leveldb::DB *db;
leveldb::Options opts;
opts.create_if_missing = true;
leveldb::Status sta = leveldb::DB::Open(
opts, (filesystem::current_path() / "test_no_rep_db").string(), &db);
if (!sta.ok()) {
cout << sta.ToString() << endl;
}
// start
system_clock::time_point begin_tm = system_clock::now();
for (int i = 0; i < entry_count; ++i) {
db->Put(leveldb::WriteOptions(), keys[i % entry_count],
vals[i % entry_count]);
}
system_clock::time_point end_tm = system_clock::now();
chrono::milliseconds dura =
chrono::duration_cast(end_tm - begin_tm);
cout << "Elapsed " << dura.count() << " ms" << endl;
delete db;
delete[] keys;
delete[] vals;
return dura.count();
}

int main(int argc, char const *argv[]) {
long long rep_total_dura = 0, no_rep_total_dura = 0;
const int rep = 10;
for (int i = 0; i < rep; ++i) {
rep_total_dura += TestSameRepeat();
filesystem::remove_all(filesystem::current_path() / "test_rep_db");
}
for (int i = 0; i < rep; ++i) {
no_rep_total_dura += TestNoRepeat();
filesystem::remove_all(filesystem::current_path() / "test_no_rep_db");
}
cout << "same rep avg dura=" << rep_total_dura * 1.0 / rep
<< ", no same avg dura=" << no_rep_total_dura * 1.0 / rep << endl;
return 0;
}
```

On my computer, the average durations are 5785.7 ms and 5175.5 ms

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.