Sign in to follow this  
wyv

optimization question + equation library?

Recommended Posts

wyv    122
Is there a way to optimize the distance equation in C to get it faster than this: d = sqrt(x*x + y*y + z*z); //3d loss in accuracy is acceptable, to a degree, if the speed gain is substantial. Also, does there exist an open source math/physics C library that covers a wide array of math/physics equations? For example, one that covers collisions of all sorts, distance finding (between points, along equations, etc), and a lot of the math/physics questions we see in this forum?

Share this post


Link to post
Share on other sites
Pxtl    354
Actually, I have a similar question: does anyone know a good "language" for user-supplied equation parsing and solving? I used a Python interpreter for an app, but it involved an absolutely monstrous amount of events. 90% of the events were simple arithmetic like "return (mouseX * mousespeed)" and simple things like that, events we wanted developers to have access to without recompiling, but at the same time were way too numerous. THere is substantial overhead involved in converting the argumetns to PyObjects, fetching the function, launching the function with the arguments, and returning the results. Even worse, we were using the functions as a flat file instead of a PyFunction object, so the pycode object had to be launched with the arguments in the namespace dictionary instead of the argument tuple, meaning the interpreter was using hashing to fetch the arguments instead of tuple index lookups.

In other words: Python too slow and heavy for user-defined arithmetic. Want fast embedding "language" for running user-defined equations with engine-supplied variables int a program.

Share this post


Link to post
Share on other sites
Raloth    379
You may not even need to do the square root at all. For example, if you use it to compare two distances, you can leave them both squared.

Share this post


Link to post
Share on other sites
Charles B    863
Exact Raloth. In my experience, the only significant (time consumming) cases where sqrt or rsqrt are actually required are for normalization of vectors, that is, in practice, recomputing normals in RT (ex : deformable mesh) for lighting. It's rarely required for collision detection or culling.

Else, as mentionned the rsqrt topic has been discussed a lot here.

Also always give your context of use. Generic optimizations mean nearly nothing unless you write a math library like me. There is always a context to exploit. The more additionnal info, the more opportunities to optimize. If you process an array of vectors with some continuity of norm, a very speedy normalization is possible using a variant of NR.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this