dimanche 15 mai 2016

Generating 100 million integers using Python

I am writing an application to generate 10 thousand to 100 million integers and I am unsure whether a .txt file is the right representation to hold the integers. Below is my code:

import random
def printrandomInts(n,file):
    for i in range(n):
        x = random.random();
        x = x * 10000000
        x = int(x)
        file.write(str(x))
        file.write("\n")
     file.close()

file = open("10k","w")

n = 10000
printrandomInts(n,file)
file = open("100k","w")
n*=10
printrandomInts(n,file)
file = open("1M","w")
n*=10
printrandomInts(n,file)
file = open("10M","w")
n*=10
printrandomInts(n,file)
file = open("100M","w")
printrandomInts(n*10,file)

When I run the above code, the size of the largest file Windows reports is 868,053 KB. Should I use binary representation to efficiently represent the integers. I also have to generate similar data for floats and strings. What should I do to make things more space efficient?




Aucun commentaire:

Enregistrer un commentaire