Guest User

Untitled

a guest
Aug 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """Which json is the best json."""
  5.  
  6. import json
  7. import simplejson as sjson
  8. import ujson
  9. import cjson
  10. import bson
  11. cbson = bson.BSON
  12.  
  13. assert bson._use_c
  14.  
  15. import msgpack
  16. import cPickle as pickle
  17. import marshal
  18.  
  19.  
  20. import sys
  21. import time
  22. from glob import glob
  23.  
  24. def fread(path):
  25. with open(path) as f:
  26. data = f.read()
  27. return data
  28.  
  29. def stdout(string):
  30. sys.stdout.write(string)
  31. sys.stdout.flush()
  32.  
  33. jsonfiles = list(sorted(glob("json/*")))
  34. msgpackfiles = list(sorted(glob("mpk/*")))
  35. marshalfiles = list(sorted(glob("mar/*")))
  36. picklefiles = list(sorted(glob("pickle/*")))
  37. bsonfiles = list(sorted(glob("bson/*")))
  38.  
  39. # -- preload data
  40. print "preloading files ..."
  41.  
  42. print "reading json ..."
  43. numfiles = len(jsonfiles)
  44. jsondata = []
  45. for i,file in enumerate(jsonfiles):
  46. jsondata.append(fread(file))
  47. stdout('%d of %d\r' % (i+1, numfiles))
  48. stdout("\n")
  49.  
  50. print "reading msgpack ..."
  51. numfiles = len(msgpackfiles)
  52. msgpackdata = []
  53. for i,file in enumerate(msgpackfiles):
  54. msgpackdata.append(fread(file))
  55. stdout('%d of %d\r' % (i+1, numfiles))
  56. stdout("\n")
  57.  
  58. print "reading marshal ..."
  59. numfiles = len(marshalfiles)
  60. marshaldata = []
  61. for i,file in enumerate(marshalfiles):
  62. marshaldata.append(fread(file))
  63. stdout('%d of %d\r' % (i+1, numfiles))
  64. stdout("\n")
  65.  
  66. print "reading pickle ..."
  67. numfiles = len(picklefiles)
  68. pickledata = []
  69. for i,file in enumerate(picklefiles):
  70. pickledata.append(fread(file))
  71. stdout('%d of %d\r' % (i+1, numfiles))
  72. stdout("\n")
  73.  
  74. print "reading bson ..."
  75. numfiles = len(bsonfiles)
  76. bsondata = []
  77. for i,file in enumerate(bsonfiles):
  78. bsondata.append(fread(file))
  79. stdout('%d of %d\r' % (i+1, numfiles))
  80. stdout("\n")
  81.  
  82. print "\ndecoding test:\n"
  83. td = {}
  84.  
  85. t0 = time.time()
  86. [json.loads(s) for s in jsondata]
  87. td['json'] = time.time() - t0
  88.  
  89. t0 = time.time()
  90. [sjson.loads(s) for s in jsondata]
  91. td['simplejson'] = time.time() - t0
  92.  
  93. t0 = time.time()
  94. [cjson.decode(s) for s in jsondata]
  95. td['cjson'] = time.time() - t0
  96.  
  97. t0 = time.time()
  98. [ujson.loads(s) for s in jsondata]
  99. td['ujson'] = time.time() - t0
  100.  
  101. t0 = time.time()
  102. [msgpack.loads(s) for s in msgpackdata]
  103. td['msgpack'] = time.time() - t0
  104.  
  105. t0 = time.time()
  106. [cbson(s).decode() for s in bsondata]
  107. td['bson'] = time.time() - t0
  108.  
  109. t0 = time.time()
  110. [marshal.loads(s) for s in marshaldata]
  111. td['marshal'] = time.time() - t0
  112.  
  113. t0 = time.time()
  114. [pickle.loads(s) for s in pickledata]
  115. td['pickle'] = time.time() - t0
  116.  
  117. for name in sorted(td):
  118. print name.ljust(15), "%0.2fs" % td[name]
  119.  
  120. encoded = [ujson.loads(s) for s in jsondata]
  121.  
  122. del jsondata
  123. del msgpackdata
  124. del marshaldata
  125. del pickledata
  126. del bsondata
  127.  
  128.  
  129. print "\nencoding test:\n"
  130.  
  131. td = {}
  132.  
  133. t0 = time.time()
  134. for s in encoded:
  135. json.dumps(s)
  136. td['json'] = time.time() - t0
  137.  
  138. t0 = time.time()
  139. for s in encoded:
  140. sjson.dumps(s)
  141. td['simplejson'] = time.time() - t0
  142.  
  143. t0 = time.time()
  144. for s in encoded:
  145. cjson.encode(s)
  146. td['cjson'] = time.time() - t0
  147.  
  148. t0 = time.time()
  149. for s in encoded:
  150. ujson.dumps(s)
  151. td['ujson'] = time.time() - t0
  152.  
  153. t0 = time.time()
  154. for s in encoded:
  155. msgpack.dumps(s)
  156. td['msgpack'] = time.time() - t0
  157.  
  158. t0 = time.time()
  159. for s in encoded:
  160. marshal.dumps(s)
  161. td['marshal'] = time.time() - t0
  162.  
  163. t0 = time.time()
  164. for s in encoded:
  165. pickle.dumps(s)
  166. td['pickle'] = time.time() - t0
  167.  
  168. t0 = time.time()
  169. for s in encoded:
  170. cbson.encode(s)
  171. td['bson'] = time.time() - t0
  172.  
  173. for name in sorted(td):
  174. print name.ljust(15), "%0.2fs" % td[name]
Add Comment
Please, Sign In to add comment