2 correct code to use Module; add nf to localization
source | link

You can speed it up by only invoking the NearestFunction once:

KPosition3[x_, y_] := 
  Block[Module[{step0, step1, nf}, 
   step0 = Thread[x -> Range[1, Length@x, 1]];
   nf = Nearest[step0];
   step1 = nf[#, y] & /@ x];

Running your three timing tests gives:

{0.003057, 0.004344, 0.051009}

You can speed it up by only invoking the NearestFunction once:

KPosition3[x_, y_] := 
  Block[{step0, step1}, 
   step0 = Thread[x -> Range[1, Length@x, 1]];
   nf = Nearest[step0];
   step1 = nf[#, y] & /@ x];

Running your three timing tests gives:

{0.003057, 0.004344, 0.051009}

You can speed it up by only invoking the NearestFunction once:

KPosition3[x_, y_] := 
  Module[{step0, step1, nf}, 
   step0 = Thread[x -> Range[1, Length@x, 1]];
   nf = Nearest[step0];
   step1 = nf[#, y] & /@ x];

Running your three timing tests gives:

{0.003057, 0.004344, 0.051009}
1
source | link

You can speed it up by only invoking the NearestFunction once:

KPosition3[x_, y_] := 
  Block[{step0, step1}, 
   step0 = Thread[x -> Range[1, Length@x, 1]];
   nf = Nearest[step0];
   step1 = nf[#, y] & /@ x];

Running your three timing tests gives:

{0.003057, 0.004344, 0.051009}