wrote:
> I don't believe measuring it on one platform is sufficient, since my
> program is deployed on many platforms.
Which is sensible, but if you can't measure it on all your target platforms,
then how do you expect us to know which is faster without even knowing what
those platforms are, or which are most important to you ?
> The code is in a method at the
> high end of the execution chain, which I would like to optimize as much
> as possible. If it truly doesn't matter, then I'll go with the more
> maintainable solution (since its a 1 liner):
It is perfectly possible (though, I'd guess, not too likely) that if this code
is in the inner loop of your application then its speed is critical for the
overall app. If you suspect that might be the case then /measure/ it.
BTW, when you do measure (if you bother), don't forget to test a version
without the modulus operator:
if (++pos >= bytes.length)
pos = 0;
It involves a branch (but one which, I imagine, would normally be correctly
predicted by the CPU -- if you are running on a machine with instruction
pipelining and branch prediction), but saves a mod operation. You (unless you
know a lot more about your target architectures than you have said) can't guess
which is quicker, so -- again -- you have to measure it. I might be worth
putting bytes.length into a local variable too, OTOH that might be a waste of
time -- it depends on whether the optimiser in the JIT (if any, on your target
platforms) spots the repeated use of bytes.length by itself.
But, whatever you do, DON'T use:
> I'll go with the more
> maintainable solution (since its a 1 liner):
>
> pos = ++pos % bytes.length;
It pre-increments pos and assigns to it -- which is just daft.
-- chris