If you think that's what the code is doing, why do you keep all the old values in an array in memory? And yes I can think of a way of doing this, it's more or less the same as yours, just without the storing ALL the values part.