D scripting (c++ integration)
Bill Baxter
dnewsgroup at billbaxter.com
Tue Oct 3 18:09:52 PDT 2006
mike wrote:
> Maybe for DMD 3.0 ...
>
> ' import std.compiler;
> '
> ' char[] source = "writefln(\"Hello World!\");";
> ' void delegate() foo = compile(source);
> ' foo();
>
> That would be just awesome :-)
>
That's pretty much what SciPy's weave.blitz and weave.inline do for
Python code.
Some example here: http://scipy.org/PerformancePython
Example:
# put expression in a string
expr = "u[1:-1, 1:-1] = ((u[0:-2, 1:-1] + u[2:, 1:-1])*dy2 + "\
"(u[1:-1,0:-2] + u[1:-1, 2:])*dx2)*dnr_inv"
# compile the expression and run it
weave.blitz(expr, check_size=0)
The first time you call this you take a hit, but the result is cached
(and a DLL saved on disk too), so subsequent calls are fast, as well as
subsequent runs of the program. This is a big win for optimizing the
inner loops of computations in Python code. 300x speed increase over
the original Python loop is not uncommon. The difference in speed would
clearly be less impressive in D, since D is already very fast, but still
it could be useful for building native-speed optimized expressions on
the fly. For instance, in a Photoshop type app you could imagine some
sort of image filter that gets some parameters from the user then
compiles the resulting parameters into to a binary format for fast
execution.
I don't think there'd be much point in using it to call "writefln"
though. :-)
The thing is you have to have the entire compiler and linker sitting
around to make this work.
But I don't see that there's anything about D that makes any of this
more feasible than it would be with C++. In fact scipy.weave is
compiling C++ code on the fly.
--bb
More information about the Digitalmars-d
mailing list