How efficient is glm when it returns the result

Started by
3 comments, last by ill 11 years, 5 months ago
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]
Advertisement

  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.
  2. Yes, modern compilers should optimize out such assignments in release (optimized) mode.
Lauris Kaplinski

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya
Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/
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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

I believe the relevant optimization technique here is Return value optimization.
Ah very nice. Now I can stop worrying about this and just write code.

This topic is closed to new replies.

Advertisement