# Slow Sum with Subscripts

Edit: My original question implicated D in the problem, but it seems unrelated, so I've removed that part of the question.

I am writing a function that uses functions involving subscripts and an open-ended Sum. It's unreasonably slow. Here's a minimal example (the real situation is much worse):

Sum[
-2 Exp[-(xx - Subscript[x, k])^2/(V + Subscript[Vx, k]^2)] Subscript[n, k] (xx - Subscript[x, k])/(V + Subscript[Vx, k]^2)
, {k, nsp}] // AbsoluteTiming


Sum[
-2 Exp[-(xx - subscript[x, k])^2/(V + subscript[Vx, k]^2)] subscript[n, k] (xx - subscript[x, k])/(V + subscript[Vx, k]^2)
, {k, nsp}] // AbsoluteTiming


Well, good thing I'm not using Superscript, because that's 10X worse:

Sum[
-2 Exp[-(xx - Superscript[x, k])^2/(V + Superscript[Vx, k]^2)] Superscript[n, k] (xx - Superscript[x, k])/(V + Superscript[Vx, k]^2)
, {k, nsp}] // AbsoluteTiming


So, is there some way to tell Sum not to bother trying anything fancy? And what's with the 100-fold range of timing of otherwise identical code?

N.B.: these timings change on rerunning the same code due to some kind of caching

• This is not a speed-up from "without subscripts" but after using "without subscripts" just apply x_[j] -> Subscript[x, j]].
– JimB
Jun 26 '21 at 21:27
• Use Format[x[j_]] := Subscript[x, j] and similar, then the indexed variables will display as subscripts in the output. Jun 26 '21 at 23:35
• You could use Block to temporarily redefine Sum and Subscript with your own versions. These need only implement minimal functionality Jun 27 '21 at 7:05
• @JimB Yeah I thought of that, but applying that back-transformation eats up basically all of the time-savings. Jun 27 '21 at 15:30
• Why not wrap Inactive around your sum, i.e., D[Inactive[Sum][...], ..]? Jun 27 '21 at 16:04

Seems like the Sum option I was looking for is Method -> "Procedural":
Sum[