Skip to content

Instantly share code, notes, and snippets.

@inokappa
Created December 23, 2016 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inokappa/7877f659757fedf202355a19ec9e383c to your computer and use it in GitHub Desktop.
Save inokappa/7877f659757fedf202355a19ec9e383c to your computer and use it in GitHub Desktop.
json モジュールの速度比較(Python 3 系)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from timer import Timer
import csv
import json
import simplejson
import ujson
JSON_FILE='dummy_1000.json'
CSV_FILE='dummy_1000.csv'
print('decode -----------------------------------------------')
with open(JSON_FILE) as f:
with Timer() as t:
data = json.load(f)
print('%s' % t.secs)
# print('=> elasped json : %s s' % t.secs)
with open(JSON_FILE) as f:
with Timer() as t:
data = simplejson.load(f)
print('%s' % t.secs)
# print('=> elasped simplejson : %s s' % t.secs)
with open(JSON_FILE) as f:
with Timer() as t:
data = ujson.load(f)
print('%s' % t.secs)
# print('=> elasped ujson : %s s' % t.secs)
print('encode -----------------------------------------------')
with open(CSV_FILE) as f:
with Timer() as t:
result = []
for line in csv.DictReader(f):
line_json = json.dumps(line)
result.append(line_json)
print('%s' % t.secs)
# print('=> elasped json : %s s' % t.secs)
with open(CSV_FILE) as f:
with Timer() as t:
result = []
for line in csv.DictReader(f):
line_json = simplejson.dumps(line)
result.append(line_json)
print('%s' % t.secs)
# print('=> elasped simplejson : %s s' % t.secs)
with open(CSV_FILE) as f:
with Timer() as t:
result = []
for line in csv.DictReader(f):
line_json = ujson.dumps(line)
result.append(line_json)
print('%s' % t.secs)
# print('=> elasped ujson : %s s' % t.secs)
@inokappa
Copy link
Author

  • timer.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import time

class Timer(object):
    def __init__(self, verbose=False):
        self.verbose = verbose

    def __enter__(self):
        self.start = time.time()
        return self

    def __exit__(self, *args):
        self.end = time.time()
        self.secs = self.end - self.start
        self.msecs = self.secs * 1000  # millisecs
        if self.verbose:
            print('elapsed time: %f ms' % self.msecs)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment