create a tmp file for system execution

Nick Mathewson QnickQm at alum.mit.edu
Thu May 30 23:40:33 EDT 2002


In article <3CF4E711.8A175798 at millfilm.co.uk>, Eric Texier wrote:
> I have a py script executing a bunch of os.system in a loop.
> It is not very fast and I was wondering if it will not be better
> to recreate a execution file.
> 
> My 2 questions:
> 
> 1) what is faster for a big number of call
> 
> for i in range(1,2000):
>     os.system("ls  F1.%d   F2.%d" % ( i , i ) "  )
> 
> OR
> 
> fileOut = open("tmpFile",w)
> for i in range(1,2000):
>     fileOut.write("ls  F1.%d   F2.%d" % ( i , i ) " )
> 
> fileOut.close()
> os.system("csh -c 'source tmpFile' ")
> os.system("rm -f tmpFile")

Which is faster?  It probably depends on your platform, but I'd be
surprised if the second option was much faster than the
first... unless csh bundles its own 'ls' implementation (which tcsh,
at least does).

Let's think about what's going on here.  In the first case, the Python
program either needs to handle the request itself (which it won't) or
fork a new process for each os.system call.  In the second case, csh
needs to handle each request itself (which it can for "ls"), or fork a
new process.

The =real= solution might be to reimplement the os.system call in
Python.  If you're doing os.ls, for instance, you can call os.listdir
instead.  That will be faster than either alternative.

Optimally Yours,

-- 
 Nick Mathewson    <Q nick Q m at alum dot mit dot edu>
                      Remove Q's to respond.  No spam.



More information about the Python-list mailing list