MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Selecting Rows Where All Columns Satisfy a Condition-Plot of Code speeds

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82788] Re: [mg82589] Re: Selecting Rows Where All Columns Satisfy a Condition-Plot of Code speeds
  • From: Syd Geraghty <sydgeraghty at mac.com>
  • Date: Wed, 31 Oct 2007 06:07:33 -0500 (EST)
  • References: <ffppu6$l6g$1@smc.vnet.net> <200710260908.FAA21913@smc.vnet.net>

Hi all,

I have been struck by how may different code suggestions can be 
generated by a seemingly simple request for help.

Below is a ListPlot of the speed of 10 inputs in reply to the post 
[mg82589] Re : Selecting Rows Where All Columns Satisfy a Condition.

As you can see by running this code there is a significant difference 
and the most arcane code won.

Below is a quick and specific attempt to get a first cut benchmark to 
assess code variations. I would be very interested if anyone has 
generated
a more sophisticated benchmark method that would take a variable 
number of code snippets and the initialisation code required and 
generate
a nice benchmark result. It seems to me that such a tool could be 
very useful to all of us who have an ongoing interest in learning how 
to write the
most elegant and efficient Mathematica code to pursue our interests.

I have used Tooltip to identify the options but maybe labels would be 
nicer.

I would appreciate a response or any suggestions to speed up the 
process of generating the plots.

(Thanks to everyone who generated the code suggestions).


(* Tooltip Test of speed for the different codes to solve the specific \
integer exclusion question*)

r := RandomInteger[{-10, -1}]; SeedRandom[1]; X =
  Table[{r, r, r, r}, {100000}];

ListPlot[a = Sort[{
     Tooltip[First[Select[X, FreeQ[#, -9] &]; // Timing],
      "Select[X,FreeQ[#,-9]&]"],
     Tooltip[First[Select[X, FreeQ[#, -9, {1}] &]; // Timing],
      "Select[X,FreeQ[#,-9,{1}]&]"],
     Tooltip[First[Select[X, VectorQ[#, # != -9 &] &]; // Timing],
      "Select[X,VectorQ[#,#=E2=89=A0-9&]&]"],
     Tooltip[First[Select[X, FreeQ[#, -9 | -9.] &]; // Timing],
      "FreeQ[#,-9|-9.]&]"],
     Tooltip[First[Select[X, ! MemberQ[#, -9. | -9] &]; // Timing],
      "Select[X,!MemberQ[#,-9.|-9]&]"],
     Tooltip[First[Select[X, And @@ Thread[# != -9] &]; // Timing],
      "Select[X,And@@Thread[#=E2=89=A0-9]&]"],
     Tooltip[First[Select[X, And @@ (# != -9 & /@ #) &]; // Timing],
      "Select[X,And@@(#=E2=89=A0-9&/@#)&]"],
     Tooltip[First[X /. {___, -9, ___} -> Sequence[]; // Timing],
      "X/.{___,-9,___}=E2=86=92Sequence[]"],
     Tooltip[First[DeleteCases[X, {___, -9, ___}]; // Timing],
      "DeleteCases[X,{___,-9,___}]"],
     Tooltip[
      First[
       X[[SparseArray[Times @@ (Transpose@X + 9)] /.
            SparseArray[_, _, _, p_] :> Flatten@p[[2, 2]]]]; // Timing],
      "X[[SparseArray[Times@@(Transpose@X+9)]/.SparseArray[_,_,_,p_]
=C6=92Flatten@p[\
[2,2]]]]"]
     }], Filling -> Axis, ImageSize -> {450, 450}, PlotMarkers -> 
Automatic,
  PlotLabel -> "Fastest Code\n\n" a[[1]][[2]]]


I have also generated a normalised version of the plot (normalised 
with respect to the fastest code) but so far have not been able
to do that and preserve the Tooltip information in the plot. The 
problem I have here is not yet finding a way of executing the 
normalising
division without first stripping the Tooltip data from the list 
elements.

Running the code code below shows the issues more clearly than my 
brief description of the problem.

normspeed 
  Sort[{Tooltip[First[Select[X, FreeQ[#, -9] &]; // Timing],
      "Select[X,FreeQ[#,-9]&]"],
     Tooltip[First[Select[X, FreeQ[#, -9, {1}] &]; // Timing],
      "Select[X,FreeQ[#,-9,{1}]&]"],
     Tooltip[First[Select[X, VectorQ[#, # != -9 &] &]; // Timing],
      "Select[X,VectorQ[#,#=E2=89=A0-9&]&]"],
     Tooltip[First[Select[X, FreeQ[#, -9 | -9.] &]; // Timing],
      "FreeQ[#,-9|-9.]&]"],
     Tooltip[First[Select[X, ! MemberQ[#, -9. | -9] &]; // Timing],
      "Select[X,!MemberQ[#,-9.|-9]&]"],
     Tooltip[First[Select[X, And @@ Thread[# != -9] &]; // Timing],
      "Select[X,And@@Thread[#=E2=89=A0-9]&]"],
     Tooltip[First[Select[X, And @@ (# != -9 & /@ #) &]; // Timing],
      "Select[X,And@@(#=E2=89=A0-9&/@#)&]"],
     Tooltip[First[X /. {___, -9, ___} -> Sequence[]; // Timing],
      "X/.{___,-9,___}=E2=86=92Sequence[]"],
     Tooltip[First[DeleteCases[X, {___, -9, ___}]; // Timing],
      "DeleteCases[X,{___,-9,___}]"],
     Tooltip[
      First[
       X[[SparseArray[Times @@ (Transpose@X + 9)] /.
           SparseArray[_, _, _, p_] :> Flatten@p[[2, 2]]]]; // Timing],
      "X[[SparseArray[Times@@(Transpose@X+9)]/.SparseArray[_,_,_,p_]
=C6=92Flatten@p[\
[2,2]]]]"]
     }] // InputForm

normspeed[[1]]
normspeed[[1]][[1]]
normspeed[[1]]/normspeed[[1]][[1]]

I hope someone can produce an elegant normalised plot.


Cheers ... Syd

Syd Geraghty B.Sc., M.Sc.
sydgeraghty at mac.com
San Jose, CA

My System

Mathematic 6.0.1 for Mac OS X x86 (32 - bit) (June 19, 2007)  License =

L2983-5890
MacOS X V 10.4 .10
MacBook Pro 2.33 Ghz Intel Core 2 Duo  2GB RAM

On Oct 26, 2007, at 2:08 AM, Norbert Marxer wrote:

> On 25 Okt., 12:08, Gregory Lypny <gregory.ly... at videotron.ca> wrote:
>> Hello everyone
>>
>> I've got an Nx4 numeric array called X.  I'd like to pull out all
>> rows where -9 does not appear in any of the four columns.  I know I
>> can this:
>>
>> Select[X, #[[1]] != -9 && #[[2]] != -9 && #[[3]] != -9 && =
#[[4]] !=
>> -9 &]
>>
>> But is there a more elegant way that applies the not-equal-to
>> condition to each column without having to repeat it?
>>
>> Regards,
>>
>>         Gregory
>
> Hello
>
> You could use:
>
>     Select[X, And @@ (#1 != -9 & ) /@ #1 & ]
>
> The following is a short test of the above code and shows that you get
> the same result:
>
> r := RandomInteger[{-10, -1}]; SeedRandom[1]; X =  Table[{r, r, r, =
r},
> {10}];
> Select[X, #1[[1]] != -9 && #1[[2]] != -9 && #1[[3]] != -9 && =
#1[[4]] !
> = -9 & ] ==  Select[X, And @@ (#1 != -9 & ) /@ #1 & ]
>
> Best Regards
> Norbert Marxer
>
>
>



  • Prev by Date: Re: wrong answer or no answer?
  • Next by Date: Re: Setting Negatives to Zero
  • Previous by thread: Re: Selecting Rows Where All Columns Satisfy a Condition
  • Next by thread: Re: Selecting Rows Where All Columns Satisfy a Condition