'''
测试任务:计算密集型程序与IO密集型程序
'''
#计算密集型
def count(x,y):
c = 0
while c < 6000000:
c += 1
x += 1
y += 1
#IO密集函数
def write():
f = open('test.txt','w')
for i in range(1000000):
f.write('hello world\n')
f.close()
def read():
f = open('test.txt','r')
lines = f.readlines()
f.close()
def io():
write()
read()
'''
单进程测试,两个程序各执行10次
'''
from test_func import *
import time
t = time.time()
#CPU密集型程序
#for i in range(10):
# count(1,1)
#IO密集型程序
for i in range(10):
write()
read() # 单线程1.9秒
print('Line cpu:',time.time() - t)
'''
多线程测试
'''
import threading
from test_func import *
import time
t = time.time()
threads = []
def main1():
# 10个线程计算CPU密集型程序
for i in range(10):
thread = threading.Thread(target = count,args = (1,1))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
def main():
# 10个线程执行IO密集型程序
for i in range(10):
thread = threading.Thread(target = io)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
if __name__ == '__main__':
main()
print('time is',time.time() - t)