Public Group

# Which Way Would Have Better Performance?

This topic is 854 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

when checking if some object is within a certain area, which approach would have better performance if using glm library?

vec3 pos;

// Using a bounding box to represent that area

1) if ( pos.x > box.MinX && pos.x < box.MaxX

&& pos.y > box.MinY....) { // do something..}

// Using a sphere to represent that area

2) if ( glm::length ( pos - sphere.centerPosition ) <= sphere.radius ) { // do something..}

##### Share on other sites

if( glm::dot( d, d ) <= sphere.radius*sphere.radius ) { // do something..}

I know this is exactly the same mathematically as working out the squared length of d but is it such a good idea to use the dot product to do this? Correct me if I am wrong but it seems an unintuitive choice.

##### Share on other sites

That depends on personal preference and the idioms used in the rest of your codebase. I do a lot of shader programming in my job and quite often see dot(a,a) used instead of a.x*a.x+a.y*a.y+a.z*a.z, so to me it's intuitive.

If you're using it for the first time in a project, it will be less intuitive to a future reader, so you could use the longform instead, or leave a comment. Typically in CPU math libraries, you see two functions - length and lengthSquared, which is more readable than an 'abused' dot :wink:

Performance-wise, it shouldn't make a difference as long as you trust your compiler... but if you're on a platform with a native dot instruction and a dot intrinsic, then the dot version would be create the best chance for the compiler to emit that instruction.

##### Share on other sites
The 2 methods are not equivalent, because a box is not a sphere. Therefore, whatever your "certain area" is, it can only be correctly represented by one of those 2 methods.

##### Share on other sites

The 2 methods are not equivalent, because a box is not a sphere. Therefore, whatever your "certain area" is, it can only be correctly represented by one of those 2 methods.

Of course the two methods are not the same. But in this case I don't care about the exact dimension of the area.

Edited by gamervb

##### Share on other sites
Well given that perfermance is not really a determining factor here (you could of course profile and see which is faster if you really want to) I think that Kylotan is right - you could choose which one better fits your area. I know you say you dont care - but if you gotta pick one way or another - why not?

##### Share on other sites

I'd say go for what you think does it for you with readable/ understandable code.

if performance becomes an issue, you can profile and change it (which I don't expect for this case).

##### Share on other sites

and change it (which I don't expect for this case).

What do those things mean?

##### Share on other sites

I think that Kylotan is right - you could choose which one better fits your area.

He didn't say anything about I could choose which one better fits my area. He merely thought I couldn't represent a sphere with a cube, and I think anyone knows that.

1. 1
2. 2
3. 3
4. 4
Rutin
13
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013435
×