Faster implementation of var across dimensions