• Advertisement
Sign in to follow this  

How efficient is glm when it returns the result

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I see many vector libraries take an argument as the destination to the result of something like dot product, cross product, etc... The destination is allocated and written to.

I see glm always returning a value instead which seems like it can be more expensive. I always think, is there some kind of compiler optimization or other stuff behind the scenes that makes the whole thing efficient after all and isn't something for me to worry about?

As an example of what I'm talking about:

[source lang="cpp"]//passing the result as a destination
vec3 result;
dot(vecA, vecB, result);


//returning the result
glm::vec3 result = glm::dot(vecA, vecB);[/source]

Share this post


Link to post
Share on other sites
Advertisement
[list=1]
[*]You are right, this is not something you should worry about, unless you have profiled your program and identified it as a performance bottleneck. Plus chances are that if it will be the bottleneck then simply passing destination as function argument will not help you.
[*]Yes, modern compilers should optimize out such assignments in release (optimized) mode.
[/list]

Share this post


Link to post
Share on other sites
Yeah, generally this kind of thing falls into the realms of micro-optimization. You'd have to have a pretty extreme use-case for it to even register on any performance graph, so it's something that you really don't need to worry about.

Share this post


Link to post
Share on other sites
I believe the relevant optimization technique here is [URL="http://en.wikipedia.org/wiki/Return_value_optimization"]Return value optimization[/URL]. Edited by kloffy

Share this post


Link to post
Share on other sites
Ah very nice. Now I can stop worrying about this and just write code.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement