yyjson vs luajit buffer vs lua-cjson

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 非insitu下
yyjson-0.3.0-debug decode file 28ms, decode str 17ms, encode 24ms
yyjson-0.3.0-release decode file 13-14ms, decode str 8-12ms, encode 7-10ms
-- yyjson insitu下
yyjson-0.3.0-insitu-release decode file 14ms, decode str 10ms, encode 9ms

其他lua库:
[json.lua](https://github.com/rxi/json.lua): encode 211ms, decode 235ms
[lua-cjson](https://github.com/openresty/lua-cjson.git)(使用resty执行):encode 49ms, decode 112ms

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

#include "yyjson.h"
#include 
#include 

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)

你可能感兴趣的