python JSON性能测试与simplejson对比

简单测试了一下,如果用JSON,也就是python2.6以上自带的json处理库,效率还算可以:

1K的数据,2.9GHz的CPU,单核下每秒能dump:36898次。大约是pyamf的5倍。但数据量较大,约为pyamf的1.67倍(1101/656)。

start_time: 1370747463.77
loop_num: 36898
end_time:   1370747464.78

 

再看看simplejson,没有安装C扩展的情况下:

simplejson,没有安装C扩展,跑出的结果让我惊讶:

start_time: 1370748132.87
loop_num: 1361
end_time:   1370748133.88

效率如此之低下。

 

下面是测试代码:

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

 

总结:python自带的json,性能可以接受。simplejson,如果没有C扩展加速,效率极其低下。

posted @ 2015-10-21 14:20  功夫 熊猫  阅读(3429)  评论(0编辑  收藏  举报