views:

122

answers:

6

right now i have a bottle neck in my program I'm trying to write. i am trying to use a pinch gesture to control the scale of a UIImage. its the calculation of the scale that is causing the program to slow down and become choppy. below is the equation.

currentScale = (currentDistance / initialDistance) * scaleMod;

scaleMod is what ever the current scale was the user took their fingers off the screen. so the next time the user does a pinch the old scale is essentially the starting point of the new scaling action.

+2  A: 

1) Can't you calculate scaleMod / initialDistance once while currentDistance is changing. That way you only have do that value times currentDistance, which removes a divide.

2) Make sure that this is actually the bottleneck. It most likely isn't, unless your doing something really wrong.

Dan McGrath
A: 

You could store scaleMod / initialDistance. When scaling is active (the user's fingers are still on the screen), multiply that value by currentDistance as needed.

Once the user has finished pinching, store the new scaleMod / initialDistance value for the next time pinching happens.

Aaron
+1  A: 

If you fix the scaleMod and initialDistance to powers of 2 you could use shifts for faster multiplication and division.

See here for reference.

Cesar
A: 

if you are doing computation with int (or other integer), see if he can do it using float precision. Floating-point divide is faster than integer (fewer bits to divide, assuming your CPU) has floating-point unit).

also, try to factor out division as multiplication by a reciprocal.

aaa
In what universe is floating point faster than integer for a divide? Granted, there are pathological sets of floats and degenerate sets of integers for which this will be true. You seem to be making a general statement.
gary
@gary it is generally true. Look up how floating-point numbers are represented (hint: recall division of powers). Compare instruction latency of IDIV and FDIV, http://siyobik.info/index.php?module=x86
aaa
The latency is just pipeline overhead, and isn't useful in defining "faster" for a real application. To declare an instruction "faster" because of lower latency is like defining the winner of an auto race according to his travel time to reach the track. Sure, it has some meaning, but not much. When you want to see how an instruction performs in an actual application, you need to look at throughput. In that respect, integers are always as fast or faster.
gary
@gary throughput is the same for integer and floating-point DIV.it may be different for SSE instructions, but I am not familiar with integer SSE instructions.
aaa
So we agree - integers are "as fast or faster", not slower
gary
+2  A: 

For any type of the three vars, this calculation can easily be done millions of times per second with little performance impact. Your problem is elsewhere.

Pavel Radzivilovsky
A: 

Check that InitialDistance != 0 first! :)

JasonBlackwoodStone