Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #10626 > unrolled thread

Re: Replace, ReplaceAll and If time performace comparition

Started byBill Rowe <readnews@sbcglobal.net>
First post2012-06-25 08:02 +0000
Last post2012-06-25 08:02 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Re: Replace, ReplaceAll and If time performace comparition Bill Rowe <readnews@sbcglobal.net> - 2012-06-25 08:02 +0000

#10626 — Re: Replace, ReplaceAll and If time performace comparition

FromBill Rowe <readnews@sbcglobal.net>
Date2012-06-25 08:02 +0000
SubjectRe: Replace, ReplaceAll and If time performace comparition
Message-ID<js95us$f3j$1@smc.vnet.net>
On 6/24/12 at 4:26 AM, rodrigomurtax@gmail.com (Murta) wrote:

>HI all I was working in some simulations with random numbers and get
>this example of performance comparition.

>randomList = RandomInteger[{-100, 100}, 10 10^6];

>(randomList /. (x_ /; x < 0 -> 0)); // AbsoluteTiming {5.747133,
>Null}

>Replace[randomList, (x_ /; x < 0 -> 0), 1]; // AbsoluteTiming
>{4.758984, Null}

>(If[# < 0, 0, #] & /@ randomList); // AbsoluteTiming {0.572200,
>Null}

>I personally prefer work with patterns because they are more compact
>and functional. Someone knows why patter is one magnitude order
>slow?? There is some trick to make it faster?

I don't know how to make the pattern matching faster. But I do
know how to create the list faster. That is:

In[1]:= randomList = RandomInteger[{-100, 100}, 10 10^6];

In[2]:= (a = If[# < 0, 0, #] & /@ randomList); // AbsoluteTiming

Out[2]= {0.597679,Null}

In[3]:= (b = Clip[randomList, {0, 100}]); // AbsoluteTiming

Out[3]= {0.086329,Null}

In[4]:= a == b

Out[4]= True

And it seems to me the syntax of Clip makes it very apparent
what the code does.

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web