cereal icon indicating copy to clipboard operation
cereal copied to clipboard

cereal::JSONOutputArchive serialize 1M large string cost more than 100ms

Open zhenjing opened this issue 3 years ago • 2 comments

I use cereal::JSONOutputArchive serialize 1M large string, it takes more than 100ms. Is it a normal behavior ? Is there a solution to reduce the serialization time ?

` std::string str1 = std::string(100 * 1024, 'a'); oStr = std::ostringstream(); // 创建新ss对象 StopWatch sw; { cereal::JSONOutputArchive archive(oStr); archive(cereal::make_nvp("Test", str1)); } std::cout << "cereal::JSONOutputArchive 100k string cost time:" << sw.elapsed().count() << std::endl;

std::string str2 = std::string(1024 * 1024, 'a');
oStr = std::ostringstream();  // 创建新ss对象
sw.reset();
{
    cereal::JSONOutputArchive archive(oStr);
    archive(cereal::make_nvp("Test", str2));
}
std::cout << "cereal::JSONOutputArchive 1M string cost time:" << sw.elapsed().count() << std::endl;

std::string str3 = std::string(10 * 1024 * 1024, 'a');
oStr = std::ostringstream();  // 创建新ss对象
sw.reset();
{
    cereal::JSONOutputArchive archive(oStr);
    archive(cereal::make_nvp("Test", str3));
}
std::cout << "cereal::JSONOutputArchive 10M string cost time:" << sw.elapsed().count() << std::endl;

`

output log: cereal::JSONOutputArchive 100k string cost time:0.01547 cereal::JSONOutputArchive 1M string cost time:0.147921 cereal::JSONOutputArchive 10M string cost time:1.36517

zhenjing avatar Dec 02 '22 06:12 zhenjing

您的邮件我已收到,谢谢合作!

redchairman avatar Dec 02 '22 06:12 redchairman

When rapidjson write String value,it scan all char to deal with Unicode escaping. So string is larger, it costs more time. Refer : rapidjson -> write.h -> bool WriteString(const Ch* str, SizeType length)

zhenjing avatar Dec 02 '22 09:12 zhenjing