Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45549

RE: How to write fast into a file in python?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <carlosnepomuceno@outlook.com>
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; 'python': 0.11; 'def': 0.12; '"seconds': 0.16; '"w")': 0.16; 'cstringio': 0.16; 'f.tell()': 0.16; 'instead:': 0.16; 'pypy.': 0.16; 'size):': 0.16; 'stringio': 0.16; 'to:name:python list': 0.16; 'subject:python': 0.16; 'sat,': 0.16; 'size,': 0.16; 'wrote:': 0.18; 'skip:f 30': 0.19; 'thu,': 0.19; 'later': 0.20; 'written': 0.21; 'import': 0.22; 'cc:addr:gmail.com': 0.22; 'python?': 0.22; 'print': 0.22; 'received:65.55.116': 0.24; 'earlier': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'source': 0.25; 'code:': 0.26; 'downloaded': 0.26; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; '[1]': 0.29; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; 'page.': 0.31; "skip:' 10": 0.31; 'that.': 0.31; 'skip:- 100': 0.31; 'file': 0.32; 'skip:c 30': 0.32; 'url:python': 0.33; 'skip:# 10': 0.33; 'date:': 0.34; 'but': 0.35; 'url:listinfo': 0.36; 'thanks': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'seconds': 0.37; 'email addr:python.org': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'skip:_ 30': 0.39; 'subject:': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'how': 0.40; 'remove': 0.60; 'numbers': 0.61; 'took': 0.61; 'times': 0.62; 'email addr:gmail.com': 0.63; 're:': 0.63; 'email name:python- list': 0.65; 'between': 0.67; 'is\xa0': 0.84; '2013': 0.98
X-TMN [slRKimDVV2OFeWazbp9oPVS6yEyGV41Y]
X-Originating-Email [carlosnepomuceno@outlook.com]
From Carlos Nepomuceno <carlosnepomuceno@outlook.com>
To Python List <python-list@python.org>
Subject RE: How to write fast into a file in python?
Date Sun, 19 May 2013 08:31:08 +0300
Importance Normal
In-Reply-To <CAGGBd_ot27sFCi_hZkytwpP-M2oyxEtz60v9zLppWsWsLLiFOQ@mail.gmail.com>
References <CAGGBd_ot27sFCi_hZkytwpP-M2oyxEtz60v9zLppWsWsLLiFOQ@mail.gmail.com>
Content-Type text/plain; charset="iso-8859-1"
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
X-OriginalArrivalTime 19 May 2013 05:31:08.0242 (UTC) FILETIME=[1092A320:01CE5452]
Cc "lokeshkoppaka@gmail.com" <lokeshkoppaka@gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1833.1368941537.3114.python-list@python.org> (permalink)
Lines 182
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1368941537 news.xs4all.nl 15947 [2001:888:2000:d::a6]:52401
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:45549

Show key headers only | View raw


Thanks Dan! I've never used CPython or PyPy. Will try them later.

I think the main difference between your create_file_numbers_file_like()
 and the fastwrite5.py I sent earlier is that I've used cStringIO 
instead of StringIO. It took 12s less using cStringIO.

My numbers are much greater, but I've used Python 2.7.5 instead:

C:\src\Python>python create_file_numbers.py
time taken to write a file of size 52428800  is  39.1199457743 seconds

time taken to write a file of size 52428800  is  14.8704800436 seconds

time taken to write a file of size 52428800  is  23.0011990985 seconds


I've downloaded bufsock.py and python2x3.py. The later one was hard to remove the source code from the web page.

Can I use them on my projects? I'm not used to the UCI license[1]. What's the difference to the GPL?




[1] http://stromberg.dnsalias.org/~dstromberg/UCI-license.html

________________________________
> Date: Sat, 18 May 2013 12:38:30 -0700 
> Subject: Re: How to write fast into a file in python? 
> From: drsalists@gmail.com 
> To: lokeshkoppaka@gmail.com 
> CC: python-list@python.org 
>  
>  
> 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,  
> <lokeshkoppaka@gmail.com<mailto:lokeshkoppaka@gmail.com>> wrote: 
> On Friday, May 17, 2013 8:50:26 AM UTC+5:30,  
> lokesh...@gmail.com<mailto: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 
>  
>  
> -- http://mail.python.org/mailman/listinfo/python-list 		 	   		  

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to write fast into a file in python? lokeshkoppaka@gmail.com - 2013-05-16 20:20 -0700
  Re: How to write fast into a file in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-17 03:51 +0000
  Re: How to write fast into a file in python? lokeshkoppaka@gmail.com - 2013-05-16 21:35 -0700
    Re: How to write fast into a file in python? Dave Angel <davea@davea.name> - 2013-05-17 07:58 -0400
    RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-17 18:20 +0300
      Re: How to write fast into a file in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-17 16:42 +0000
        RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-17 20:25 +0300
      Re: How to write fast into a file in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-17 17:47 +0000
        RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-17 21:18 +0300
          Re: How to write fast into a file in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-18 04:01 +0000
            Re: How to write fast into a file in python? Chris Angelico <rosuav@gmail.com> - 2013-05-18 15:28 +1000
            Re: How to write fast into a file in python? 88888 Dihedral <dihedral88888@googlemail.com> - 2013-05-18 04:09 -0700
        RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-17 21:33 +0300
        RE: How to write fast into a file in python? Fábio Santos <fabiosantosart@gmail.com> - 2013-05-18 08:49 +0100
        Re: How to write fast into a file in python? Chris Angelico <rosuav@gmail.com> - 2013-05-19 00:29 +1000
        RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-18 20:00 +0300
          Re: How to write fast into a file in python? Tim Roberts <timr@probo.com> - 2013-05-19 19:04 -0700
        Re: How to write fast into a file in python? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-05-18 15:14 -0400
          Re: How to write fast into a file in python? Roy Smith <roy@panix.com> - 2013-05-18 15:37 -0400
          Re: How to write fast into a file in python? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-18 22:23 +0000
        Re: How to write fast into a file in python? Fábio Santos <fabiosantosart@gmail.com> - 2013-05-18 22:19 +0100
        Re: How to write fast into a file in python? Dave Angel <davea@davea.name> - 2013-05-18 22:41 -0400
        RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-19 06:53 +0300
        Re: How to write fast into a file in python? MRAB <python@mrabarnett.plus.com> - 2013-05-19 16:44 +0100
        RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-20 13:34 +0300
    Re: How to write fast into a file in python? Dan Stromberg <drsalists@gmail.com> - 2013-05-18 12:38 -0700
    RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-19 08:31 +0300
    RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-19 08:42 +0300
    Re: How to write fast into a file in python? Chris Angelico <rosuav@gmail.com> - 2013-05-19 19:21 +1000
    RE: How to write fast into a file in python? Carlos Nepomuceno <carlosnepomuceno@outlook.com> - 2013-05-19 12:41 +0300

csiph-web