speed-up using vectorization instead of iteration