-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to effectively store binary data? #898
Comments
The data in a |
Well, I just use json as the basic data structures of other languages. The json object is quite large and deep and there are many |
No, it uses You should not be trying to put arbitrary pointers into the I'm not sure how one would map an arbitrary array of bytes into json such that it can be retrieved quite frequently without the expense of extracting from |
// ConsoleApplication4.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <nlohmann/json.hpp>
#include <iostream>
#include <chrono>
using namespace std;
using json = nlohmann::json;
int main()
{
uint8_t my_data[26]={1,2,3,4,5};
json j = { {"test1", my_data},{"test2",string(26,'a')} };
cout << j.dump() << endl;
const int test_times = 50000; //
{
auto begin = std::chrono::high_resolution_clock::now();
string s;
for (int i = 0; i < test_times; i++) {
memset(my_data, 0, sizeof(my_data));
s = j["test2"].get<string>();
}
auto end = std::chrono::high_resolution_clock::now();
cout << s << " ";
cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << endl;
}
{
auto begin = std::chrono::high_resolution_clock::now();
vector<uint8_t> a;
for (int i = 0; i < test_times; i++) {
a = j["test1"].get<vector<uint8_t>>();
}
auto end = std::chrono::high_resolution_clock::now();
cout << int(a[4]) << " ";
cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << endl;
}
{
auto begin = std::chrono::high_resolution_clock::now();
json::array_t* p;
for (int i = 0; i < test_times; i++) {
p = j["test1"].get_ptr<json::array_t*>();
}
auto end = std::chrono::high_resolution_clock::now();
cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << endl;
}
//system("pause");
return 0;
} The result:
On linux, I get this:
It's still about 3:10:1 |
I have a data ,
uint8_t my_data[26]
which I want to put injson
object once and fetch it quite frequently. There is no need todump
thejson
object.I try to convert it to
std::string
and it does not work(rapidjson
seems ok). Casting to and fromstd::vector
seems quite expensive. I also need the lifecycle of the my_data is same as the json object. Is there a better way to solve it?I thought that maybe I could get the
std::string::data()
orstd::vector::data()
directly fromjson
object but I could not find the right way.The text was updated successfully, but these errors were encountered: