English 中文(简体)
how can i make this equation faster
原标题:

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.

最佳回答

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.

问题回答

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.

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

See here for reference.

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.

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.

Check that InitialDistance != 0 first! :)





相关问题
What to look for in performance analyzer in VS 2008

What to look for in performance analyzer in VS 2008 I am using VS Team system and got the performance wizard and reports going. What benchmarks/process do I use? There is a lot of stuff in the ...

SQL Table Size And Query Performance

We have a number of items coming in from a web service; each item containing an unknown number of properties. We are storing them in a database with the following Schema. Items - ItemID - ...

How to speed up Visual Studio 2008? Add more resources?

I m using Visual Studio 2008 (with the latest service pack) I also have ReSharper 4.5 installed. ReSharper Code analysis/ scan is turned off. OS: Windows 7 Enterprise Edition It takes me a long time ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

热门标签