It seems that using -Vector3.forward and Vector3.back provide the same functionality. My curiosity is whether or not it makes a difference in the underlying code, or are they just basically shortcuts to the same struct? Will functionality be affected by using them interchangeably, or is there a specific use for one over the other in different circumstances?
It’s the same thing, but I believe -Vector3.forward
will get multiplied by -1 where Vector3.back
is a constant value. The compiler may be smart enough to precalculate this since Vector3.forward
is also a constant but I cannot say that with absolute confidence. Multiplication is fast enough that, unless you’re doing this a lot each frame, it will have no real impact.
Edit
Had a quick look. If the compiler is really smart, this -Vector3.forward
may only be about 2 or 3 instructions per component (x, y and z) but there would be some moving of data I’m not accounting for. This means that that multiplication would complete in less than 1 millisecond (multiple times, too).
I don’t think the compiler is that smart but it would still be incredibly fast. I don’t think you need to worry about performance here.
This is, of course, if it’s calculating the value with each use. If it precalculated it, it would be exactly the same as Vector3.back
in the compiled code
Another Edit
I misspoke. Vector3.forward
and Vector3.back
are not constants, they’re static variables. So, the compiler probably will not precalculate the value, although it may find that the values don’t change beyond their initialisation and treat them as constant values. Again, I cannot promise this is the case. I don’t know the in-depth workings of the compilers and their optimisation strategies
Then from that speculation, we should be able to assume that -Vector3.forward
and Vector3.back
as well as the inverse -Vector3.back
and Vector3.forward
operate identically. If the calculation difference is negligible anyway, and I see no reason it wouldn’t be, then the performance impact should be negligible unless you have thousands of these executing simultaneously in the same scene, which I see no reason for doing.
Thanks for helping clear this up a bit. Might try to do some research later to dig into compiler function, but that’s just my morbid curiosity being what it is.
I just realised I rambled on about performance and you didn’t even ask that. Sorry
They are not shortcuts to the same struct. Vector3.forward
is defined as new Vector3(0f, 0f, 1f)
and Vector3.back
is new Vector3(0f, 0f, -1f)
. Using -Vector3.forward
is the same as writing Vector3.forward * -1f
which will result in Vector(0f, 0f, -1f)
- the same as Vector3.back
No, the final value is the same so the functionality will be the same.
Correct. Although I would personally recommend using Vector3.back
over -Vector3.forward
simply for consistency and readability.
Enjoy. Compilers are highly complex but knowing even just a little of what they do and how they do it can make help you write better, more performant code
Ok, that makes a lot more sense broken down in that manner. So utilizing -Vector3.forward
and -Vector3.back
is actually adding a small step in the calculation. Again, I think it’ll be negligible from a performance aspect (I didn’t ask about it directly, but that was part of the underlying concern, so good catch) unless you have an obscene amount of these happening each frame. This answers my questions perfectly: 1. The end result will be the same regardless of what is used; 2. There is a functional difference, but it’s not important in the grand scheme; and the bonus unasked-yet-implied question 3. Performance should not be affected under less than extreme circumstances. Perfect. Thank you so much for your help in understanding this!
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.