How do I fill a vector in one line of code?

So if I have a vector 3 and I know what it should be in x,y, and z, what is the most efficient way to fill it?

If I go:

Vector3 victor;
...
victor.x = a;
victor.y = b;
victor.z = c;

then I just used three processor cycles to fill that vector. That’s 2.85 x 10^-10 seconds. Ain’t nobody got time fo dat.

I would assume there is a way to fill it in one line, but I don’t know the proper syntax.
I tried:

victor = (a,b,c);
victor = [a,b,c];
victor = <a,b,c>;

but all these give an error.

(Working in C#, BTW.)

victor = new Vector3 (a, b, c);

Okay! Thank you.

That’s one line of code, but it’s actually slower, as it does exactly the same thing plus a method call with three arguments. Your first method is the fastest way to assign those three values independently.

Unless you’re doing this thousands of times each frame, it’s not significant.

Frankly, the different of performance is beyond tiny; 0,000006 ms per loop. I would really not care unless parsing hundred of thousands of vectors per frame.

I just figure it’s best to be in the habit of writing code as efficiently as possible. Sure, with modern speeds we are likely to never see even a hint of a difference, so I won’t fault someone for it. But here a little and there a little… Maybe it will make a slight difference for a machine never meant to run the game. :slight_smile:

That’s what’s known as doing “micro-optimizations.” If it takes any extra time to code or could introduce extra complexities, it’s worth questioning whether that time could be spent on real optimization, if you even need to spend time on it at all. In the case of vector initialization, it’s no big deal either way. But I don’t know that I would go as far as saying that micro-optimizations are generally a good habit to get into, especially if they affect development time and/or code maintainability, readability, etc.

Your game on such a machine is much, much more likely to be GPU bound to begin with. I really would just focus on results rather than any optimisations at all. I’ve finished quite a few games, many on sale and I only optimise at the end. It’s always better like that as you get to the end to begin with.

I doubt of that. When you create a struct instance (ie. “Vector3 point;”) the default zeroing constructor is called which I think is equivalent to calling a custom constructor.
Plus, when assigning x, y and z independently, you are doing three more assignments. So I’m almost sure it is actually faster to do Vector3 = new Vector3(a,b,c);

I did test it, and the complex constructor is slower. My guess would be because you pass values (int) to a method, therefore making copies of them too. The difference is 6ms per millions of operation.

It’s true; I’ve tested it before and

Vector3 v3 = new Vector3(1, 2, 3);

is slower than

Vector3 v3;
v3.x = 1;
v3.y = 2;
v3.z = 3;

But not by a huge amount. I seriously doubt you’d ever want to use the second method unless you’re creating zillions of Vector3s at once and need every last bit of speed.

–Eric

Did something change recently? I thought the last time I tried to assign individual components of a vector3 using c# the compiler got real sad with me. Granted it was long ago and I always use the first method… its just that second method feels like it didn’t work for me.

If it does work then ill just chock that up to me incorrectly converting some unityscript to c# back in my transition days.

Nothing has changed recently. Or ever. :wink: If you’re talking about stuff like transform.position.x = 5, that’s a different thing.

–Eric

Yeah that was most likely it now that I see that… thanks

The default constructor is not called. That’s why the compiler complains when you don’t assign values to all components yourself before using the vector.

Assigning the values yourself is simply setting three floats. Calling the constructor is a code jump and pushing three values on the stack, then doing the exact same three assignments, popping the stack, then jumping back while pushing the result vector and then popping it into your desired vector. Jumping into the Vector3 code might mean a cache miss if you’re not using it elsewhere for a long time (not using + on vectors and so on, as that’s a method call too), which can cascade into far worse a performance loss than you’d expect. This, however, is micro optimization. It can make a huge difference in very specific cases, but it’s the last thing you’d check, and only if you really know how computer hardware works.