Does flatten code would be more efficient than this loop ?