python3.6 json、simplejson(没安装C拓展)、ujson的性能对比 ujson性能最好!

测试代码为:

 1 import time
 2 import json
 3 # import ujson
 4 # import simplejson
 5 test_data = {
 6     'baihe': {
 7         'name': ('百合', 'utf-8'),
 8         'say': ('清新,淡雅,花香', 'utf-8'),
 9         'grow_time': 0.5,
10         'fruit_time':  0.5,
11         'super_time': 0.5,
12         'total_time': 1,
13         'buy':{'gold':2, } ,
14         'harvest_fruit': 1,
15         'harvest_super': 1,
16         'sale': 1,
17         'level_need': 0,
18         'experience' : 2,
19         'exp_fruit': 1,
20         'exp_super': 1,
21         'used': True,
22     },
23     '1':{
24         'interval' : 0.3,
25         'probability' : {
26             '98': {'chips' : (5, 25), },
27             '2' : {'gem' : (1,1), },
28         },
29     },
30     '2':{
31         'unlock' : {'chips':1000, 'FC':10,},
32         'interval' : 12,
33         'probability' : {
34             '70': {'chips' : (120, 250), },
35             '20': {'gem' : (1,1), },
36             '10': {'gem' : (2,2), },
37         },
38     },
39     'one':{
40         '10,5' :{'id':'m01', 'Y':1, 'msg':u'在罐子里发现了一个银币!',},
41         '3,7'  :{'id':'m02', 'Y':10,'msg':u'发现了十个银币!好大一笔钱!',},
42         '15,5' :{'id':'m03', 'Y':2, 'msg':u'一只老鼠跑了过去',},
43         '7,4'  :{'id':'m04', 'Y':4, 'msg':u'发现了四个生锈的银币……',},
44         '2,12' :{'id':'m05', 'Y':6, 'msg':u'六个闪亮的银币!',},
45     },
46 }
47 start_time = time.time()
48 print ("start_time:", start_time)
49 j = 1
50 while True:
51     j += 1
52     a = json.dumps(test_data)
53     # a = ujson.dumps(test_data)
54     # a = simplejson.dumps(test_data)
55     data_length = len(a)
56     end_time = time.time()
57     if end_time - start_time >= 1 :
58         break
59 print ("loop_num:", j)
60 print ("end_time:  ",end_time)
61 print (data_length ,a)
查看测试代码

3次测试都在在我的笔记本上测试,即然同一配置环境下,:

json 测试3次


start_time: 1596546865.35845 loop_num: 37571 end_time: 1596546866.3591375

start_time: 1596546903.352182 loop_num: 36305 end_time: 1596546904.3528645


start_time: 1596546945.0092795 loop_num: 38542 end_time: 1596546946.009977

平均数为:(37571+36305+38542)/3= 37472.666666666664



simplejson 测3次


start_time: 1596547174.9759703 loop_num: 23419 end_time: 1596547175.9766564


start_time: 1596547194.0049856 loop_num: 22503 end_time: 1596547195.0056722

start_time: 1596547248.2294648 loop_num: 23353 end_time: 1596547249.2301195

平均数为:(23419+22503+23353)/3=23091.666666666668

 

ujson 测试3次:


start_time: 1596547361.6813502 loop_num: 74860 end_time: 1596547362.6820362

start_time: 1596547385.706816 loop_num: 75275 end_time: 1596547386.7074997

start_time: 1596547408.5539103 loop_num: 77128 end_time: 1596547409.5545647

平均数为:(74860+75275+77128)/3=75754.33333333333

 

 

 

结果显而易见:ujson >  json > simplejson(未安装C扩展)

 

posted @ 2020-08-04 21:27  Mozoltov  阅读(355)  评论(0)    收藏  举报