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


Groups > comp.lang.python > #63909

Re: Python Fast I/o

Path csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'string.': 0.05; 'subject:Python': 0.06; 'string': 0.09; 'data:': 0.09; 'f.close()': 0.09; 'separately': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'jan': 0.12; '12:50': 0.16; 'chunks': 0.16; 'chunks.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iterating': 0.16; 'posting,': 0.16; 'quoted': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'memory': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'string,': 0.24; 'file.': 0.24; 'cc:2**0': 0.24; '15,': 0.26; 'right.': 0.26; 'switch': 0.26; 'this:': 0.26; 'subject:/': 0.26; 'header:In- Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'compared': 0.30; 'fastest': 0.30; 'list:': 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'code': 0.31; 'that.': 0.31; 'becoming': 0.31; 'file': 0.32; 'thanks!': 0.32; 'text': 0.33; 'url:python': 0.33; 'bugs': 0.33; 'operations': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'google': 0.35; 'building': 0.35; '14,': 0.36; 'data,': 0.36; 'scheme': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'january': 0.37; 'two': 0.37; 'list': 0.37; 'project': 0.37; 'performance': 0.37; 'skip:o 20': 0.38; 'presently': 0.38; 'that,': 0.38; 'anything': 0.39; 'mailing': 0.39; 'either': 0.39; 'url:mail': 0.40; 'even': 0.60; 'worry': 0.60; 'details.': 0.61; "you're": 0.61; 'first': 0.61; 'making': 0.63; 'more': 0.64; "'with'": 0.84; 'fast?': 0.84; 'subject:Fast': 0.84; 'you;': 0.84; 'sectors,': 0.91; 'to:none': 0.92; 'tied': 0.93; 'hundred': 0.95
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=dePxZwLH4kLzxFiVLDkZLGAK2rMkyKmjNDma/3BqeME=; b=dM5xT1exxZYGIEki4cUDGmwPW5T2FoBcD3wbYEsfcEu+Lhd2vfMseMhFZR+By8huvQ h2BM9gPT+krD6E/xrxt2qaMpxRQsSzDRteHLRU6yLcjJYm9bho4ynI+d2qApzHPQHJKf UiIqMOMuruqIWYOfeH2t7/yo8o9aQT8w4xEDlhbCJrh/gDLFKF7NLwDxS5QnWqdG9PFy vEXfLYOuQ1ZMWvb6xlOYlHT6OpvojUAlHZL2SEkYOdieelka7VL0l3wrDloB//r+KGAk uw4AZuNK/NTxHgvSS0LLQ49ihh1n/eW9eI+Kua96Iz9vGqF+QIHsYM3yifVzPy5Y3ZV+ Z47g==
MIME-Version 1.0
X-Received by 10.68.247.6 with SMTP id ya6mr2053520pbc.45.1389709964303; Tue, 14 Jan 2014 06:32:44 -0800 (PST)
In-Reply-To <53affb01-0c5e-46e3-9ff7-27a529db6425@googlegroups.com>
References <a9c545f2-fbc3-4ac4-81ee-a91f61c72b84@googlegroups.com> <mailman.5458.1389708197.18130.python-list@python.org> <53affb01-0c5e-46e3-9ff7-27a529db6425@googlegroups.com>
Date Wed, 15 Jan 2014 01:32:44 +1100
Subject Re: Python Fast I/o
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5461.1389709967.18130.python-list@python.org> (permalink)
Lines 68
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389709967 news.xs4all.nl 2831 [2001:888:2000:d::a6]:52192
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63909

Show key headers only | View raw


On Wed, Jan 15, 2014 at 1:24 AM, Ayushi Dalmia
<ayushidalmia2604@gmail.com> wrote:
> On Tuesday, January 14, 2014 7:33:08 PM UTC+5:30, Chris Angelico wrote:
>> On Wed, Jan 15, 2014 at 12:50 AM, Ayushi Dalmia
>>
>> <ayushidalmia2604@gmail.com> wrote:
>>
>> > I need to write into a file for a project which will be evaluated on the basis of time. What is the fastest way to write 200 Mb of data, accumulated as a list into a file.
>>
>> >
>>
>> > Presently I am using this:
>>
>> >
>>
>> > with open('index.txt','w') as f:
>>
>> >       f.write("".join(data))
>>
>> >       f.close()
>>
>>
>>
>> with open('index.txt','w') as f:
>>
>>     for hunk in data:
>>
>>         f.write(hunk)
>>
>>
>>
>> You don't need to f.close() - that's what the 'with' block guarantees.
>>
>> Iterating over data and writing each block separately means you don't
>>
>> have to first build up a 200MB string. After that, your performance is
>>
>> going to be mainly tied to the speed of your disk, not anything that
>>
>> Python can affect.
>>
>>
>>
>> ChrisA

Your quoted text is becoming double spaced, because of bugs in the
Google Groups client. Please either edit this before posting, or
switch to a better newsreader, or use the mailing list:

https://mail.python.org/mailman/listinfo/python-list

Thanks!

> Which is more fast?
> Creating a 200 Mb string and then dumping into a file or dividing the 200 Mb string into chunks and then writing those chunks. Won't writing the chunks call more i/o operation?
>

When you're writing two hundred megabytes, the number of I/O
operations is dominated by that. You have to write that many sectors,
and nothing can change that. Joining your list of strings before
writing incurs the cost of building up a single 200MB string, but even
that is likely to be insignificant in the scheme of things (if you
have the memory available, it won't take very long compared to the
time it takes to write to the disk). Python will buffer its writes, so
you don't have to worry about the details. It's going to do the right
thing for you; you can concentrate on making your code look right.

ChrisA

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


Thread

Python Fast I/o Ayushi Dalmia <ayushidalmia2604@gmail.com> - 2014-01-14 05:50 -0800
  Re: Python Fast I/o Chris Angelico <rosuav@gmail.com> - 2014-01-15 01:03 +1100
    Re: Python Fast I/o Ayushi Dalmia <ayushidalmia2604@gmail.com> - 2014-01-14 06:24 -0800
      Re: Python Fast I/o Chris Angelico <rosuav@gmail.com> - 2014-01-15 01:32 +1100
      Re: Python Fast I/o Roy Smith <roy@panix.com> - 2014-01-14 09:40 -0500
  Re: Python Fast I/o Tim Chase <python.list@tim.thechases.com> - 2014-01-14 08:18 -0600

csiph-web