Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'scripts': 0.03; 'that?': 0.05; 'problem?': 0.07; 'subject:file': 0.07; 'dst': 0.09; 'f.close()': 0.09; 'filename': 0.09; 'friday,': 0.09; 'shutil': 0.09; 'subject:into': 0.09; 'subject:How': 0.10; 'python': 0.11; '"w")': 0.16; '###': 0.16; "'w')": 0.16; '126': 0.16; '50mb.': 0.16; 'cstringio': 0.16; 'dst)': 0.16; 'preexisting': 0.16; 'sec': 0.16; 'seconds.': 0.16; 'src': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'skip:f 30': 0.19; 'written': 0.21; '>>>': 0.22; 'import': 0.22; 'python?': 0.22; 'to:name:python-list@python.org': 0.22; 'received:65.55.116': 0.24; 'specify': 0.24; 'url:moin': 0.24; "i've": 0.25; 'least': 0.26; 'skip:" 20': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'skip:- 40': 0.29; 'am,': 0.29; 'url:mailman': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'that.': 0.31; 'url:wiki': 0.31; 'file': 0.32; 'run': 0.32; 'linux': 0.33; 'url:python': 0.33; 'fri,': 0.33; 'raw': 0.33; 'date:': 0.34; 'operations': 0.35; 'but': 0.35; 'surely': 0.36; 'url:listinfo': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'seconds': 0.37; 'email addr:python.org': 0.37; 'system,': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'subject:': 0.39; '\xa0\xa0\xa0': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'how': 0.40; 'read': 0.60; 'most': 0.60; 'numbers': 0.61; 'matter': 0.61; 'first': 0.61; 'times': 0.62; 'complete': 0.62; 'email addr:gmail.com': 0.63; 're:': 0.63; 'such': 0.63; 'reached': 0.63; 'choose': 0.64; 'email name:python- list': 0.65; 'levels': 0.65; 'size.': 0.65; 'capable': 0.67; 'results': 0.69; 'limit': 0.70; 'obvious': 0.74; 'increase': 0.74; '<<<': 0.84; 'fast,': 0.84; '2013': 0.98 X-TMN: [WdA7CbE9G7prPZ/FAdsRySeyH+fr6btw] X-Originating-Email: [carlosnepomuceno@outlook.com] From: Carlos Nepomuceno To: "python-list@python.org" Subject: RE: How to write fast into a file in python? Date: Fri, 17 May 2013 18:20:33 +0300 Importance: Normal In-Reply-To: <51961B73.2070401@davea.name> References: , <87f9a3d4-427e-472f-bee7-9501ba842b36@googlegroups.com>, <51961B73.2070401@davea.name> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginalArrivalTime: 17 May 2013 15:20:34.0165 (UTC) FILETIME=[137ABA50:01CE5312] 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: 110 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368804103 news.xs4all.nl 15983 [2001:888:2000:d::a6]:35565 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45475 I've got the following results on my desktop PC (Win7/Python2.7.5):=0A= =0A= C:\src\Python>python -m timeit -cvn3 -r3 "execfile('fastwrite2.py')"=0A= raw times: 123 126 125=0A= 3 loops=2C best of 3: 41 sec per loop=0A= =0A= C:\src\Python>python -m timeit -cvn3 -r3 "execfile('fastwrite5.py')"=0A= raw times: 34 34.3 34=0A= 3 loops=2C best of 3: 11.3 sec per loop=0A= =0A= C:\src\Python>python -m timeit -cvn3 -r3 "execfile('fastwrite6.py')"=0A= raw times: 0.4 0.447 0.391=0A= 3 loops=2C best of 3: 130 msec per loop=0A= =0A= =0A= If you can just copy a preexisting file it will surely increase the speed t= o the levels you need=2C but doing the cStringIO operations can reduce the = time in 72%.=0A= =0A= Strangely I just realised that the time it takes to complete such scripts i= s the same no matter what hard drive I choose to run them. The results are = the same for an SSD (main drive) and a HDD.=0A= =0A= I think it's very strange to take 11.3s to write 50MB (4.4MB/s) sequentiall= y on a SSD which is capable of 140MB/s.=0A= =0A= Is that a Python problem? Why does it take the same time on the HDD?=0A= =0A= =0A= ### fastwrite2.py ###=A0 <<< this is your code=0A= size =3D 50*1024*1024=0A= value =3D 0=0A= filename =3D 'fastwrite2.dat'=0A= with open(filename=2C "w") as f:=0A= =A0=A0=A0 while f.tell()< size:=0A= =A0=A0=A0=A0=A0=A0=A0 f.write("{0}\n".format(value))=0A= =A0=A0=A0=A0=A0=A0=A0 value +=3D 1=0A= =A0=A0=A0 f.close()=0A= =0A= =0A= ### fastwrite5.py ###=0A= import cStringIO=0A= size =3D 50*1024*1024=0A= value =3D 0=0A= filename =3D 'fastwrite5.dat'=0A= x =3D 0=0A= b =3D cStringIO.StringIO()=0A= while x < size:=0A= =A0=A0=A0 line =3D '{0}\n'.format(value)=0A= =A0=A0=A0 b.write(line)=0A= =A0=A0=A0 value +=3D 1=0A= =A0=A0=A0 x +=3D len(line)+1=0A= f =3D open(filename=2C 'w')=0A= f.write(b.getvalue())=0A= f.close()=0A= b.close()=0A= =0A= =0A= ### fastwrite6.py ###=0A= import shutil=0A= src =3D 'fastwrite.dat'=0A= dst =3D 'fastwrite6.dat'=0A= shutil.copyfile(src=2C dst)=0A= =0A= =0A= =0A= ----------------------------------------=0A= > Date: Fri=2C 17 May 2013 07:58:43 -0400=0A= > From: davea@davea.name=0A= > To: python-list@python.org=0A= > Subject: Re: How to write fast into a file in python?=0A= >=0A= > On 05/17/2013 12:35 AM=2C lokeshkoppaka@gmail.com wrote:=0A= >> On Friday=2C May 17=2C 2013 8:50:26 AM UTC+5:30=2C lokesh...@gmail.com w= rote:=0A= >>> I need to write numbers into a file upto 50mb and it should be fast=0A= >>>=0A= >>> can any one help me how to do that?=0A= >>>=0A= >>> i had written the following code..=0A= >>>=0A= >>> =0A= >>> value =3D 0=0A= >>>=0A= >>> with open(filename=2C "w") as f:=0A= >>>=0A= >>> while f.tell()< size:=0A= >>>=0A= >>> f.write("{0}\n".format(value))=0A= >>> =0A= > If you must use googlegroups=2C at least read this=0A= > http://wiki.python.org/moin/GoogleGroupsPython.=0A= >>>=0A= >>>=0A= >>> it takes about 20sec i need 5 to 10 times less than that.=0A= >> size =3D 50mb=0A= >>=0A= >=0A= > Most of the time is spent figuring out whether the file has reached its= =0A= > limit size. If you want Python to go fast=2C just specify the data. On=0A= > my Linux system=2C it takes 11 seconds to write the first 6338888 values= =2C=0A= > which is just under 50mb. If I write the obvious loop=2C writing that=0A= > many values takes .25 seconds.=0A= >=0A= > --=0A= > DaveA=0A= > --=0A= > http://mail.python.org/mailman/listinfo/python-list =