Skip to main content
added 151 characters in body
Source Link
s0rce
  • 9.7k
  • 4
  • 47
  • 79

edit: this doesn't really answer the question but merely provides some other alternatives, you should probably up-vote other more useful answers.

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.

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.

edit: this doesn't really answer the question but merely provides some other alternatives, you should probably up-vote other more useful answers.

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.

added 626 characters in body
Source Link
s0rce
  • 9.7k
  • 4
  • 47
  • 79

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.

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*)

Compiling Select would appear to be the fastest here.

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.

added 355 characters in body
Source Link
s0rce
  • 9.7k
  • 4
  • 47
  • 79

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*)

Compiling Select would appear to be the fastest here.

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*)

Compiling Select would appear to be the fastest here.

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*)

Compiling Select would appear to be the fastest here.

added 415 characters in body
Source Link
s0rce
  • 9.7k
  • 4
  • 47
  • 79
Loading
Source Link
s0rce
  • 9.7k
  • 4
  • 47
  • 79
Loading