How can SO encourage people to explain their answers, so they are generalizable to other questions?
Comments sometimes work. Some people just reply that they'll do that next time, and IDK if that happens.
You can even downvote if you want to apply more pressure, especially if that won't make the score negative (if you don't think it deserves it.) The mouseover on the downvote button says "this answer is not useful". A valid interpretation of that is "not useful enough", as in doesn't meet the quality standard you expect for an SO answer.
I've had this happen: I posted a very terse answer that did answer the question, but IIRC only for people with the required background knowledge to understand it, who probably wouldn't have needed to ask the question in the first place. I forget which question (likely something about explaining a performance effect in terms of computer architecture and/or compilers), and I forget how bad it was, like whether it would likely have helped the OP or not. I think it was somewhat better than just posting for the sake of being smug, but I think I was kind of annoyed with such a simple question, IIRC, or something like that. Oh, I think it was going to be a comment, but then I realized it was technically an answer to the question so shouldn't be posted as a comment, so I posted it as an answer. A downvote or two from (what I assumed were) "fools who don't understand my genius" motivated me to edit it into a much longer answer which did explain what was going on in terms more people could understand, from where it went on to get multiple upvotes. (I'm phrasing it for humour, but at the time I felt like my answer didn't deserve downvotes, and that people who downvoted should learn the subject before voting on answers about it or something. Maybe not deserve many upvotes, but it was still technically an answer. Anyway, with hindsight and getting over being maybe grumpy about the question (or the general state of question quality) when posting the answer, it's not totally unreasonable for people to downvote an answer that's not useful to them. And for me to in future take a few more seconds to see if any more thoughts occur about stuff to say before hitting "post".)
I would not recommend mentioning your downvote in a comment that requests improvement; that could come across as holding their answer hostage. Or holding them hostage, since when people have bad reactions to voting, it's often from taking it personally, as if we were voting on them as a user or as a person, not on the contents of their post. (That kind of phrasing, like I'll reverse my downvote when you fix this problem, is ok IMHO on questions with problems like images of text, where that's a clear problem that can be fixed with just copy/paste as text.)
However, you could encourage improvement with a carrot, not a stick, by saying something like
This is a good speedup, but it would help other future readers with similar problems a lot more if you explain what you changed and why. I'd like to upvote an answer that looked like that.
In this case you're asking them to provide an explanation, rather than fix a mistake or explain something differently/better. In past cases of commenting on questions to request improvements, I've more often than not had good results from explaining in comments what bugs or problems or downsides exist in an answer (including saying what the right or better approach would be). Sometimes I end up making an edit myself if the author isn't understanding what I'm saying, and 95% of the time that's well-received, especially when it's a change the author had been trying to make at my suggestion in the first place. So IDK if asking for explanations could tend to go the same way.
Also, most of my experience with this is in the [assembly] tag, where people writing answers that need such improvement are often beginners that are trying to apply the small amount of knowledge they do have. Or experienced users that got lazy with an answer, or that weren't aware of some performance details or tricks.
(Most of this meta answer was written assuming answers from new users, who maybe aren't familiar with what a good answer looks like. Adjust as necessary if scolding experienced high-rep SO users that ought to know better and just got lazy, or didn't have time when they first posted to write a good answer.)
It looks like that's what actually happened in the case you linked. 2 downvotes and a much-upvoted comment shamed(?) the high-rep poster of that answer into editing with a bullet-list of changes that looks like the kind of thing such an answer should contain. The downvoters can now hopefully remove their downvotes.
Isn't that the point?
Yes, absolutely. Code-only answers without even a changelog summary require way more work for any future reader to figure out whether any of the same fixes could help for their problem, since they'd have to first read the question in detail.
When change to a few lines are marked with comments inside the code block but there's no text explaining why, that's only slightly better. Further along the sliding scale, there is some actual answer about why, but it's buried in comments, possibly inside a multi-page code-block.
smsurv
function". It's not required to state what makes it faster than the original code, since the question wasn't (despite the original unfocused title) "How do I make any arbitrary function containing a for loop faster?". The question could however benefit from adding context about whatsmsurv
is, and of course, having a useful-to-others title.