Most efficient way to write data out to a text file?

Dave Kuhlman dkuhlman at rexx.com
Sat Jun 29 14:45:32 EDT 2002


candiazoo at attbi.com wrote:

[snip]

Thank you for posting this.  Very helpful.

Could we have a bit of evaluation and explanation please.  I'm 
suspicious about whether some of these changes actually made a 
difference.  I'd like a little understanding before I start 
changing my Python coding style.

I'd also like some enlightenment about *why* they would make an 
improvement.

>  my aforementined optimizations...
> 
> 1) "import sys" became "import exit from sys"
> 2) "import os" became "import makedires from os"

Does importing a function from a module (as opposed to importing 
the whole module) save a look-up each time the function is called?

> 3) "import time" bacame "import asctime from time" (this one is
> used frequently) 
> 4) I localized constants I was using in boolean
> tests (there were declared at the beginning of the module, I moved
> them to the function call just before the loop).

Is finding things in globals slower than finding them in locals?  
Did this change in Python 2.2?

> 5) I localized self.__source_id...  source_id = self.__source_id
> before using it in every loop iteration.
> 6) I localized self.fix_date_range()... fix_date =
> self.fix_date_range before using it in every loop iteration/
> 7) I changed my method that built the output string by changing...
> 
> return self.val_a + self.value_b + etc..
> 
> ...to...
> 
> return "%-10.10s%-200.200s (etc)" % (self.val_a, self.val_b, etc.)
> 
> ...and those were basically the changes I made!  They made a huge
> difference!
> 
> Mike J.

Thanks in advance.

  - Dave

-- 
Dave Kuhlman
dkuhlman at rexx.com
http://www.rexx.com/~dkuhlman




More information about the Python-list mailing list