MathGroup Archive 2008

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

Search the Archive

Re: Position of Last Element in List

  • To: mathgroup at smc.vnet.net
  • Subject: [mg94182] Re: Position of Last Element in List
  • From: Mark Westwood <markc.westwood at gmail.com>
  • Date: Sat, 6 Dec 2008 06:18:14 -0500 (EST)
  • References: <ghb02f$oho$1@smc.vnet.net>

Hi Raffy

I'm sure I've missed something about your example, but here goes:

-- running your example code gives a time of 18.7s on my machine, cut
to about 3.5s by Szabolcs code.

-- my naive implementation -- Timing[Position[Range[1000000], 900000]]
-- takes 0.25s to find all occurrences of the test value in the test
list, in this case 1

-- if I try a more demanding variation, such as Timing[Position
[RandomChoice[Range[1000000], 1000000], 900000]] -- my computer takes
about 0.35s, whether there are 0, 1 or 2 occurrences of 900000 in the
random list; note that in this variation the timing includes the
generation of the unsorted list

-- OK, so the list may not be flat, so create a test example outside
the timed section:

rlist = Partition[RandomChoice[Range[1000000], 1000000], 7];

and try

Timing[Position[rlist, 900000]]

which on my PC gives the output

{0.328, {{81644, 4}}}

Sure, that's not at the 1st level, but your Mathematica now has ~20s
to figure out what depth that's at and reject any which don't conform
to your selection criteria


Still puzzled, still think I'm missing the point

Regards

Mark Westwood

On 5 Dec, 10:33, Raffy <ra... at mac.com> wrote:
> I'm looking for the fastest way to find the last position of an
> element at level 1 in a list.
>
> The fastest way, which is still extremely bloated and extremely slow,
> appears to be:
>
> lastPos[v_List,p_:True,default_:$Failed]:=(
> $Temp=Position[Reverse[v],p,{1},1,Heads->False];
> If[Length[$Temp]>0,$Temp[[1,1]],default]
> );
>
> Any suggestions?
>
> Use the following code to compare against lastPos:
>
> v = Range[1000000];
> Do[lastPos[v,900000],{100}]//Timing



  • Prev by Date: Re: Mathematica 5,6,7 WeatherData, CityData
  • Next by Date: Re: A plot of Sign[x]
  • Previous by thread: Re: Position of Last Element in List
  • Next by thread: Re: Position of Last Element in List