Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2003
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2003

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

Search the Archive

Re: Re: recode procedural algorithm to faster functional module

  • To: mathgroup at smc.vnet.net
  • Subject: [mg44087] Re: [mg44078] Re: recode procedural algorithm to faster functional module
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Wed, 22 Oct 2003 03:24:40 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

I wonder what platform you are using. However, note that the original 
procedural code was hardly optimized. I deliberately did not change 
anything but just one look at it suggests the following obvious 
improvements:

procConcurrence = Compile[{{v1, _Real, 2}, {v2, _Real, 2}}
        , Module[{c = 0},
     Do[If[
     v1[[i]][[1]] == v2[[k]][[1]] ||
     v1[[i]][[1]] < v2[[k]][[1]] && v1[[i]][[2]] > v2[[k]][[1]] ||
     v1[[i]][[1]] > v2[[k]][[1]] && v1[[i]][[1]] < v2[[k]][[2]], c++],
     {i, Length[v1], 1, -1}, {k, Length[v2], 1, -1}]; c]];

Now, with exactly the same tests as before and using Peter Pein's 
faster functional Concurrence I get:

  In[3]:=
v1 = Table[{i - Random[], i + Random[]}, {i, 100}];
v2 = Table[{i - Random[], i + Random[]}, {i, 1000}];

In[5]:=
(Timing[#1[v1, v2]] & ) /@ {Concurrence, procConcurrence}

Out[5]=
{{2.16 Second,214},{0.95 Second,214}}

In[6]:=
(Timing[#1[v2, v1]] & ) /@ {Concurrence, procConcurrence}

Out[6]=
{{2. Second,214},{0.9 Second,214}}

As you can see, on my machine, the compiled procedural code is over 
twice as fast. I certainly am not arguing for any superiority of 
procedural programming, one of the main attractions of Mathematica is 
in fact that it is not procedural. But in numerical tasks it is pure 
myth that functional programming has any advantage.

Andrzej Kozlowski
Yokohama, Japan
http://www.mimuw.edu.pl/~akoz/
http://platon.c.u-tokyo.ac.jp/andrzej/



On Tuesday, October 21, 2003, at 03:07 PM, Tom Burton wrote:

> On 10/17/03 9:33 PM, in article bmqqcl$rjv$1 at smc.vnet.net, 
> "Prince-Wright,
> Robert G SEPCO" <robert.prince-wright at shell.com> wrote:
>
>> Can someone please help me recode this Module so it is less procedural
>> and hopefully runs a lot faster...
>
> I find that Peter Pein's solution is both faster than the Compiled 
> original
> but also easier to look at. I would only suggest the following 
> alternative
> when the number of sets becomes large:
>
> Concurrence6[v1_List, v2_List,nsim_] := Total[
>    Take[v1,nsim] /. {a_?AtomQ, b_} :>
>        Count[Take[v2,nsim] /. {c_?AtomQ, d_} -> (a <= c < b || c < a < 
> d),
>     True]
>   ]
>
> It is slightly slower, but creates much less temporary storage.
>
> Tom Burton
>
>
>


  • Prev by Date: Re: Re: Re: recode procedural algorithm to faster functional module
  • Next by Date: Re: What Happens to Garbage in Mathematica?
  • Previous by thread: Re: Re: Re: recode procedural algorithm to faster functional module
  • Next by thread: Re: recode procedural algorithm to faster functional module