I am fairly new to mathematica and working my way thru Paul Wellins book on Mathematica programming so experimenting with various language constructs. I was experimenting with Select
and I am curious to understand why the first example is much faster than the second. My background is programming in C/C++/Java so I am wondering why the performance is quite different. In the second example I am appending to a list r so I can see there is possibly some memory allocation etc. going on behind the scenes but I would assume the same is true for the Select
example. I am curious to understand why the performance characteristics of these two examples is so different.
Example 1 -
t = Table[RandomInteger[100],{x,100000}];
Timing[Select[t, #>50 &]]
Example 2 -
t = Table[RandomInteger[100],{x,100000}];
r = {};
Timing[For[i=0,i<Length @ t,i++,If[t[[i]]>50,r=Append[r,t[[i]]]]]]
Example 3 -
Timing @ Reap[For[i=0,i< Length @ t, i++,If[t[[i]]>0,Sow[t[[i]]]]]]
Select
in this answer. $\endgroup$