Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by Marscaleb, Jan 23, 2014.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    992
    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:
    Code (csharp):
    1. Vector3 victor;
    2. ...
    3. victor.x = a;
    4. victor.y = b;
    5. 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:
    Code (csharp):
    1. victor = (a,b,c);
    2. victor = [a,b,c];
    3. victor = <a,b,c>;
    but all these give an error.

    (Working in C#, BTW.)
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Code (csharp):
    1.  
    2. victor = new Vector3 (a, b, c);
    3.  
     
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    992
    Okay! Thank you.
     
  4. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    950
    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.
     
  5. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    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.
     
  6. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    992
    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. :)
     
  7. Steve-Tack

    Steve-Tack

    Joined:
    Mar 12, 2013
    Posts:
    1,240
    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.
     
  8. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    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.
     
  9. Casto

    Casto

    Joined:
    Aug 14, 2013
    Posts:
    44
    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);
     
  10. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    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.
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's true; I've tested it before and

    Code (csharp):
    1. Vector3 v3 = new Vector3(1, 2, 3);
    is slower than

    Code (csharp):
    1. Vector3 v3;
    2. v3.x = 1;
    3. v3.y = 2;
    4. 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
     
  12. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    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.
     
  13. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Nothing has changed recently. Or ever. ;) If you're talking about stuff like transform.position.x = 5, that's a different thing.

    --Eric
     
  14. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Yeah that was most likely it now that I see that... thanks
     
  15. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    950
    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.