Compile lesson
- To: mathgroup@smc.vnet.net
- Subject: [mg12308] Compile lesson
- From: Ersek_Ted%PAX1A@mr.nawcad.navy.mil
- Date: Thu, 7 May 1998 18:51:50 -0400
Way back on 24 April 1997 Dave Withoff debugged some Compile code a user
sent to the mathgroup. Before Dave debugged the code it ran slower
than normal Mathematica code. Dave explained that you should try to
get nothing but a list of pseudocode instructions in the compiled
function.
More precisely, if you say
In[1]:= funct=Compile[{vars}, blah; blah; blah ] Then you should be
able to do:
In[2]:= funct[[3]]
and get a list of lists, where each of the sublists are all integers.
As I understand it your compiled function will execute quickly as long
as can be converted to all psuedocode instructions. Function "demo1"
below is an example of a function that was compiled into 100%
pseudocode instructions. This is good.
In[1]:=
demo1=Compile[{x},Module[{y=0.0},
If[x<0,y=2*x;];
y+5 ]];
demo1[[3]]
Out[1]=
{{1,2},{4,1,0},{15,0.,1},{14,0,0},{24,0,2},{98,0,2,0},{90,0,6},{14,2,1},{24,
1,
3},{38,3,0,3},{20,3,1},{91,1},{14,5,1},{24,1,3},{34,1,3,3},{9,3}}
____________________________________
The next line has the same problem as the function Dave debugged. The
If statement will return a Real if (x<0), and return Null if (x>=0).
The compiler doesn't know what the If statement will return, so it
isn't translated into pseudocode.
In[2]:=
demo2=Compile[{x},Module[{y=0.0},
If[x<0,y=2*x];
y+5]];
demo2[[3]]
Out[2]=
{{1,2},{4,1,0},{15,0.,1},{14,0,0},{24,0,2},{98,0,2,0},{31,
Function[{x},If[x<0,y=2
x]],{y,3,0,1,Module},6,0,17},{14,5,1},{24,1,3},{
34,1,3,3},{9,3}}
________________________________________ The next function tries to
change the value of the function's argument. This can't be done with
pseudocode instructions, so standard Mathematica code is used.
In[3]:=
demo3=Compile[{x}, If[x>10^20,x=10^20;]; ]; demo3[[3]]
Out[3]=
{{1, 2}, {4, 1, 0}, {14, 10, 0}, {37, 0, 0, 1},
{37, 1, 1, 1, 2}, {19, 2, 1}, {37, 1, 1, 1, 2},
{37, 0, 0, 2, 1}, {24, 1, 1}, {98, 1, 0, 0},
{90, 0, 3}, {31, Function[{x}, x = 10^20], 6, 0, 17},
{91, 1}, {12, 187}}
________________________________________ The next function tries to use
a Global variable. We can't do this either with pseudocode
instructions, so standard Mathematica code is used.
In[4]:=
num=10;
demo4=Compile[{x},x+num];
demo4[[3]]
Out[4]=
{{1,2},{4,1,0},{31,Function[{x},num],3,0,1},{34,0,1,1},{9,1}}
________________________________________ Any Mathematica function can be
used inside compile, but there may be no advantage in using Compile if
some of the functions can't be translated into pseudocode. As
demonstrated above your code may not translate into pseudocode even if
all the functions used are supported by Compile.
In the lines above I gave a few reasons why Compile may not be able to
translate a function into pseudocode. If any members of the group have
other things to avoid inside Compile I would like to hear about them.
Ted Ersek
- Follow-Ups:
- Re: Compile lesson
- From: Ahmed Maarouf <maarouf@student.physics.upenn.edu>
- Re: Compile lesson