Last active
December 10, 2015 17:08
-
-
Save vincentwyshan/4465830 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf8 | |
import time | |
import simplejson as json | |
from cadaapi.stock.ttypes import * | |
from thrift.transport import TTransport | |
from thrift.protocol import TBinaryProtocol | |
def load(data): | |
head = data[0] | |
data = data[1:] | |
body = [] | |
head = [str(v) for v in head] | |
for row in data: | |
row = row[0] | |
try: | |
body.append(RatingByReport(**dict(zip(head,row)))) | |
except:import pdb;pdb.set_trace() | |
result = TableRatingByReport(head=head, body=body) | |
return result | |
def ser(data): | |
transportOut = TTransport.TMemoryBuffer() | |
protocolOut = TBinaryProtocol.TBinaryProtocol(transportOut) | |
data.write(protocolOut) | |
vals = transportOut.getvalue() | |
return vals | |
def test_thrift(data): | |
print >>open('/tmp/test.thrift', 'w'), ser(data) | |
now = time.time() | |
print 'start:', now | |
for i in range(10): | |
print '\tFile length:', len(ser(data)) | |
now2 = time.time() | |
print 'end:', now2 | |
print 'elapse:', now2-now | |
def test_json(data): | |
now = time.time() | |
print 'start:', now | |
for i in range(10): | |
print '\tFile length:', len(json.dumps(data)) | |
now2 = time.time() | |
print 'end:', now2 | |
print 'elapse:', now2-now | |
if __name__ == '__main__': | |
rawdata = json.loads(open('/tmp/test.json', 'r').read()) | |
data = load(rawdata) | |
print 'Test thrift' | |
test_thrift(data) | |
print '\n\nTest json' | |
test_json(rawdata) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment