Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1995

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

Search the Archive

Re: Challenge! ....RESULTS...

  • To: mathgroup at
  • Subject: [mg1044] Re: Challenge! ....RESULTS...
  • From: roth at (Arnd Roth)
  • Date: Wed, 10 May 1995 08:05:35 -0400
  • Organization: Max-Planck-Institut fuer Medizinische Forschung

In article <3nv8if$91e at>
pehowland at (Paul E. Howland) writes:

> T H E  R E S U L T S
> ++++++++++++++++++++
> Tested by using list1 = {a,b,c,d,e,f,g,h,i,j};
>                 list2 = {A,B,C,D,E,F,G,H,I,J}; 
> With
>         Timing[Do[swap[list1,list2],{1000}]]
> on a DEC 3000/400 running Mathematica V2.2.4 for OSF/1.
> My function            9.93333 seconds
> Buchanan's function    9.56667 seconds
> Lichtblau's function    4.06667 seconds
> Villegas' function 1    3.9     seconds
> Villegas' function 2    6.05    seconds
> Wagner's function       3.66667 seconds
> So David Wagner is the winner!
> It's interesting that Wagner and Lichtblau have almost identical solutions, the
> fundamental difference being that Wagner uses Random[Integer, {1,2}] whereas
> Lichtblau uses Random[Integer]+1.  The latter is obviously slower to compute.

There are solutions that are faster than David Wagner's, but they
may not have reached you when you made this timing comparison.

So here is another set of timings (done on a Sparc 10/51).

In[1]:= l1 = {a,b,c,d,e,f,g,h,i,j};
In[2]:= l2 = {A,B,C,D,E,F,G,H,I,J};

Howland's function    pehowland at
swap[list1_List,list2_List] := Module[{newlist={},M,i},
In[4]:= Timing[Do[swap[l1,l2],{1000}]]
Out[4]= {6.45 Second, Null}

Buchanan's function    buchanan at
swap[list1_List,list2_List] := Module[{len,sel},len=Length[list1];
        sel = Table[{Random[Integer, {1,2}], i}, {i, len}];
        Map[{list1,list2}[[#[[1]],#[[2]]]]&, sel]]
In[6]:= Timing[Do[swap[l1,l2],{1000}]]
Out[6]= {6.09 Second, Null}

Lichtblau's function   danl at
swap[list1_List,list2_List] := 
In[8]:= Timing[Do[swap[l1,l2],{1000}]]
Out[8]= {2.17 Second, Null}

Villegas' function 1    villegas at
swap[list1_List,list2_List] :=
        MapThread[Part[{#1,#2},1+Random[Integer]]&, {list1,list2}]
In[10]:= Timing[Do[swap[l1,l2],{1000}]]
Out[10]= {2.08 Second, Null}

Villegas' function 2    villegas at
swap[list1_List,list2_List] := Module[{i,len}, len=Length[list1];
In[12]:= Timing[Do[swap[l1,l2],{1000}]]
Out[12]= {3.98 Second, Null}

Wagner's function    wagner at
swap[list1_List,list2_List] := 
        #[[Random[Integer,{1,2}]]]& /@ Transpose[{list1,list2}]
In[14]:= Timing[Do[swap[l1,l2],{1000}]]
Out[14]= {1.98 Second, Null}

My function    roth at
swap[list1_List, list2_List] := ReleaseHold[Thread[
Hold[If[Random[Integer] == 0, #1, #2] &][list1, list2]]]
In[16]:= Timing[Do[swap[l1,l2],{1000}]]
Out[16]= {1.79 Second, Null}

Villegas' function 3    villegas at
swap[list1_List, list2_List] := Thread @
Unevaluated[If[Random[Integer] == 0, #1, #2]& [list1, list2]]
In[18]:= Timing[Do[swap[l1,l2],{1000}]]
Out[18]= {1.44 Second, Null}

So Robby Villegas' function 3 is fastest among the solutions I
tested. But of course there may still be room for further

Arnd Roth
Abteilung Zellphysiologie
Max-Planck-Institut fuer Medizinische Forschung
Postfach 10 38 20, D-69028 Heidelberg, Germany

  • Prev by Date: How to solve system of iinequalities?
  • Next by Date: Re: DAE Solver in Mathematica?
  • Previous by thread: Challenge! ....RESULTS...
  • Next by thread: Re: Challenge! ....RESULTS...