lundi 23 juillet 2018

Haxe: Efficiency of mapping a variable to one of two values

I feel this question is too general and poorly asked. Improvement suggestions would be appreciated.

Say I have an integer variable which I need to convert to one of two other values. If the variable is 0, it will remain 0, otherwise, it will become 1. I can think of two ways to do this.

Method 1: An inline if/else assignment.

function runFunc(input:Int):Void
{
    <script>
}

for (index in 0...5)
{
    runFunc(if (index == 0) {0;} else {1;});
}

Method 2: Division and rounding.

function runFunc(input:Int):Void
{
    <script>
}

for (index in 0...5)
{
    runFunc(Math.round(index / index));
}

Method 1 is more standardised and would work for other data-types and other values, but Method 2 seems like it would take less processing power (especially if this needs to be done almost constantly).

Assuming that Method 2 wouldn't have a problem with rounding, is there enough of a difference in times between the two methods to consider using one over the other? How do different things like if/else statements and Math.round() impact processing time?

Aucun commentaire:

Enregistrer un commentaire