json系列(三)cjson,rapidjson,yyjson解析性能对比
前言
本篇对cjson,rapidjson,yyjson三种json反序列化工具的性能进行对比。
有json样本数据如下:

实验环境:
cpu:Xeon
cpu主频:2.20GHz
以下示例均未对字段的安全性进行检查。各示例的字段安全性检查参考json系列第一篇“cjson,rapidjson,yyjson解析示例”。
一、cjson反序列化性能
1 #include<stdio.h>
2 #include<sys/time.h>
3
4 #include"cJSON.h"
5
6 // g++ -g -o cjson_speed_test -std=c++11 cjson_speed_test.c cJSON.c
7
8 int main()
9 {
10 int cnt = 0;
11 timeval st, et;
12
13 cJSON *json_root;
14 char str_buf[1024] = "{\"uri\":\"/uriCSh56j30cbGa\",\"host\":\"www.baidu.com\",\"uagent\":\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.64\",\"accept\":\"*/*\",\"method\":\"GET\",\"date\":\"Mon, 12 Jul 21 10:35:26 GMT\",\"resp_content_type\":\"video/fli\",\"status\":200,\"resp_content_length\":20480,\"timestamp\":\"2021-07-12T02:38:13.074829000\",\"traffic_id\":1057153235624398,\"protocol\":\"http\",\"src_ip\":\"112.1.101.40\",\"src_port\":22291,\"dst_ip\":\"112.2.81.190\",\"dst_port\":80,\"random_code\":7449212903698783717,\"feature_code\":\"y0BMEwnJ7RFICUAC5FKYkStTLVw=\"}";
15
16 gettimeofday(&st, NULL);
17 while(1) {
18 json_root = cJSON_Parse(str_buf);
19
20 cJSON_GetObjectItem(json_root, "protocol");
21 cJSON_GetObjectItem(json_root, "uri");
22
23 cJSON_GetObjectItem(json_root, "host");
24 cJSON_GetObjectItem(json_root, "uagent");
25
26 cJSON_GetObjectItem(json_root, "src_port");
27 cJSON_GetObjectItem(json_root, "dst_port");
28
29 cJSON_GetObjectItem(json_root, "timestamp");
30 cJSON_GetObjectItem(json_root, "feature_code");
31
32 cJSON_GetObjectItem(json_root, "src_ip");
33 cJSON_GetObjectItem(json_root, "dst_ip");
34
35 cJSON_GetObjectItem(json_root, "traffic_id");
36 cJSON_GetObjectItem(json_root, "random_code");
37
38 cJSON_Delete(json_root);
39
40 cnt++;
41 gettimeofday(&et, NULL);
42 if(et.tv_sec - st.tv_sec >= 10) {
43 break;
44 }
45 }
46
47 printf("deserialization per second:%d\n", cnt/10);
48 return 0;
49 }
反序列化性能:

二、rapidjson反序列化性能
rapidjson有两种解析方法,一种是Parse,另一种是ParseInsitu(原位解析)。区别在于ParseInsitu不需要进行malloc操作,在原来的字符串空间中进行字符串反序列化,弊端是原来的字符串会被修改。这里选用Parse方法。
1 #include<stdio.h>
2 #include<sys/time.h>
3
4 #include "rapidjson/rapidjson.h"
5 #include "rapidjson/document.h"
6 #include "rapidjson/stringbuffer.h"
7 #include "rapidjson/writer.h"
8
9 // g++ -g -o rapidjson_speed_test -std=c++11 rapidjson_speed_test.c
10
11 int main()
12 {
13 int cnt = 0;
14 timeval st, et;
15
16 char str_buf[1024] = "{\"uri\":\"/uriCSh56j30cbGa\",\"host\":\"www.baidu.com\",\"uagent\":\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.64\",\"accept\":\"*/*\",\"method\":\"GET\",\"date\":\"Mon, 12 Jul 21 10:35:26 GMT\",\"resp_content_type\":\"video/fli\",\"status\":200,\"resp_content_length\":20480,\"timestamp\":\"2021-07-12T02:38:13.074829000\",\"traffic_id\":1057153235624398,\"protocol\":\"http\",\"src_ip\":\"112.1.101.40\",\"src_port\":22291,\"dst_ip\":\"112.2.81.190\",\"dst_port\":80,\"random_code\":7449212903698783717,\"feature_code\":\"y0BMEwnJ7RFICUAC5FKYkStTLVw=\"}";
17 char str_buf_tmp[1024] = {0};
18
19 rapidjson::Document parse_doc;
20
21 gettimeofday(&st, NULL);
22 while(1) {
23
24 parse_doc.Parse(str_buf);
25 parse_doc.FindMember("protocol")->value.GetString();
26 parse_doc.FindMember("uri")->value.GetString();
27
28 parse_doc.FindMember("host")->value.GetString();
29 parse_doc.FindMember("resp_content_type")->value.GetString();
30
31 parse_doc.FindMember("src_port")->value.GetInt();
32 parse_doc.FindMember("dst_port")->value.GetInt();
33
34 parse_doc.FindMember("timestamp")->value.GetString();
35 parse_doc.FindMember("feature_code")->value.GetString();
36
37 parse_doc.FindMember("src_ip")->value.GetString();
38 parse_doc.FindMember("dst_ip")->value.GetString();
39
40 parse_doc.FindMember("traffic_id")->value.GetUint64();
41 parse_doc.FindMember("random_code")->value.GetUint64();
42
43 cnt++;
44 gettimeofday(&et, NULL);
45 if(et.tv_sec - st.tv_sec >= 10) {
46 break;
47 }
48 }
49
50 printf("deserialization per second:%d\n", cnt/10);
51 return 0;
52 }
反序列化性能:

三、yyjson反序列化性能
1 #include<stdio.h>
2 #include<sys/time.h>
3
4 #include "yyjson.h"
5
6 // g++ -g -o yyjson_speed_test -std=c++11 yyjson_speed_test.c yyjson.c
7
8 int main()
9 {
10 int cnt = 0;
11 timeval st, et;
12
13 long value2;
14 char str_buf[1024] = "{\"uri\":\"/uriCSh56j30cbGa\",\"host\":\"www.baidu.com\",\"uagent\":\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36 Edg/91.0.864.64\",\"accept\":\"*/*\",\"method\":\"GET\",\"date\":\"Mon, 12 Jul 21 10:35:26 GMT\",\"resp_content_type\":\"video/fli\",\"status\":200,\"resp_content_length\":20480,\"timestamp\":\"2021-07-12T02:38:13.074829000\",\"traffic_id\":1057153235624398,\"protocol\":\"http\",\"src_ip\":\"112.1.101.40\",\"src_port\":22291,\"dst_ip\":\"112.2.81.190\",\"dst_port\":80,\"random_code\":7449212903698783717,\"feature_code\":\"y0BMEwnJ7RFICUAC5FKYkStTLVw=\"}";
15
16 gettimeofday(&st, NULL);
17 while(1) {
18
19 yyjson_doc *doc = yyjson_read(str_buf, strlen(str_buf), 0);
20 yyjson_val *root = yyjson_doc_get_root(doc);
21
22 yyjson_get_str(yyjson_obj_get(root, "protocol"));
23 yyjson_get_str(yyjson_obj_get(root, "uri"));
24
25 yyjson_get_str(yyjson_obj_get(root, "host"));
26 yyjson_get_str(yyjson_obj_get(root, "uagent"));
27
28 yyjson_get_int(yyjson_obj_get(root, "src_port"));
29 yyjson_get_int(yyjson_obj_get(root, "dst_port"));
30
31 yyjson_get_str(yyjson_obj_get(root, "timestamp"));
32 yyjson_get_str(yyjson_obj_get(root, "feature_code"));
33
34 yyjson_get_str(yyjson_obj_get(root, "src_ip"));
35 yyjson_get_str(yyjson_obj_get(root, "dst_ip"));
36
37 yyjson_get_uint(yyjson_obj_get(root, "traffic_id"));
38 yyjson_get_uint(yyjson_obj_get(root, "random_code"));
39
40 yyjson_doc_free(doc);
41
42 cnt++;
43 gettimeofday(&et, NULL);
44 if(et.tv_sec - st.tv_sec >= 10) {
45 break;
46 }
47 }
48
49 printf("deserialization per second:%d\n", cnt/10);
50 return 0;
51 }
反序列化性能:

四、结论
yyjson的反序列化高性能真是让我感到欣喜,是cjson三倍以上、rapidjson Parse方法的10倍以上。若编译添加-O2优化参数,yyjson的测试结果更加惊喜,并且yyjson的cpu使用率更低。
另外,yyjson的序列化性能也是三者中最高的。
推荐大家使用yyjson!
json系列(三)cjson,rapidjson,yyjson解析性能对比的更多相关文章
- 开源 JSON 库解析性能对比( Jackson / Json.simple / Gson )
Json 已成为当前服务器与 web 应用之间数据传输的公认标准. 微服务及分布式架构经常会使用 Json 来传输此类文件,因为这已经是 webAPI 的事实标准. 不过正如许多我们习以为常的事情一样 ...
- MySQL并发复制系列三:MySQL和MariaDB实现对比
http://blog.itpub.net/28218939/viewspace-1975856/ 并发复制(Parallel Replication) 系列三:MySQL 5.7 和MariaDB ...
- Go_18: Golang 中三种读取文件发放性能对比
Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提 ...
- Golang 中三种读取文件发放性能对比
Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提 ...
- Java爬虫系列三:使用Jsoup解析HTML
在上一篇随笔<Java爬虫系列二:使用HttpClient抓取页面HTML>中介绍了怎么使用HttpClient进行爬虫的第一步--抓取页面html,今天接着来看下爬虫的第二步--解析抓取 ...
- ThreadPoolExecutor系列三——ThreadPoolExecutor 源码解析
ThreadPoolExecutor 源码解析 本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7681826.htm ...
- 《HTML5编程之旅》系列三:WebSockets 技术解析
本文主要研究HTML5 WebSockets的使用方法,它是HTML5中最强大的通信功能,定义了一个全双工的通信信道,只需Web上的一个Socket即可进行通信,能减少不必要的网络流量并降低网络延迟. ...
- ES系列(三):网络通信模块解析
ES是一个分布式搜索引擎,其除了用户提供必要的通信服务外,集群间也必须保持紧密的通信联系,才能在必要的时候给出正确的结果.其则必然涉及到各种繁多且要求高的通信场景,那么如何实现高性能的通信,则是其必须 ...
- java基础解析系列(三)---HashMap
java基础解析系列(三)---HashMap java基础解析系列 java基础解析系列(一)---String.StringBuffer.StringBuilder java基础解析系列(二)-- ...
随机推荐
- 带你学习BFS最小步数模型
最小步数模型 一.简介 最小步数模型和最短路模型的区别? 最短路模型:某一个点到另一个点的最短距离(坐标与坐标之间) 最小步数模型:不再是点(坐标),而是状态到另一个状态的转变 BFS难点所在(最短路 ...
- 使用 ES Module 的正确姿势
前面我们在深入理解 ES Module 中详细介绍过 ES Module 的工作原理.目前,ES Module 已经在逐步得到各大浏览器厂商以及 NodeJS 的原生支持.像 vite 等新一代的构建 ...
- Mybatis插件,能做的事情真的很多
大家好,我是架构摆渡人.这是实践经验系列的第九篇文章,这个系列会给大家分享很多在实际工作中有用的经验,如果有收获,还请分享给更多的朋友. Mybatis是我们经常用的一款操作数据库的框架,它的插件机制 ...
- golang中的标准库template
html/template包实现了数据驱动的模板,用于生成可对抗代码注入的安全HTML输出.它提供了和text/template包相同的接口,Go语言中输出HTML的场景都应使用text/templa ...
- gin初识
Gin 是一个用 Go (Golang) 编写的 web 框架. 它是一个类似于 martini 但拥有更好性能的 API 框架, 由于 httprouter,速度提高了近 40 倍. 如果你是性能和 ...
- 平滑增加Nginx模块
目录 一:平滑增加Nginx模块 1.1.增加模块必须重新编译 一:平滑增加Nginx模块 1.1.增加模块必须重新编译 解决依赖 编译安装不能解决依赖 yum install zlib zlib-d ...
- 优化.NET 应用程序 CPU 和内存的11 个实践
https://michaelscodingspot.com/cpu-bound-memory-bound/ 优化.NET 应用程序 CPU 和内存的11 个实践 凡事都有其限度,对吧?汽车只能开这么 ...
- java中的数据模型类
package com.aaa.zxf.ajax.test; import java.io.Serializable; /** * java中的继承. * * 一.数据模型类 * 数据模型类:用来存取 ...
- uni微信小程序优化,几行代码就能省100kb的主包空间?
不是标题党,我们公司的项目确确实实是省下了100kb的主包空间,而且还是在没有牺牲任何的性能和业务的前提下实现的. 但是100kb是根据项目大小,所以你用这个插件可能省下超过100kb或者更少. 直接 ...
- Spring service本类中方法互相调用事物失效问题
简介 Spring事物利用的是AOP,动态代理采用CGLIB代理(默认,也可以用Proxy代理,但是Proxy代理效率低于CGLIB代理).故只要弄懂Spring的AOP实现,就知道为什么servic ...