陈云佳 2019-12-04
以上两种场景中,json和table的互转使用频率是非常高的,如果转换效率有提升,对于降低我们业务延时将会有很大的帮助。因此,选择一种高效率的转换方式的重要性可见一斑。
下面我们简单介绍两种转换方式,并对他们的转换效率做一个对比。
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) 结果1:使用json转换后的输出,耗时为51秒。 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}}
表格的现在还是较为常用的一种标签,但不是用来布局,常见处理、显示表格式数据。在HTML网页中,要想创建表格,就需要使用表格相关的标签。<table> <tr> <td>单元格内的文字</td> ...