MathGroup Archive 2003

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

Search the Archive

Re: Increase in efficiency with Module

  • To: mathgroup at smc.vnet.net
  • Subject: [mg40059] Re: Increase in efficiency with Module
  • From: atelesforos at hotmail.com (Orestis Vantzos)
  • Date: Tue, 18 Mar 2003 02:21:24 -0500 (EST)
  • References: <b5424l$mfr$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

ftauc2 is not Compiled properly.
Evaluate:
List@@ftauc
and
List@@ftauc2

All the Function[...] elements in ftauc2 mean that it is not compiled
properly, hence the poor performance.
The key concept is that CompiledFunctions must not refer to global
symbols...everything must either be a local variable(using Module) or
an argument.
In the case of ftauc2, i is not a local variable or a parameter and
therefore it 'taints' the compiled code.
Orestis


"Aaron E. Hirsh" <aehirsh at stanford.edu> wrote in message news:<b5424l$mfr$1 at smc.vnet.net>...
> I would be grateful if someone could explain the difference in 
> efficiency between the following two simple programs. ry is a list of 
> length n. For n = 1000, the program using Module takes 1 second on my 
> laptop, whereas the program without Module takes 75 seconds.
> 
> ftauc = Compile[{{ry, _Real, 1}, {n, _Real, 0}},
>     
>     Module[{i, 
> j, a},

>       
>       i = 1;
>       a = 0;
>       
>       Do[
>         
>  
> j = i + 1;
>         
>         Do[
>           If[ry[[i]] < ry[[j]], a++, 
> If[ry[[i]] > ry[[j]], a--]];
>           j++, {n - i}];
>         
>  
> i++, {n - 1}]; a
>       ]]
> 
> ftauc2 = Compile[{{ry, _Real, 1}, {n, _Real, 0}},
>     
>     
>     
>  
> i = 1;
>     a = 0;
>     
>     Do[
>       
>       j = i + 1;
>       
>  
> Do[
>         If[ry[[i]] < ry[[j]], a++, If[ry[[i]] > ry[[j]], a--]];
>  
> j++, {n - i}];
>       
>       i++, {n - 1}]; a
>     
>     ]
> 
> thank you,


  • Prev by Date: a challenge/problem.
  • Next by Date: Re: Increase in efficiency with Module
  • Previous by thread: Re: Increase in efficiency with Module
  • Next by thread: RE: Increase in efficiency with Module