A
Alex Hunsley
Boudewijn said:That method always runs in the same amount of VM ticks. It takes:
- 3 comparisons;
- 3 copies; and
- 3 jumps.
How about:
private static float maximum(float a, float b, float c, float d)
{
float max = a;
if (b > max) max = b;
if (c > max) max = c;
if (d > max) max = d;
return max;
}
which takes:
- 1..4 copies;
- 3 comparisons; and
- 3 jumps.
The average number of copies is calculated as follows:
if a is maximum: 1 copy
if b is maximum: 2 copies
if c is maximum: 3 copies
if d is maximum: 4 copies
average copies = (1 + 2 + 3 + 4) / 4 = 10 / 4 = 2.5
Note that 2.5 < 3
I was going to suggest the same code structure as you wrote, but merely
because it's much more readable and obvious if you've written it wrong!