Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
1995
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*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 christensen.cybernetics.net
  • Subject: [mg1044] Re: Challenge! ....RESULTS...
  • From: roth at sunny.mpimf-heidelberg.mpg.de (Arnd Roth)
  • Date: Wed, 10 May 1995 08:05:35 -0400
  • Organization: Max-Planck-Institut fuer Medizinische Forschung

In article <3nv8if$91e at news0.cybernetics.net>
pehowland at taz.dra.hmg.gb (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 dra.hmg.gb
In[3]:=
swap[list1_List,list2_List] := Module[{newlist={},M,i},
M=Length[list1];
       
Do[AppendTo[newlist,If[Random[]>0.5,list1[[i]],list2[[i]]]],{i,M}];
        newlist]
In[4]:= Timing[Do[swap[l1,l2],{1000}]]
Out[4]= {6.45 Second, Null}

Buchanan's function    buchanan at niehs.nih.gov
In[5]:=
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 wri.com
In[7]:=
swap[list1_List,list2_List] := 
        Map[#[[Random[Integer]+1]]&,Transpose[{list1,list2}]]
In[8]:= Timing[Do[swap[l1,l2],{1000}]]
Out[8]= {2.17 Second, Null}

Villegas' function 1    villegas at wri.com
In[9]:=
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 wri.com
In[11]:=
swap[list1_List,list2_List] := Module[{i,len}, len=Length[list1];
        Table[Part[{list1,list2},Random[Integer,{1,2}],i],{i,len}]]
In[12]:= Timing[Do[swap[l1,l2],{1000}]]
Out[12]= {3.98 Second, Null}

Wagner's function    wagner at bullwinkle.cs.colorado.edu
In[13]:=
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 sunny.mpimf-heidelberg.mpg.de
In[15]:=
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 wri.com
In[17]:=
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
speedup.

Arnd Roth
Abteilung Zellphysiologie
Max-Planck-Institut fuer Medizinische Forschung
Postfach 10 38 20, D-69028 Heidelberg, Germany
http://sunny.mpimf-heidelberg.mpg.de/people/roth/ArndRoth.html


  • 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...