MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Re: Compiling a Module

  • To: mathgroup at
  • Subject: [mg15995] RE: [mg15877] Re: Compiling a Module
  • From: "Ersek, Ted R" <ErsekTR at>
  • Date: Fri, 19 Feb 1999 03:27:09 -0500
  • Sender: owner-wri-mathgroup at

A member of this group wrote:
>Can anyone tell me, why compiling a module saves no time during

As another member of the group indicated a CompiledFunction doesn't run very
fast if it can't be represented using all op-code instructions.  If you have
a CompiledFunction named (func) you can see if any part of it didn't get
converted to op-code by evaluating func[[3]].  Often times this is much more
concise than FullForm[func]. What you get back will mostly be a list of
lists.  Most of the sub-lists will be vectors of integers.  The parts that
don't make it into op-code look like {31,Function[{x},......]}.  What you
see inside Function will be a part of your program the interpreter has a
problem with.

The function 'CheckCompiled' below takes a CompiledFunction and returns a
list of all the expressions that don't get converted to op-code.  If it
gives you an empty list you have an efficient CompiledFunction.



Some very helpful info is available at
This page includes a list of symbols version 3.0 can convert into op-code.

But even if every symbol you use is included on that page your program still
may not convert to op-code.  I hear this can happen for lots of reasons, but
I only know of a few (each discussed below).
A global variable can't be represented using op-code.  The compiled function
(f2) below isn't converted to op-code because the global variable (w) is
used in the function.  The best way around this that I know is to provide
the global variable to the function as an argument.

w = 0.25; 

f2 = Compile[{x}, (x^2 + Sqrt[Exp[x]]+w)];

Evaluate f2[[3]] and you will see the op-code translation of this program.

You can't use opcode to change the value of a function's argument.  The
CompiledFunction below doesn't convert to op-code because the function
changes the value of (x) (one of the function's arguments).



About a year ago a relatively long program using Compile had the same
problem as the short program below.  The solution with extensive discussion
was provided by Dave Withoff (of Wolfram Research).  The program below isn't
converted to op-code because of the line If[x<0, a=1.0].  When x<0 the If
returns a Real.  When x>=0 the If returns Null (Mathematica jargon for
nothing).  This is unacceptable for the interpreter so it doesn't get
converted to op-code.


To fix the program above change (a=1.0) to (a=1.0;) as below.  That way the
interpreter knows the If will always return 'Null'.

(* This will convert to op-code *)


Ted Ersek

  • Prev by Date: RE: Mathematica vs. Executable code
  • Next by Date: RE: Pure Functions in rules
  • Previous by thread: Re: Compiling a Module
  • Next by thread: graphing inequalities