乐趣区

关于luajit:yyjson-vs-luajit-buffer-vs-luacjson

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 <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)
退出移动版