Joseph Wakeling wrote:
> Thanks to all for helpful answers about this!
>
> Flash Gordon wrote:
>> Global (or at least file scope) variable named x and n? Yuk.
>
> Normally I would agree. Originally these variable came as part of a
> structure called "state", so the system was referring to state->x,
> state->n and state->shuffle. Given the content of the program and how
> it's going to be used (as an individually compiled module) it's not a
> big deal.
I stand by my comment. If I had to review that code I would tell you to
go and fix it. After all, some other poor chap may have to maintain that
code.
>>> return;
>> Rather pointless having a return here IMHO.
>
> That came with the GSL and I left it in. return; at the end of a void
> function is a matter of preference anyway, right? It doesn't
> functionally affect things but some people prefer it for readability.
Agreed.
>> It's called minimising scope. Because they are declared in the loop you
>> know, without having to check, that they are not used outside.
>> Otherwise, you would have to read beyond the end of the loop to see if
>> the last value gets used outside the loop.
>
> OK. Does it make any difference to the speed at which the code runs?
> Let's assume I'm going to be calling this function a LOT so small
> differences add up.
It depends. The only way to find out it to measure on your specific
system. However, my opinion would be that with modern compilers it will
generally make absolutely no difference one way or the other.
If you are concerned with speed the first thing to do is forget it
unless you have a very good reason to think it won't be fast enough.
If you actually do find yourself with a real performance issue, don't
start by looking at the code, start by looking at the algorithm to see
if there is a more efficient algorithm.
My experience is that trying to optimise one function only helps if that
function is very badly written, the compiler is very bad, or you are
doing embedded work and there is a time constraint on that specific
function (e.g. it has to complete during the video blanking period).
Other than those rare cases there is more to be had by improving the
algorithm.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.