Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'cpython': 0.05; 'that?': 0.05; 'subject:file': 0.07; 'sys': 0.07; 'string': 0.09; 'friday,': 0.09; 'skip:# 30': 0.09; 'skip:- 110': 0.09; 'subject:into': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'def': 0.12; '"seconds': 0.16; '"w")': 0.16; 'cc:name:python list': 0.16; 'f.tell()': 0.16; 'size):': 0.16; 'stringio': 0.16; 'subject:python': 0.16; 'size,': 0.16; 'wrote:': 0.18; '<': 0.19; 'skip:f 30': 0.19; 'thu,': 0.19; 'written': 0.21; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; '>': 0.26; 'code:': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; "skip:' 10": 0.31; 'that.': 0.31; '"': 0.31; 'skip:- 100': 0.31; 'file': 0.32; 'skip:c 30': 0.32; 'url:python': 0.33; 'skip:# 10': 0.33; 'skip:& 30': 0.33; 'received:209.85': 0.35; 'received:google.com': 0.35; 'skip:f 40': 0.36; 'url:listinfo': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'seconds': 0.37; 'received:209': 0.37; 'skip:& 10': 0.38; 'pm,': 0.38; '\xa0\xa0\xa0': 0.39; 'url:mail': 0.40; 'how': 0.40; 'numbers': 0.61; 'times': 0.62; 'email addr:gmail.com': 0.63; 'to:addr:gmail.com': 0.65; 'is\xa0': 0.84; '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=s50Oe8QBpH6VUORI+XJlCf5tch5XGBu9LgT4dyhxZZk=; b=Lr2pGoLYYXuVVTYOajxzwN7fPwjziOo/B+1r1LQkOgxUeKefizp5FTkm3usNUAmu5r 9whNZGzx5Uz3gtux0lNTjZS0vhdMAMX6vfAP29D4N9Hsg6ph9IUr9UNU6xqNO+iCF9eA 4spy0SCeGpehoLpF7DZTa0/5Y9GG/+BBbbOrg2pVXnErYc7LhmUg94p5f6c7ibwNCgHL l3iNaChJANrDIstTmoG/H7VdxEpShfWesuP/qzeDPg5bSj1z5EJ/HtWsRcC58unYG9bf B7x8dWrxJV9cq2VU6XKbuL/qG/Uz1xmiyMuK2yNPMwCznIV0r4GwW1GB+GYc5AyNSigr bwcQ== MIME-Version: 1.0 X-Received: by 10.224.166.67 with SMTP id l3mr42308922qay.48.1368905910452; Sat, 18 May 2013 12:38:30 -0700 (PDT) In-Reply-To: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> References: <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com> Date: Sat, 18 May 2013 12:38:30 -0700 Subject: Re: How to write fast into a file in python? From: Dan Stromberg To: lokeshkoppaka@gmail.com Content-Type: multipart/alternative; boundary=047d7b62527c86241e04dd033ed7 Cc: Python List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 245 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368905919 news.xs4all.nl 15909 [2001:888:2000:d::a6]:57248 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45527 --047d7b62527c86241e04dd033ed7 Content-Type: text/plain; charset=ISO-8859-1 With CPython 2.7.3: ./t time taken to write a file of size 52428800 is 15.86 seconds time taken to write a file of size 52428800 is 7.91 seconds time taken to write a file of size 52428800 is 9.64 seconds With pypy-1.9: ./t time taken to write a file of size 52428800 is 3.708232 seconds time taken to write a file of size 52428800 is 4.868304 seconds time taken to write a file of size 52428800 is 1.93612 seconds Here's the code: #!/usr/local/pypy-1.9/bin/pypy #!/usr/bin/python import sys import time import StringIO sys.path.insert(0, '/usr/local/lib') import bufsock def create_file_numbers_old(filename, size): start = time.clock() value = 0 with open(filename, "w") as f: while f.tell() < size: f.write("{0}\n".format(value)) value += 1 end = time.clock() print "time taken to write a file of size", size, " is ", (end -start), "seconds \n" def create_file_numbers_bufsock(filename, intended_size): start = time.clock() value = 0 with open(filename, "w") as f: bs = bufsock.bufsock(f) actual_size = 0 while actual_size < intended_size: string = "{0}\n".format(value) actual_size += len(string) + 1 bs.write(string) value += 1 bs.flush() end = time.clock() print "time taken to write a file of size", intended_size, " is ", (end -start), "seconds \n" def create_file_numbers_file_like(filename, intended_size): start = time.clock() value = 0 with open(filename, "w") as f: file_like = StringIO.StringIO() actual_size = 0 while actual_size < intended_size: string = "{0}\n".format(value) actual_size += len(string) + 1 file_like.write(string) value += 1 file_like.seek(0) f.write(file_like.read()) end = time.clock() print "time taken to write a file of size", intended_size, " is ", (end -start), "seconds \n" create_file_numbers_old('output.txt', 50 * 2**20) create_file_numbers_bufsock('output2.txt', 50 * 2**20) create_file_numbers_file_like('output3.txt', 50 * 2**20) On Thu, May 16, 2013 at 9:35 PM, wrote: > On Friday, May 17, 2013 8:50:26 AM UTC+5:30, lokesh...@gmail.com wrote: > > I need to write numbers into a file upto 50mb and it should be fast > > > > can any one help me how to do that? > > > > i had written the following code.. > > > > > ----------------------------------------------------------------------------------------------------------- > > > > def create_file_numbers_old(filename, size): > > > > start = time.clock() > > > > > > > > value = 0 > > > > with open(filename, "w") as f: > > > > while f.tell()< size: > > > > f.write("{0}\n".format(value)) > > > > value += 1 > > > > > > > > end = time.clock() > > > > > > > > print "time taken to write a file of size", size, " is ", (end -start), > "seconds \n" > > > > > ------------------------------------------------------------------------------------------------------------------ > > > > it takes about 20sec i need 5 to 10 times less than that. > size = 50mb > -- > http://mail.python.org/mailman/listinfo/python-list > --047d7b62527c86241e04dd033ed7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

With CPython 2.7.3:
./t
tim= e taken to write a file of size 52428800=A0 is=A0 15.86 seconds

time= taken to write a file of size 52428800=A0 is=A0 7.91 seconds

time t= aken to write a file of size 52428800=A0 is=A0 9.64 seconds


With pypy-1.9:
./t
time taken to write a file of size 5= 2428800=A0 is=A0 3.708232 seconds

time taken to write a file of size= 52428800=A0 is=A0 4.868304 seconds

time taken to write a file of si= ze 52428800=A0 is=A0 1.93612 seconds


Here's the code:
#!/usr/local/pypy-1.9/bin/pypy
#!/= usr/bin/python

import sys
import time
import StringIO

s= ys.path.insert(0, '/usr/local/lib')
import bufsock

def cr= eate_file_numbers_old(filename, size):
=A0=A0=A0 start =3D time.clock()

=A0=A0=A0 value =3D 0
=A0=A0=A0 = with open(filename, "w") as f:
=A0=A0=A0=A0=A0=A0=A0 while f.t= ell() < size:
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 f.write("{0}\n&q= uot;.format(value))
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 value +=3D 1
=A0=A0=A0 end =3D time.clock()

=A0=A0=A0 print "time taken to write a file of size", size, &= quot; is ", (end -start), "seconds \n"

def create_fil= e_numbers_bufsock(filename, intended_size):
=A0=A0=A0 start =3D time.clo= ck()

=A0=A0=A0 value =3D 0
=A0=A0=A0 with open(filename, "w") as f:=
=A0=A0=A0=A0=A0=A0=A0 bs =3D bufsock.bufsock(f)
=A0=A0=A0=A0=A0=A0= =A0 actual_size =3D 0
=A0=A0=A0=A0=A0=A0=A0 while actual_size < inten= ded_size:
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 string =3D "{0}\n"= .format(value)
=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 actual_size +=3D len(string) + 1
=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 bs.write(string)
=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0 value +=3D 1
=A0=A0=A0=A0=A0=A0=A0 bs.flush()

=A0=A0=A0= end =3D time.clock()

=A0=A0=A0 print "time taken to write a fi= le of size", intended_size, " is ", (end -start), "seco= nds \n"


def create_file_numbers_file_like(filename, intended_size):
=A0= =A0=A0 start =3D time.clock()

=A0=A0=A0 value =3D 0
=A0=A0=A0 wit= h open(filename, "w") as f:
=A0=A0=A0=A0=A0=A0=A0 file_like = =3D StringIO.StringIO()
=A0=A0=A0=A0=A0=A0=A0 actual_size =3D 0
=A0=A0=A0=A0=A0=A0=A0 while actual_size < intended_size:
=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0 string =3D "{0}\n".format(value)
=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0 actual_size +=3D len(string) + 1
=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0 file_like.write(string)
=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0 value +=3D 1
=A0=A0=A0=A0=A0=A0=A0 file_like.seek(0)
=A0=A0=A0=A0=A0=A0=A0 f.write(file_like.read())

=A0=A0=A0 end =3D ti= me.clock()

=A0=A0=A0 print "time taken to write a file of size&= quot;, intended_size, " is ", (end -start), "seconds \n"= ;

create_file_numbers_old('output.txt', 50 * 2**20)
create_file_numbers_bufsock('output2.txt', 50 * 2**20)
create_fi= le_numbers_file_like('output3.txt', 50 * 2**20)




On Thu, May 16,= 2013 at 9:35 PM, <lokeshkoppaka@gmail.com> wrote:
On F= riday, May 17, 2013 8:50:26 AM UTC+5:30, lokesh...@gmail.com wrote:
> I need to write numbers into a file upto 50mb and it should be fast >
> can any one help me how to do that?
>
> i had written the following code..
>
> ----------------------------------------------------------------------= -------------------------------------
>
> def create_file_numbers_old(filename, size):
>
> start =3D time.clock()
>
>
>
> value =3D 0
>
> with open(filename, "w") as f:
>
> while f.tell()< size:
>
> f.write("{0}\n".format(value))
>
> value +=3D 1
>
>
>
> end =3D time.clock()
>
>
>
> print "time taken to write a file of size", size, " is = ", (end -start), "seconds \n"
>
> ----------------------------------------------------------------------= --------------------------------------------
>
> it takes about 20sec i need 5 to 10 times less than that.
size =3D 50mb
--
http://mail.python.org/mailman/listinfo/python-list

--047d7b62527c86241e04dd033ed7--