以上兩種場景中,json和table的互轉使用頻率是很是高的,若是轉換效率有提高,對於下降咱們業務延時將會有很大的幫助。所以,選擇一種高效率的轉換方式的重要性可見一斑。git
下面咱們簡單介紹兩種轉換方式,並對他們的轉換效率作一個對比。github
local t = { a = "hello", b = "world", c = 123456, d = "123456", e = {"hhh", "11", "22"}, f = { a = "hello", b = "world", c = 123456, d = "123456", e = {"hhh", "11", "22"}, count = 0 } } local tm1 = os.time() local str_json = "" for i = 1, 50000, 1 do --str_json = json.encode(t) --t = json.decode(str_json) str_json = cjson.encode(t) t = cjson.decode(str_json) t.f.count = t.f.count + 1 end local tm2 = os.time() print("tm1:" .. tm1 .. ",tm2:" .. tm2 .. ",dt:" .. (tm2 - tm1)) print(str_json)
tm1:1575363384,tm2:1575363435,dt:51 {"a":"hello","c":123456,"b":"world","e":["hhh","11","22"],"d":"123456","f":{"a":"hello","c":123456,"b":"world","e":["hhh","11","22"],"d":"123456","count":49999}}
tm1:1575363457,tm2:1575363462,dt:5 {"a":"hello","c":123456,"b":"world","e":["hhh","11","22"],"d":"123456","f":{"a":"hello","c":123456,"b":"world","e":["hhh","11","22"],"d":"123456","count":49999}}