在很多情况下,我们需要对复杂数据进行模拟测试,这里分享一个仿真随机数据生成,随机从1-100中抽取数据,随机生成两万次——当然可以自定义的,如果你的计算机足够强大,感觉就算生成数亿组数据也可以做到。
import random
with open('data.txt','w') as f1,\
open('data.txt','r') as f2:
for i in range(10000):
f1.write(U"%d\r\n"%random.randint(1,100))
print(f2.read())
#########
import random
import time
st=time.time()
with open('data.txt','w') as f1,\
open('data.txt','r') as f2:
for i in range(20000):
f1.write("%d\r"%random.randint(1,100))
print(f2.read())
et=time.time()
print("耗时",et-st,"秒")