yyjson vs luajit buffer vs lua-cjson

字节追梦人
• 阅读 2724

luajit新推出了buffer接口,yyjson是几乎就是最快json c库(不使用simd指令)
比较yyjson与luajit buffer的性能。
测试json文件:使用rapidjson性能测试仓库中的测试文件:data目录下的三个文件: "./canada.json", "./twitter.json", "./citm_catalog.json"

测试机:Manjaro 21/5.10.60 linux内核,gcc-11.1.0, P50(Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz, 8G)
测试结论
Luajit的buffer编码性能是yyjson 2-3倍, 解码性能与yyjson相当, 但是luajit buffer解码出来是table非常方便查询, 且buffer还有元词典扩展功能未使用。json文本,buffer二进制格式。

luajit-2.1 buffer encode 4-5ms, decode 8-11ms
yyjson :
yyjson-0.3.0-debug encode 24ms, decode file 28ms, decode str 17ms
yyjson-0.3.0-release encode 7-10ms, decode file 13-14ms, decode str 8-12ms
-- yyjson insitu下
yyjson-0.3.0-insitu-release encode 9ms, decode file 14ms, decode str 10ms

其他库:
cJSON: encode: 207ms, decode:76ms;(-O3时:encode:193ms, decode:53ms)
json.lua: luajit: encode 235ms, decode 211ms;(lua5.4.3: encode:440ms, decode:738ms)
lua-cjson:resty-1.19.9.1 执行:encode 112ms, decode 49ms;(lua5.4.3: encode:120ms, decode:71ms)

yyjson代码:
cmake -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release ..
cmake --build . --config Release

#include "yyjson.h"
#include <stdio.h>
#include <time.h>

int main(){
    struct timespec s = {0,0}, e = {0,0}, e2 = {0,0};
    clock_gettime(CLOCK_MONOTONIC, &s);
    yyjson_doc *doc1 = yyjson_read_file("./canada.json", 0, NULL, NULL);
    yyjson_doc *doc2 = yyjson_read_file("./twitter.json", 0, NULL, NULL);
    yyjson_doc *doc3 = yyjson_read_file("./citm_catalog.json", 0, NULL, NULL);
    clock_gettime(CLOCK_MONOTONIC, &e);
    printf("%d\n", ((e.tv_sec - s.tv_sec) * 10^9 + e.tv_nsec - s.tv_nsec)/1000000);
    char *json1 = yyjson_write(doc1, 0, NULL);
    char *json2 = yyjson_write(doc2, 0, NULL);
    char *json3 = yyjson_write(doc3, 0, NULL);

    clock_gettime(CLOCK_MONOTONIC, &s);
    yyjson_doc *doc11 = yyjson_read(json1, strlen(json1), 0);
    yyjson_doc *doc12 = yyjson_read(json2, strlen(json2), 0);
    yyjson_doc *doc13 = yyjson_read(json3, strlen(json3), 0);
    clock_gettime(CLOCK_MONOTONIC, &e);
    printf("%d\n", ((e.tv_sec - s.tv_sec) * 10^9 + e.tv_nsec - s.tv_nsec)/1000000);
    char *json11 = yyjson_write(doc11, 0, NULL);
    char *json12 = yyjson_write(doc12, 0, NULL);
    char *json13 = yyjson_write(doc13, 0, NULL);
    clock_gettime(CLOCK_MONOTONIC, &e2);
    printf("%d\n", ((e2.tv_sec - e.tv_sec) * 10^9 + e2.tv_nsec - e.tv_nsec)/1000000);
    // free(json1);free(json2);free(json3);
    // yyjson_doc_free(doc1);
    // yyjson_doc_free(doc2);
    // yyjson_doc_free(doc3);
    return 0;
}

luajit buffer 测试代码:


local buffer = require("string.buffer")
local json = require("json")

-- local json = require("cjson")
-- json.encode_escape_forward_slash(false)

local a = {"./canada.json", "./twitter.json", "./citm_catalog.json"}
local file = {}
for _, n in ipairs(a) do
    local f = io.open(n)
    file[#file + 1] = f:read("a")
    f:close()
end

local s = os.clock()
local decfile = {}
for i, f in ipairs(file) do
    decfile[i] = json.decode(f)
end
local e = os.clock()
print("json decode:", e - s)


local s = os.clock()
for _, jt in ipairs(decfile) do
    local b = json.encode(jt)
end
local e = os.clock()
print("json encode:", e - s)


local a = {}
local s = os.clock()
for i, jt in ipairs(decfile) do
    a[i] = buffer.encode(jt)
end
local e = os.clock()
print("luajit buffer encode:", e - s)
-- print(#a[1])

local b = {}
local s = os.clock()
for i, jt in ipairs(a) do
    b[i] = buffer.decode(jt)
end
local e = os.clock()
print("luajit buffer decode:", e - s)
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Easter79 Easter79
3年前
sql注入
反引号是个比较特别的字符,下面记录下怎么利用0x00SQL注入反引号可利用在分隔符及注释作用,不过使用范围只于表名、数据库名、字段名、起别名这些场景,下面具体说下1)表名payload:select\from\users\whereuser\_id1limit0,1;!(https://o
Jacquelyn38 Jacquelyn38
4年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
SpringBoot学习:整合shiro自动登录功能(rememberMe记住我功能)
首先在shiro配置类中注入rememberMe管理器!复制代码(https://oscimg.oschina.net/oscnet/675f5689159acfa2c39c91f4df40a00ce0f.gif)/cookie对象;rememberMeCookie()方法是设置Cookie的生成模
Wesley13 Wesley13
3年前
Lua中cJson的读写
这里采用的是LuaCJson库,是一个高性能的JSON解析器和编码器,其性能比纯Lua库要高10~20倍。并且LuaJson完全支持UTF8,无需以来其他非Lua/LuaJit相关包。环境安装这里就不详细写了,随便问下谷歌就有一大堆答案。示例代码解析JSONlocal cjson  requi
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Easter79 Easter79
3年前
SpringBoot学习:整合shiro自动登录功能(rememberMe记住我功能)
首先在shiro配置类中注入rememberMe管理器!复制代码(https://oscimg.oschina.net/oscnet/675f5689159acfa2c39c91f4df40a00ce0f.gif)/cookie对象;rememberMeCookie()方法是设置Cookie的生成模
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
字节追梦人
字节追梦人
Lv1
我一直在你身后从未离开,只要你能回头
文章
3
粉丝
0
获赞
0