Skip to content

Instantly share code, notes, and snippets.

@hankliu5
Created April 2, 2018 02:24
Show Gist options
  • Save hankliu5/507623477a1d11fa79751b9d2ff8a361 to your computer and use it in GitHub Desktop.
Save hankliu5/507623477a1d11fa79751b9d2ff8a361 to your computer and use it in GitHub Desktop.
import numpy as np
import time
import sys
import csv
import mmap
ret = []
current_time = time.time()
with open(sys.argv[1], 'r') as f:
for line in f:
floats = map(float, line.split(' '))
ret.append(floats)
x = np.asarray(ret)
convert_time = time.time() - current_time
print('customized function convertion time: {} secs'.format(convert_time))
ret = []
current_time = time.time()
with open(sys.argv[1], 'r+b') as f:
mm = mmap.mmap(f.fileno(), 0)
line = mm.readline()
while line != '':
ret.append(map(float, line.split(' ')))
line = mm.readline()
y = np.asarray(ret)
convert_time = time.time() - current_time
print('mmap convertion time: {} secs'.format(convert_time))
ret = []
current_time = time.time()
with open(sys.argv[1], 'r') as f:
reader = csv.reader(f, delimiter=' ')
for row in reader:
floats = map(float, row)
ret.append(floats)
z = np.asarray(ret)
convert_time = time.time() - current_time
A = np.load(sys.argv[2])
print('CSV convertion time: {} secs'.format(convert_time))
print('Customized Function Correctness: {}'.format(np.array_equal(x, A)))
print('mmap Function Correctness: {}'.format(np.array_equal(y, A)))
print('CSV Function Correctness: {}'.format(np.array_equal(z, A)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment