Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Created October 30, 2016 05:03
Show Gist options
  • Save tanaikech/25e9bf5fbda71e68b6b6a49bea8cee7d to your computer and use it in GitHub Desktop.
Save tanaikech/25e9bf5fbda71e68b6b6a49bea8cee7d to your computer and use it in GitHub Desktop.
PythonでのCSVファイル出力の速度評価 ref: http://qiita.com/tanaike/items/f1a986a34e03dcbb7729
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import time
import csv
import pandas as pd
import SOUWA
def measure_csv(ar):
start = time.time()
with open('csvmod.csv', 'w') as f:
writer = csv.writer(f, lineterminator='\n')
writer.writerows(ar)
Processing_time = time.time() - start
print("Processing time = {0}".format(Processing_time) + " [s]")
return
def measure_pandas(ar):
start = time.time()
df = pd.DataFrame(ar)
df.to_csv('pandastest.csv', header=False, index=False)
Processing_time = time.time() - start
print("Processing time = {0}".format(Processing_time) + " [s]")
return
def measure_souwapy(ar):
start = time.time()
s = SOUWA.sou()
result = s.getcsvdata(ar, ",", "\n")
with open('souwa.csv', 'w') as f:
f.write(result)
Processing_time = time.time() - start
print("Processing time = {0}".format(Processing_time) + " [s]")
return
def measure_standard(ar):
start = time.time()
result = ''
for dat in ar:
result += ",".join(dat) + "\n"
with open('standard.csv', 'w') as f:
f.write(result)
Processing_time = time.time() - start
print("Processing time = {0}".format(Processing_time) + " [s]")
return
def MakeArray(row):
theta = [0 for i in range(row)]
for i in range(0, row):
theta[i] = [str(i + 1).zfill(9), 'a', 'b', 'c', 'd', 'e']
return theta
ar = MakeArray(10)
measure = 1
if measure == 1:
measure_csv(ar)
elif measure == 2:
measure_pandas(ar)
elif measure == 3:
measure_souwapy(ar)
elif measure == 4:
measure_standard(ar)
000000001,a,b,c,d,e
000000002,a,b,c,d,e
000000003,a,b,c,d,e
000000004,a,b,c,d,e
000000005,a,b,c,d,e
000000006,a,b,c,d,e
000000007,a,b,c,d,e
000000008,a,b,c,d,e
000000009,a,b,c,d,e
000000010,a,b,c,d,e
$ pip install souwapy
from souwapy import SOUWA
s = SOUWA.sou()
result = s.getcsvdata(array, ",", "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment