python array module

🏠

Write a python program that loads 10 million floats into an array, and writes them to disk.

 1 from array import array
 2 from random import random
 3 
 4 def main():
 5     floats = array('d', (random() for i in range(10**7)))
 6     fp = open('floats.bin', 'wb')
 7     floats.tofile(fp)
 8     fp.close()
 9     floats2 = array('d')
10     fp = open('floats.bin', 'rb')
11     floats2.fromfile(fp, 10**7)
12     fp.close()
13     print(floats == floats2)
14 
15 main()

Output:

1 real  0m2.989s
2 user   0m2.407s
3 sys    0m0.273s