I used two formulas for calculating the variance which were almost the same but the derivation differs.
1) xVarCom <- (xSquareSum - (n*xMeanSquare)) / (n-1)
[Note: Mean square = sum square/n and squaresum is 1square+2square.....nsquare]
2) varLoop <- (varLoop + (x[i]-(xMean)^2)/(n-1)
I have given the limits as 1:n. Both gives the same variance, but when I give higher values of mean like more than 100 thousands the first one is getting break down. Also the first expression elapsed time is faster than the second one. why is this happening?