This question has been asked many times on SO (for instance here), but there is no real answer yet.
I am writing a short command line tool that renders templates. It is frigged using a Makefile:
i = $(wildcard *.in)
o = $(patsubst %.in, %.out, $(t))
all: $(o)
%.out: %.in
./script.py -o $@ $<
In this dummy example, the Makefile parses every .in
file to generate an .out
file. It is very convenient for me to use make
because I have a lot of other actions to trig before and after this script. Moreover I would like to remain as KISS as possible.
Thus, I want to keep my tool simple, stupid and process each file separately using the syntax
script -o out in
My script uses the following:
#!/usr/bin/env python
from jinja2 import Template, nodes
from jinja2.ext import Extension
import hiyapyco
import argparse
import re
...
The problem is that each execution costs me about 1.2s ( ~60ms for the processing and ~1140ms for the import directives):
$ time ./script.py -o foo.out foo.in
real 0m1.625s
user 0m0.452s
sys 0m1.185s
The overall execution of my Makefile for 100 files is ridiculous: ~100 files x 1.2s = 120s.
This is not a solution, but this should be the solution.
What alternative can I use?
EDIT
I love Python because its syntax is readable and size of its community. In this particular case (command line tools), I have to admit Perl is still a decent alternative. The same script written in Perl (which is also an interpreted language) is about 12 times faster (using Text::Xslate
).
I don't want to promote Perl in anyway I am just trying to solve my biggest issue with Python: it is not yet a suitable language for simple command line tools because of the poor import time.