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


Groups > comp.soft-sys.math.mathematica > #10624

Re: Replace, ReplaceAll and If time performace comparition

From Andrzej Kozlowski <akozlowski@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Replace, ReplaceAll and If time performace comparition
Date 2012-06-25 08:01 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <js95sv$f2s$1@smc.vnet.net> (permalink)
References <201206240826.EAA08423@smc.vnet.net>

Show all headers | View raw


On 24 Jun 2012, at 10:26, 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?
>
> tks
> Murta
>

Purpose built-in functions will always be much faster than general methods (such as pattern matching). However, in this particular your fastest method is actually far from optional:

In[1]:= randomList=RandomInteger[{-100,100},10 10^6];
In[2]:= (a=If[#<0,0,#]&/@randomList);//AbsoluteTiming
Out[2]= {0.874206,Null}
In[3]:= (b=Clip[randomList,{0,Infinity},{0,1}]);//AbsoluteTiming
Out[3]= {0.064867,Null}
In[4]:= a==b
Out[4]= True

There is no way to speed up pattern matching to give comparable performance.

Andrzej Kozlowski

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Replace, ReplaceAll and If time performace comparition Andrzej Kozlowski <akozlowski@gmail.com> - 2012-06-25 08:01 +0000

csiph-web