Last active
May 12, 2019 20:16
-
-
Save tsukanov-as/d1532303e16d757e3fa9e70ccb21d64b to your computer and use it in GitHub Desktop.
LuaJit 2.1 beta 3 vs Rust 1.34.1 https://habr.com/ru/post/450512/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local json = require('rapidjson') | |
local path = "C:/temp/1.json" | |
local file = io.open(path, 'r') | |
local debtors, debtorsMap, company, phones, phone, debt, debtor | |
debtors = {} | |
debtorsMap = {} | |
local s = file:read(256*1024) | |
local v, h | |
local b, e | |
function find_object(init) | |
local x = 0 | |
local b = s:find("{", init, true) | |
if b then | |
local str, esc = false, false | |
for i = b, #s do | |
local c = s:byte(i) | |
if esc then | |
esc = false | |
elseif str then | |
if c == 0x5C then -- \ | |
esc = true | |
elseif c == 0x22 then | |
str = false | |
end | |
elseif c == 0x22 then -- " | |
str = true | |
elseif c == 0x7B then -- { | |
x = x + 1 | |
elseif c == 0x7D then -- } | |
x = x - 1 | |
if x == 0 then | |
return b, i | |
end | |
end | |
end | |
end | |
return b, nil | |
end | |
while s do | |
if h then | |
s = h..s | |
end | |
b, e = find_object(1) | |
while e do | |
v = json.decode(s:sub(b, e)) | |
company = v.company | |
if type(company) == 'table' then | |
company = company.name | |
end | |
assert(company) | |
phones = v.phones or {} | |
if type(phones) ~= 'table' then | |
phones = {phones} | |
end | |
phones[#phones+1] = v.phone | |
debt = v.debt or 0 | |
for _, phone in ipairs(phones) do | |
debtor = debtorsMap[phone] | |
if debtor ~= nil then | |
break | |
end | |
end | |
if debtor == nil then | |
debtor = { | |
companies = {}, | |
phones = {}, | |
debt = 0 | |
} | |
debtors[#debtors+1] = debtor | |
end | |
debtor.companies[company] = true | |
debtor.debt = debtor.debt + debt | |
for _, phone in ipairs(phones) do | |
debtor.phones[phone] = true | |
debtorsMap[phone] = debtor | |
end | |
b, e = find_object(e) | |
end | |
h = b and s:sub(b) | |
s = file:read(256*1024) | |
end | |
function out_list(src) | |
local t = {} | |
for k in pairs(src) do | |
t[#t+1] = k | |
end | |
return table.concat(t, ", ") | |
end | |
print("--------------------------") | |
for _, d in ipairs(debtors) do | |
print("debt: "..d.debt) | |
print("companies: "..out_list(d.companies)) | |
print("phones:"..out_list(d.phones)) | |
print("--------------------------") | |
end | |
print('done', os.clock(), 'sec.') | |
--[[ | |
.\luajit.exe -v | |
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/ | |
.\luajit.exe .\test.lua | |
-------------------------- | |
debt: 910000000 | |
companies: Шестерочка, Рога и копыта, Первая коллекторская | |
phones:788, 456, 789, 2128506, 123, 234 | |
-------------------------- | |
debt: 433200000 | |
companies: Святой престол, Казачий спас | |
phones:234567, 666, 345678 | |
-------------------------- | |
done 1.353 sec. | |
--]] |
Новая версия на Rust: https://gist.github.com/red75prime/53111e066cb1a32721c461b24eadbe6e
.\rustjson.exe -f "C:\temp\1.json"
1.2950531s
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Новая версия на Rust: https://github.com/epishman/habr_samples/blob/master/jsonparse/main.rs
.\rustjson.exe "C:\temp\1.json" -t 6 sync
634.3834ms
.\rustjson.exe "C:\temp\1.json" -t 6 async
501.4707ms
.\rustjson.exe "C:\temp\1.json" -t 1 async
1.9714653s