Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.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.044 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'needed,': 0.07; 'advance': 0.07; 'f.close()': 0.09; "wouldn't": 0.14; '......': 0.16; '11:19': 0.16; '23,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wrote:': 0.18; 'not,': 0.20; 'certainly': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "skip:' 40": 0.31; 'open': 0.33; "i'd": 0.34; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'disk': 0.36; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; "you're": 0.61; 'name': 0.63; 'close': 0.67; 'end.': 0.84; "it'd": 0.84; 'subject:skip:o 10': 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:content-type; bh=7iQCpZuHAHmYOBROtHL/025VzRWgcxwvSKHQf4LjaLE=; b=Gv1t+sY4g3WvHuwC358KyHGEj6IhDAtUa4TAfMwUpHwhTq7soxivu+xxSogC+ttGPy Dil3YF04BOIcd8iFeMmZqbJzspGbeZI27ezepK3IEtTdSROyA9pyn1rtdmfgzczBK0T5 QQ+7BCMo37pBjlPHwN3KZi5iqfanCo8fSgzYg+Hpkchy2RMCnHDz03n7sRXXmdblCtYc Urnn9eiELE+OJo7UfwZabcu+uEHr1Xv6tMDQ+vM6gJoi/MS5Wxg9yqNVWd7MAvOD6mXj Plvm8DMqcPMvIJlzr5NIrSsj4w9JieYbVbCLQugbvrLu1mSvT+KjDHf8joEbx8cLbdug P/ag== MIME-Version: 1.0 X-Received: by 10.220.83.138 with SMTP id f10mr3390069vcl.7.1366681098094; Mon, 22 Apr 2013 18:38:18 -0700 (PDT) In-Reply-To: References: Date: Tue, 23 Apr 2013 11:38:17 +1000 Subject: Re: optomizations From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366681100 news.xs4all.nl 2246 [2001:888:2000:d::a6]:50100 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44131 On Tue, Apr 23, 2013 at 11:19 AM, Rodrick Brown wrote: > with gzip.open(args.inputfile) as datafile: > for line in datafile: > outfile = '{}{}{}_combined.log'.format(dateobj.year, > dateobj.month, dateobj.day) > outdir = (args.outputdir + os.sep + siteurl) > > with open(outdir + os.sep + outfile, 'w+') as outf: > outf.write(line) You're opening files and closing them again for every line. This wouldn't cause you to spin the CPU (more likely it'd thrash the hard disk - unless you have an SSD), but it is certainly an optimization target. Can you know in advance what files you need? If not, I'd try something like this: outf = {} # Might want a better name though ..... outfile = ... if outfile not in outf: os.makedirs(...) outf[outfile] = open(...) outf[outfile].write(line) for f in outf.values(): f.close() Open files only as needed, close 'em all at the end. ChrisA