For something to experience pain, some information needs to exist (e.g. in the mind of the sufferer, informing them that they are experiencing pain). There are known information limits, e.g. https://en.wikipedia.org/wiki/Bekenstein_bound or https://en.wikipedia.org/wiki/Landauer%27s_principle
These limits are related to entropy, space, energy, etc., so if you further assume the universe is finite (or perhaps equivalently, that the malicious agent can only access a finite portion of the universe due to e.g. speed-of-light limits), then there is an upon bound of information possible, which implies an upper bound of pain possible.
This seems like an odd choice for your primary example.
Is the primary concern that a sufficiently smart compiler could take your matrix multiplication and turn it into a vectorized instruction?
Is it only applicable in certain languages then? E.g. do JVM languages typically enable vectorized instruction optimizations?
Is the primary concern that a single matrix multiplication is more maintainable than nested for loops?
Is it only applicable in certain domains then (e.g. machine learning)? Most of my data isn’t modelled as matrices, so would I need some nested for loops anyway to populate a matrix to enable this refactoring?
Is it perhaps worth writing a (short?) top level post with an worked out example of the refactoring you have in mind, and why matrix multiplication would be better than nested for loops?