The odd extra bracket isn't the point - it's the maximal iterations in your algorithm that is the issue. Although the suggestion about using one liners inline does save a fair amount. But a really well-conceived algorithm ought to reduce iterations by several orders of magnitude. labelled loops with winning exits might be a start. Also, sorting data once to reduce iterating through is better than always iterating the lot. Even with rand, you could generate blocks of data in advance and sort them, rather than pick up values on the fly.