On the other hand, my impression of the OP was that the person was looking for a more efficient way to write the code -- they weren't really concerned with runtime benchmarks, but rather with how to make the code both flexible and compactly written, allowing it to handle an array of any size without making it bulkier. This is often a more important sense of "efficiency". No problem with xmath's answer in this respect.