Is there a way to do a quick and dirty 3D distance check where the results are rough, but it is very very fast? I need to do depth sorting. I use STL sort
like this
You can leave out the square root because for all positive (or really, non-negative) numbers x
and y
, if sqrt(x) < sqrt(y)
then x < y
. Since you're summing squares of real numbers, the square of every real number is non-negative, and the sum of any positive numbers is positive, the square root condition holds.
You cannot eliminate the multiplication, however, without changing the algorithm. Here's a counterexample: if x
is (3, 1, 1) and y
is (4, 0, 0), |x| < |y|
because sqrt(1*1+1*1+3*3) < sqrt(4*4+0*0+0*0)
and 1*1+1*1+3*3 < 4*4+0*0+0*0
, but 1+1+3 > 4+0+0
.
Since modern CPUs can compute a dot product faster than they can actually load the operands from memory, it's unlikely that you would have anything to gain by eliminating the multiply anyway (I think the newest CPUs have a special instruction that can compute a dot product every 3 cycles!).
I would not consider changing the algorithm without doing some profiling first. Your choice of algorithm will heavily depend on the size of your dataset (does it fit in cache?), how often you have to run it, and what you do with the results (collision detection? proximity? occlusion?).