Created
February 23, 2013 11:12
-
-
Save draftcode/5019366 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env python | |
| # vim: fileencoding=utf-8 | |
| from __future__ import division, absolute_import, print_function, unicode_literals | |
| import os.path | |
| import unicode_csv | |
| import io | |
| writer = unicode_csv.writer(io.open(os.path.expanduser("~/large.csv"), 'w', encoding='cp932')) | |
| row = [unicode(i) for i in range(200)] | |
| for i in range(100000): | |
| writer.writerow(row) | |
This file contains hidden or 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
| # vim: fileencoding=utf-8 | |
| from __future__ import division, absolute_import, print_function, unicode_literals | |
| import csv | |
| import datetime | |
| import io | |
| import os.path | |
| import unicode_csv | |
| CSV_PATH = os.path.expanduser("~/large.csv") | |
| NUM = 10 | |
| def calc(reader_f): | |
| s = 0.0 | |
| for i in range(NUM): | |
| start = datetime.datetime.now() | |
| for row in reader_f(): | |
| pass | |
| end = datetime.datetime.now() | |
| d = end - start | |
| s += d.seconds*1000000 + d.microseconds | |
| return s/NUM | |
| def csv_reader(): | |
| return csv.reader(open(CSV_PATH, 'rb')) | |
| def unicode_csv_reader(): | |
| return unicode_csv.reader(io.open(CSV_PATH, 'r', encoding='cp932')) | |
| readers = { | |
| 'csv': csv_reader, | |
| 'unicode_csv': unicode_csv_reader, | |
| } | |
| for key, reader in readers.iteritems(): | |
| t = calc(reader) | |
| print("{}:\t{}msec".format(key, t)) | |
| # csv: 1977300.0msec | |
| # unicode_csv: 2374700.0msec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment