There are also faster ways to do this using Pick
or by compiling Select
. Timing comparison done on a Macbook Air OS X 10.8.3 w/ 1.7 GHz Intel Core i5 with Mathematica 9.0.0.0.
t = RandomInteger[100, 10^7];
Timing[Select[t, # > 50 &];]
(*7.87 sec*)
t~Extract~SparseArray[Clip[t, {51, \[Infinity]}, {0, 0}]]["NonzeroPositions"]; // Timing
(*0.402 sec*)
Timing[Pick[t, UnitStep[t - 51], 1];]
(*0.375 sec*)
greaterthan50 = Compile[{{t, _Integer, 1}}, Select[t, # > 50 &], CompilationTarget ->"C", RuntimeOptions -> "Speed"]
greaterthan50[t]; // Timing
(*0.126 sec*)
compiledbagselect =
Compile[{{t, _Integer, 1}},
Module[{output = Internal`Bag[Most[{0}]], i},
Do[If[i > 50, Internal`StuffBag[output, i]], {i, t}];
Internal`BagPart[output, All]], RuntimeOptions -> {"Speed"},
CompilationTarget -> "C"];
compiledbagselect[t]; // Timing
(*0.175 sec*)
Here are some JIT compiled options:
selectJIT[pred_, listType_] :=
selectJIT[pred, Verbatim[listType]] =
Block[{lst},
With[{decl = {Prepend[listType, lst]}},
Compile @@
Hold[decl, Select[lst, pred], CompilationTarget -> "C",
RuntimeOptions -> "Speed"]]];
selectJIT[# > 50 &, {_Integer, 1}][t]; // Timing
(*I'm not on my laptop so I can't get a comparable timing but this is fast*)
Experimental`CompileEvaluate[Select[t, # > 50 &]]
(*this is faster than uncompiled Select but still slower than the other options*)
Compiling Select
would appear to be the fastest here.