Re: Re: Need a nice way to do this
- To: mathgroup at smc.vnet.net
- Subject: [mg40526] Re: Re: Need a nice way to do this
- From: Dr Bob <majort at cox-internet.com>
- Date: Wed, 9 Apr 2003 01:33:02 -0400 (EDT)
- References: <00cc01c2fde5$0e64cb60$0d1f0944@Main>
- Reply-to: majort at cox-internet.com
- Sender: owner-wri-mathgroup at wolfram.com
Yet another winner! Carl's solution (or Hartmut's) seems identical to mine in principal, yet it's more than twice as fast. It requires table-lookup of f[i] at each element of the list, like mine, but it increments a variable (stored in the definition of f[i]) rather than incrementing f[i] itself. I wonder if ++f[i] uses TWO table look-ups? n = 100000; test = Array[Random[Integer] &, n]; Timing[drbob[test];] Timing[drbob2[test];] Timing[drbob3[test];] Timing[drbob4[test];] Timing[andrzej2[test];] Timing[kuska2[test];] Timing[deLouis2[test];] Timing[carl[test];] {0.797 Second,Null} {1.344 Second,Null} {1.328 Second,Null} {0.719 Second,Null} {0.5 Second,Null} {1.344 Second,Null} {0.36 Second,Null} {0.312 Second,Null} n = 100000; test = Array[Round[20Random[]] &, n]; Timing[drbob[test];] Timing[drbob4[test];] Timing[kuska2[test];] Timing[deLouis2[test];] Timing[carl[test];] {0.844 Second,Null} {0.75 Second,Null} {12.234 Second,Null} {0.563 Second,Null} {0.281 Second,Null} n = 100000; test = Array[Round[99Random[]] &, n]; Timing[drbob4[test];] Timing[deLouis2[test];] Timing[carl[test];] {1.047 Second,Null} {0.625 Second,Null} {0.375 Second,Null} Bobby On Tue, 8 Apr 2003 11:39:24 -0400, Carl K. Woll <carlw at u.washington.edu> wrote: > Hi Bobby, > > You didn't mention Hartmut Wolf's solution, but then his function > wouldn't > work on your test data. However, his basic idea demonstrates a useful > technique, since he increments a symbol, and not a function of a symbol > as > most of the other solutions do. We can incorporate this concept and > arrive > at the following function: > > carl[s_] := Block[{f}, > f[i_] := With[{j = Unique[]}, f[i] := ++j; j = 1]; > Range[Length[s]] - f /@ s] > > In my timing tests, this function is faster than all of the others that > have > been posted. > > Carl Woll > Physics Dept > U of Washington > > "Dr Bob" <majort at cox-internet.com> wrote in message > news:b6tsjh$n0r$1 at smc.vnet.net... >> We have a new winner! (For two-value lists.) >> >> n = 100000; >> test = Array[Random[Integer] &, n]; >> Timing[drbob[test];] >> Timing[drbob2[test];] >> Timing[drbob3[test];] >> Timing[drbob4[test];] >> Timing[andrzej2[test];] >> Timing[kuska2[test];] >> Timing[deLouis2[test];] >> >> {0.781 Second,Null} >> {1.328 Second,Null} >> {1.328 Second,Null} >> {0.735 Second,Null} >> {0.5 Second,Null} >> {1.344 Second,Null} >> {0.328 Second,Null} >> >> Bobby >> >> On Mon, 7 Apr 2003 04:53:53 -0400 (EDT), Dana DeLouis >> <delouis at bellsouth.net> wrote: >> >> > Hello. I'm too new at this to contribute much. >> > I modified Andrzej's second code slightly for two variables. I seem >> to >> > get >> > a 30% speed increase with the following idea. >> > f3[s_List] := Module[ >> > {v, t}, v = Length /@ Split[s]; t = Transpose[Partition[v, 2, 2, {1, >> 1}, >> > 0]]; t = (FoldList[Plus, 0, #1] & ) /@ t; t = >> > Take[Rest[Flatten[Transpose[t]]], Length[v]]; > Flatten[MapThread[Table[#1, >> > {#2}] & , {t, v}]] >> > ] >> > >> >> >> >> -- >> majort at cox-internet.com >> Bobby R. Treat >> >> > > > > > -- majort at cox-internet.com Bobby R. Treat