Fastest way to write huge data in file

后端 未结 3 1903
感动是毒
感动是毒 2020-12-05 15:07

I am trying to create a random real, integers, alphanumeric, alpha strings and then writing to a file till the file size reaches 10MB.

The code is a

3条回答
  •  时光取名叫无心
    2020-12-05 16:00

    The while loop under main calls generate_alphanumeric, which chooses several characters out of (fresh randomly generated) strings composed of twelve ascii letters and twelve numbers. That's basically the same as choosing randomly either a random letter or a random number twelve times. That's your main bottleneck. This version will make your code one order of magnitude faster:

    def generate_alphanumeric(self):
        res = ''
        for i in range(12):
            if random.randrange(2):
                res += random.choice(string.ascii_lowercase)
            else:
                res += random.choice(string.digits)
        return res
    

    I'm sure it can be improved upon. I suggest you take your profiler for a spin.

提交回复
热议问题