I just extended @Michael-E2 answer by including FunctionCompile
:
lx = Range[40000, 59999];
bx = RandomSample[Range[20000], 10000];
ares = MapAt[#^2 &, lx, {#} & /@ bx]; // AbsoluteTiming
bres = SubsetMap[#^2 &, lx, {#} & /@ bx]; // AbsoluteTiming
(cres = lx; Do[cres[[k]] = cres[[k]]^2, {k, bx}]); // AbsoluteTiming
dres = Compile[{{lx, _Integer, 1}, {bx, _Integer, 1}},
Block[{dres = lx}, Do[dres[[k]] = dres[[k]]^2, {k, bx}];
dres]][lx, bx]; // AbsoluteTiming
(eres = lx; eres[[bx]] = eres[[bx]]^2); // AbsoluteTiming
ffun = FunctionCompile[
Function[
{Typed[lx, TypeSpecifier["PackedArray"]["MachineInteger", 1]],
Typed[bx, TypeSpecifier["PackedArray"]["MachineInteger", 1]]},
Block[{dresfres = lx}, Do[dres[[k]]Do[fres[[k]] = dres[[k]]^2fres[[k]]^2, {k, bx}]; dres]]];fres]]];
fres = ffun[lx, bx]; // AbsoluteTiming
ares == bres == cres == dres == eres == fres
(*
{1.3647729098,Null}
{0.05271280542107,Null}
{0.02524940229712,Null}
{0.00047740005062,Null}
{0.00018410001768,Null}
{0.00007680000713,Null}
True
*)