How many cycles do these operations take

[codebox]//First version

if(NP == 0) EP = 0;

else EP = inputArray[barNumber + INPUT_O];

//Second version

EP = inputArray[barNumber + INPUT_O] * abs(NP);[/codebox]

The above code statements produce the same result.

The question is which is faster.

Worst case scenario for the first, must be, half a warp goes if, half a warp goes else, taking 100% more cycles, but how many cycles is an if to begin with?

The later always performs an addition and a multiplication, that’s 8 cycles. How can I find out how many cycles the abs takes?

The time taken by the memory read and then write can differ quite a lot, I know that, I’m trying to understand it now.

You might find it illuminating to compile these two cases to the PTX level (nvcc --ptx) and compare the result from the compiler. Small divergence, like the first case, is probably much quicker (and easier to understand) than any mathematical trick to avoid the branch.