Best way to write a file n-bytes long
Dialtone
dialtone#NOSPAM#.despammed at aruba.it
Tue Aug 26 18:30:29 EDT 2003
cappy2112 at yahoo.com (Tony C) writes:
> Sure, I could create a string that is n Megabytes long, and pass it to
> write, but it seems as though there should be a better way ???
> String manipulation is typically considered slow.
>
> st="X" * 1048576
> fh=open("junk","wb")
> fh.write(st)
> fh.close()
For little files (less than 2 or 3 Mb I think) your code is the
fastest I can think of. But growing There is a new version which is a
lot faster:
In [14]: def p3 (a):
....: tmp = time.time()
....: str = 'X'*1024
....: fh = file('junk','wb')
....: for i in xrange(a):
....: fh.write(str)
....: print time.time()-tmp
....:
In [14]: def p4 (a):
....: tmp = time.time()
....: str = 'X'*(1024*a) #Parenthesis are fundamental here
....: fh = file('junk','wb')
....: fh.write(str)
....: fh.close()
....: print time.time()-tmp
....:
In [15]: p3(10240) # 10Mb file
0.478311061859
In [16]: p4(10240)
0.625810027122
And this is only slightly faster, If we go up a lil bit more (100Mb)
we get to:
In [17]: p3(100000) # This is a 100 Mb file, so not really little
# and performance is not that bad, 25Mb/s
4.00167894363
In [18]: p4(100000)
8.85632002354
Otherwise if we stay down (1K):
In [22]: p3(1)
0.00810098648071
In [23]: p4(1)
0.000607967376709
HTH
--
Valentino Volonghi, Regia SpA, Milan
Linux User #310274, Debian Sid Proud User
More information about the Python-list
mailing list