我正在尝试使用多线程加速磁盘上的写入时间。为了看到有效性,我编写了以下代码,测量两种方法之间的时间:
from __future__ import print_function
import random, time
import numpy as np
import threading
def file_write(filepath, V):
filepath = open(filepath, 'w')
np.savetxt(filepath, V, fmt='%i')
filepath.close()
if __name__ == "__main__":
filepath = 'output.txt'
V = 10000
dim = 128
print('Starting generating ...')
V_ = [random.randint(0, 2 ** dim) for _ in range(V)]
V = list(set(V_))
V = [('{0:0' + str(dim) + 'b}').format(x) for x in V]
V = np.asarray([list(map(int, list(x))) for x in V], dtype=np.uint8)
# This is Non-Threading write
t0 = time.time()
file_write(filepath, V)
t1 = time.time()
print('\n\n>> No-Thread Writing Time= %.2f '% (t1-t0))
# Generating 10 threads
thread_list = []
for i in range(1, 10):
t = threading.Thread(target=file_write, args=(filepath, V))
thread_list.append(t)
# Starts threads
t0 = time.time()
for thread in thread_list:
thread.start()
# Closing threads
for thread in thread_list:
thread.join()
t1 = time.time()
print('\n\n>> 10-Threads Writing Time= %.2f ' % (t1 - t0))
结果令人惊讶如下。线程比非线程工作需要更长的时间。不应该是相反的吗?我认为线程更快!
Starting generating ...
>> No-Thread Writing Time= 0.26
>> 10-Threads Writing Time= 2.44