Idiom for running compiled python scripts?

Steven D'Aprano steve at REMOVE.THIS.cybersource.com.au
Fri Mar 23 07:24:07 EDT 2007


On Fri, 23 Mar 2007 07:30:58 +0000, Mark wrote:

> On Fri, 23 Mar 2007 14:03:12 +1100, Steven D'Aprano wrote:
>> Since you've done these tests already, perhaps you can tell us what gain
>> you actually got?
> 
> About the same as you, ~20 msecs for my small script samples.

Well, I think that pretty much answers your question about whether it is
worth pre-compiling short shell scripts: you save about 20ms in execution
time, and lose 200ms in typing time. (Maybe a bit less if you are a
fast typist and don't use auto-completion.) You do the maths.


>> Of course you have to type the "c". You're not deleting the source files
>> away are you? *wink*
> 
> Sorry, the wink is lost on me?

It is because I didn't really think you were deleting the source files.
That would be incredibly stupid. But I mentioned it just in case some
not-so-bright spark decided to argue that you could use auto-completion
without needing to type that final "c" if you deleted the source file.

Presumably now somebody is going to suggest merely *moving* the source
files into another directory, thus spending a minute or two each time they
edit a script re-arranging files in order to save twenty or thirty
milliseconds when they execute the script. Hey, if your time is so
valuable that 20ms means that much to you, go for it.


> Of course I am not deleting the sources. In fact, I am also talking
> about python scripts being called from shell scripts. I guess I'm just
> surprised that the python installation does not provide a small stub
> invoker, e.g:
> 
> A small script called "python_compile_and_run" in "pseudo" code:

[snip pseudo-code]

> so I could just do a "python_compile_and_run myscript.py" and it would
> do what I want, i.e. run myscript.pyc if available and valid, generate
> and run it if necessary.

You shouldn't expect Python to come with every imaginable special-purpose
script already written for you! Besides, it's pretty simple to get that
functionality by hand when you need it, or automatically for that matter.

Here's one (untested) script that executes the pyc file in a subshell if
it exists and is new enough, and compiles it if it doesn't.


import os, sys, compiler
from stat import ST_MTIME as MT
if __name__ == "__main__":
    scriptname = sys.argv[1]
    compiledname = scriptname + "c"
    if not os.path.exists(compiledname) or \
    os.stat(compiledname)[MT] < os.stat(scriptname)[MT]:
        # compiled file doesn't exist, or is too old
        compiler.compileFile(scriptname)
        assert os.path.exists(compiledname)
    resultcode = os.system('python %s' % compiledname)
    sys.exit(resultcode)

Now don't forget to test whether launching the subshell takes longer than
the 20ms you might save. All that effort, and wouldn't it be ironic if it
was actually *slower* than executing the script from scratch each time...


-- 
Steven.




More information about the Python-list mailing list