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


Groups > comp.lang.python > #44131 > unrolled thread

Re: optomizations

Started byChris Angelico <rosuav@gmail.com>
First post2013-04-23 11:38 +1000
Last post2013-04-23 11:38 +1000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: optomizations Chris Angelico <rosuav@gmail.com> - 2013-04-23 11:38 +1000

#44131 — Re: optomizations

FromChris Angelico <rosuav@gmail.com>
Date2013-04-23 11:38 +1000
SubjectRe: optomizations
Message-ID<mailman.945.1366681100.3114.python-list@python.org>
On Tue, Apr 23, 2013 at 11:19 AM, Rodrick Brown <rodrick.brown@gmail.com> 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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web