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


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

Re: Replace, ReplaceAll and If time performace comparition

Started byChristoph Lhotka <christoph.lhotka@fundp.ac.be>
First post2012-06-27 08:06 +0000
Last post2012-06-30 09:16 +0000
Articles 5 — 2 participants

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

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Replace, ReplaceAll and If time performace comparition Christoph Lhotka <christoph.lhotka@fundp.ac.be> - 2012-06-27 08:06 +0000
    Re: Replace, ReplaceAll and If time performace comparition awnl <awnl@gmx-topmail.de> - 2012-06-28 08:04 +0000
    Re: Replace, ReplaceAll and If time performace comparition awnl <awnl@gmx-topmail.de> - 2012-06-28 08:00 +0000
    Re: Replace, ReplaceAll and If time performace comparition Christoph Lhotka <christoph.lhotka@fundp.ac.be> - 2012-06-29 08:48 +0000
      Re: Replace, ReplaceAll and If time performace comparition awnl <awnl@gmx-topmail.de> - 2012-06-30 09:16 +0000

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

FromChristoph Lhotka <christoph.lhotka@fundp.ac.be>
Date2012-06-27 08:06 +0000
SubjectRe: Replace, ReplaceAll and If time performace comparition
Message-ID<jseetb$i34$1@smc.vnet.net>
Hello,

  I played around to see if it is possible to top the performance goal 
of Clip:

WARNING: SET THE LENGTH OF THE LIST randomList TO A SMALLER VALUE,
IF YOU ARE NOT SURE ABOUT HOW MUCH MEMORY YOU CAN ADDRESS AND
WANT TO AVOID CRASHING YOUR PC.

(on my computer it was 10*^9 for function cf2)


cf0 = Compile[{{list, _Integer, 1}},
    Module[{clist = list, cur},
     Do[
      clist[[i]] = If[(cur = list[[i]]) > 0, cur, 0],
      {i, 1, Length[list]}];
     clist
     ],
    CompilationTarget -> "C"
    ];

cf1 = Compile[{{list, _Integer, 1}},
    Module[{clist = list, cur},
     Do[
      clist[[i]] = If[(cur = list[[i]]) > 0, cur, 0],
      {i, 1, Length[list]}];
     clist
     ],
    CompilationTarget -> "C",
    RuntimeOptions -> "Quality"
    ];

cf2 = Compile[{{list, _Integer, 1}},
    Module[{clist = list, cur},
     Do[
      clist[[i]] = If[(cur = list[[i]]) > 0, cur, 0],
      {i, 1, Length[list]}];
     clist
     ],
    CompilationTarget -> "C",
    RuntimeOptions -> "Speed"
    ];

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

a = cf2[randomList]; // AbsoluteTiming

{0.756555, Null}

b = Clip[randomList, {0, Infinity}, {0, 1}]; // AbsoluteTiming

{0.713735, Null}

c = cf1[randomList]; // AbsoluteTiming

{0.501313, Null}

d = cf0[randomList]; // AbsoluteTiming

{0.491033, Null}

a == b == c == d

True

As you can see Clip can still be topped by a factor 1.45 by using 
procedural and compiled code
(for me the most interesting enhancements in Mathematica 8).

What I found surprising is that cf2 ("Speed") is slower than cf1 
("Quality") and cf0. I also
did not increase the performance when parallelizing (use 
Parallelization->True). By the way,
the ranking between cf1, cf0 is just an incidence . They are of the same 
performance (on my PC).

Can anyone top the result?

Best,

   Christoph


On 06/25/2012 10:01 AM, Andrzej Kozlowski wrote:
> 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
>
>

[toc] | [next] | [standalone]


#10666

Fromawnl <awnl@gmx-topmail.de>
Date2012-06-28 08:04 +0000
Message-ID<jsh35g$sff$1@smc.vnet.net>
In reply to#10660
Hi,

> Can anyone top the result?

contradicting to my announcement, I tried anyway and found this to be 
considerably faster on my machine:

cf3 = Compile[{{list, _Integer, 1}},
  Map[If[# > 0, #, 0] &, list],
  CompilationTarget -> "C"
]

hth,

albert

[toc] | [prev] | [next] | [standalone]


#10674

Fromawnl <awnl@gmx-topmail.de>
Date2012-06-28 08:00 +0000
Message-ID<jsh2uq$sah$1@smc.vnet.net>
In reply to#10660
Hi,

> As you can see Clip can still be topped by a factor 1.45 by using
> procedural and compiled code
> (for me the most interesting enhancements in Mathematica 8).

that seems to be somewhat dependent on your setup (hardware, OS, 
compiler ...). For me the compiled versions are slightly slower than 
Clip (with MS compiler on Windows 7 64bit). As the code is so simple I 
wouldn't expect that a compiler would have much chance to do good or bad 
optimizing it, but still, probably your compiler is doing a better job 
than mine :-).

> What I found surprising is that cf2 ("Speed") is slower than cf1
> ("Quality") and cf0. I also
> did not increase the performance when parallelizing (use
> Parallelization->True).

that is strange indeed, I see the same thing. I have seen large 
differences in other cases, but probably that's because most of those 
options are irrelevant for the code that's run (no danger of 
underflow/overflow etc.). For the parallelization: I think this will 
only have an effect if you make your compiled function Listable and the 
function "threads over list arguments". Rewriting your function that way 
(avoiding the Do-loop) turns out to be a performance disaster, though :-)

> By the way,
> the ranking between cf1, cf0 is just an incidence . They are of the same
> performance (on my PC).
>
> Can anyone top the result?

not me, I won't even try. And I won't buy a better compiler to proof a 
point :-)

albert

[toc] | [prev] | [next] | [standalone]


#10678

FromChristoph Lhotka <christoph.lhotka@fundp.ac.be>
Date2012-06-29 08:48 +0000
Message-ID<jsjq44$ct6$1@smc.vnet.net>
In reply to#10660
Hi,

  congratulations!

  It strikes the performance of Clip on my machine by a factor of 2 
(instead of
a factor of 1.45 as published in my previous post).

Does it mean, that Compile + Map + raw functions is a good combination 
to speed up codes?


Question: Can anyone explain, why RuntimeOptions->"Speed" does not 
improve the performance?


Thanks for your code,

   Christoph



On 06/28/2012 10:02 AM, awnl wrote:
> Hi,
>
>> Can anyone top the result?
> contradicting to my announcement, I tried anyway and found this to be
> considerably faster on my machine:
>
> cf3 = Compile[{{list, _Integer, 1}},
>    Map[If[# > 0, #, 0] &, list],
>    CompilationTarget -> "C"
> ]
>
> hth,
>
> albert
>


[toc] | [prev] | [next] | [standalone]


#10706

Fromawnl <awnl@gmx-topmail.de>
Date2012-06-30 09:16 +0000
Message-ID<jsmg60$1ij$1@smc.vnet.net>
In reply to#10678
Hi,


> Does it mean, that Compile + Map + raw functions is a good combination
> to speed up codes?

It looks like yes. What you should have in mind is that Compile does 
transform Mathematica code to something else (with CompilationTarget -> 
"C" to C). The speedup depends a lot on how well optimized the generated 
code is. I think the general rule is that constructs which are less 
general and easier to analyze will be easier to optimize and thus have a 
greater chance to be translated to something efficient.

As we don't know which transformation rules the code generator uses, it 
will always need some experimenting, though.

hth,

albert

[toc] | [prev] | [standalone]


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


csiph-web