Skip to main content

Another useful thing to do when testing such things is to determine whether packed arrays are unpacking. For all of your cases there is a lot of unpacking going on (I've only shown the first of such messages...)

In[1]:= On["Packing"]

In[2]:= test = RandomInteger[{-25, 25}, {10^6, 2}];

In[3]:= (res1 = Cases[test, {_, _?Positive}]); // AbsoluteTiming

During evaluation of In[3]:= Developer`FromPackedArray::punpackl1: Unpacking array with dimensions {1000000,2} to level 1. >>

Out[3]= {1.060814, Null}

In[4]:= (res2 = Select[test, #[[2]] > 0 &]); // AbsoluteTiming

During evaluation of In[4]:= Developer`FromPackedArray::punpackl1: Unpacking array with dimensions {1000000,2} to level 1. >>

Out[4]= {1.856424, Null}

In[5]:= (res3 = test[[Flatten@ Position[test[[All, 2]], _?Positive]]]); // AbsoluteTiming

During evaluation of In[5]:= Developer`FromPackedArray::unpack: Unpacking array in call to Position. >>

Out[5]= {4.742461, Null}

In[1]:= On["Packing"]

In[2]:= test = RandomInteger[{-25, 25}, {10^6, 2}];

In[3]:= (res1 = Cases[test, {_, _?Positive}]); // AbsoluteTiming

During evaluation of In[3]:= Developer`FromPackedArray::punpackl1: Unpacking array with dimensions {1000000,2} to level 1. >>

Out[3]= {1.060814, Null}

In[4]:= (res2 = Select[test, #[[2]] > 0 &]); // AbsoluteTiming

During evaluation of In[4]:= Developer`FromPackedArray::punpackl1: Unpacking array with dimensions {1000000,2} to level 1. >>

Out[4]= {1.856424, Null}

In[5]:= (res3 = 
    test[[Flatten@
       Position[test[[All, 2]], _?Positive]]]); // AbsoluteTiming

During evaluation of In[5]:= Developer`FromPackedArray::unpack: Unpacking array in call to Position. >>


Out[5]= {4.742461, Null}

Now Pick can be written so that it doesn't unpack...

In[6]:= (res4 = Pick[test, Sign[test[[All, 2]]], 1]); // AbsoluteTiming

Out[6]= {0.046801, Null}

In[8]:= Off["Packing"]

In[9]:= res1 == res2 == res3 == res4

Out[9]= True

In[6]:= (res4 = Pick[test, Sign[test[[All, 2]]], 1]); // AbsoluteTiming

Out[6]= {0.046801, Null}

In[8]:= Off["Packing"]

In[9]:= res1 == res2 == res3 == res4

Out[9]= True

Another useful thing to do when testing such things is to determine whether packed arrays are unpacking. For all of your cases there is a lot of unpacking going on (I've only shown the first of such messages...)

In[1]:= On["Packing"]

In[2]:= test = RandomInteger[{-25, 25}, {10^6, 2}];

In[3]:= (res1 = Cases[test, {_, _?Positive}]); // AbsoluteTiming

During evaluation of In[3]:= Developer`FromPackedArray::punpackl1: Unpacking array with dimensions {1000000,2} to level 1. >>

Out[3]= {1.060814, Null}

In[4]:= (res2 = Select[test, #[[2]] > 0 &]); // AbsoluteTiming

During evaluation of In[4]:= Developer`FromPackedArray::punpackl1: Unpacking array with dimensions {1000000,2} to level 1. >>

Out[4]= {1.856424, Null}

In[5]:= (res3 = test[[Flatten@ Position[test[[All, 2]], _?Positive]]]); // AbsoluteTiming

During evaluation of In[5]:= Developer`FromPackedArray::unpack: Unpacking array in call to Position. >>

Out[5]= {4.742461, Null}

Now Pick can be written so that it doesn't unpack...

In[6]:= (res4 = Pick[test, Sign[test[[All, 2]]], 1]); // AbsoluteTiming

Out[6]= {0.046801, Null}

In[8]:= Off["Packing"]

In[9]:= res1 == res2 == res3 == res4

Out[9]= True

Another useful thing to do when testing such things is to determine whether packed arrays are unpacking. For all of your cases there is a lot of unpacking going on (I've only shown the first of such messages...)

In[1]:= On["Packing"]

In[2]:= test = RandomInteger[{-25, 25}, {10^6, 2}];

In[3]:= (res1 = Cases[test, {_, _?Positive}]); // AbsoluteTiming

During evaluation of In[3]:= Developer`FromPackedArray::punpackl1: Unpacking array with dimensions {1000000,2} to level 1. >>

Out[3]= {1.060814, Null}

In[4]:= (res2 = Select[test, #[[2]] > 0 &]); // AbsoluteTiming

During evaluation of In[4]:= Developer`FromPackedArray::punpackl1: Unpacking array with dimensions {1000000,2} to level 1. >>

Out[4]= {1.856424, Null}

In[5]:= (res3 = 
    test[[Flatten@
       Position[test[[All, 2]], _?Positive]]]); // AbsoluteTiming

During evaluation of In[5]:= Developer`FromPackedArray::unpack: Unpacking array in call to Position. >>


Out[5]= {4.742461, Null}

Now Pick can be written so that it doesn't unpack...

In[6]:= (res4 = Pick[test, Sign[test[[All, 2]]], 1]); // AbsoluteTiming

Out[6]= {0.046801, Null}

In[8]:= Off["Packing"]

In[9]:= res1 == res2 == res3 == res4

Out[9]= True
Source Link
Andy Ross
  • 19.4k
  • 2
  • 61
  • 93

Another useful thing to do when testing such things is to determine whether packed arrays are unpacking. For all of your cases there is a lot of unpacking going on (I've only shown the first of such messages...)

In[1]:= On["Packing"]

In[2]:= test = RandomInteger[{-25, 25}, {10^6, 2}];

In[3]:= (res1 = Cases[test, {_, _?Positive}]); // AbsoluteTiming

During evaluation of In[3]:= Developer`FromPackedArray::punpackl1: Unpacking array with dimensions {1000000,2} to level 1. >>

Out[3]= {1.060814, Null}

In[4]:= (res2 = Select[test, #[[2]] > 0 &]); // AbsoluteTiming

During evaluation of In[4]:= Developer`FromPackedArray::punpackl1: Unpacking array with dimensions {1000000,2} to level 1. >>

Out[4]= {1.856424, Null}

In[5]:= (res3 = test[[Flatten@ Position[test[[All, 2]], _?Positive]]]); // AbsoluteTiming

During evaluation of In[5]:= Developer`FromPackedArray::unpack: Unpacking array in call to Position. >>

Out[5]= {4.742461, Null}

Now Pick can be written so that it doesn't unpack...

In[6]:= (res4 = Pick[test, Sign[test[[All, 2]]], 1]); // AbsoluteTiming

Out[6]= {0.046801, Null}

In[8]:= Off["Packing"]

In[9]:= res1 == res2 == res3 == res4

Out[9]= True