-2

I am completing a tutorial on unity, and came across a block of code that i was supposed to copy into the editor (Option 1) I am wondering if my use of the new operator in Option 2 is any more or less performant than the suggested code.

void Update()
{
    Option1();
    //or
    Option2();
}

private void Option1()
{
    Vector2 position = transform.position;
    position.x = position.x + 0.1f;
    transform.position = position;
}

private void Option2()
{
    transform.position = new Vector2(transform.position.x + .1f, transform.position.y);
}

}

  • 9
    This calls for the Speed Rant: https://ericlippert.com/2012/12/17/performance-rant/ – Christopher Aug 02 '19 at 18:42
  • These are not the same and does it really matter, what's the issue you are facing? – Trevor Aug 02 '19 at 18:44
  • 1
    there's also `transform.position += new Vector2(0.1f, 0f);` You can even make that vector a constant. I wouldn't worry about performance here, just correctness and readability. – juharr Aug 02 '19 at 18:50
  • I tried that first, but it will bring up the following error message : Operator '+=' is ambiguous on operands of type 'Vector3' and 'Vector2'. – JaceXanthos Aug 02 '19 at 19:26

2 Answers2

0

Technically, in Option 1 you are creating a new instance of a variable 'Vector2 position = transform.position;' This means that you are allocating memory space for the information the same way as Option2.

If we break down the rest of the code, you can think of code as a series of operators and each operator is a jump / calculation made by the CPU.

Breaking Down Option 1:

private void Option1()
{
    // Memory allocation, =, and reference call '.'
    Vector2 position = transform.position;
   // Reference call, =, reference call, +
    position.x = position.x + 0.1f;
    // reference call, =
    transform.position = position;
}

Through those lets say "they take the same amount of processing time", so 9 actions took place.

Breaking down Option2:

private void Option2()
{
    // Reference call, =, memory allocation, reference call, +, reference call);
    transform.position = new Vector2(transform.position.x + .1f, transform.position.y);
}

Through those lets say "they take the same amount of processing time", so 6 actions took place.

Based on just generalizing the actions to the same time, Option2 will take 2/3 the time that Option1 will.

Note: This is a number so insignificant with today's processors that unless you are doing this calculation 1,000+ times per frame it doesn't matter.

0

You can give a look to this post.

Performance cost of 'new' in C#?

As far as I can guess if you can perform your tasks avoiding new, should be more optimal. More than for the new itself, because you trigger the garbage collector. Working with pointers, by reference and avoiding copies and instances is always faster, however for small projects is not a big deal and I at list care more first of making things work. As far as I have encountered, what should be considered in large projects regarding performance is to keep a stable frame rate (moslty in virtual reality), mainly being careful with the abuse of coroutine usage.

Hope that helps!

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47
  • 1
    Since `Vector2` is a struct memory allocation and garbage collection are not much of an issue. – juharr Aug 02 '19 at 19:01
  • I did not say they are an issue and I agree the the different is as insignificant as it can be. However if the question is if one option is any more or less performant than the other, I would say there is a difference – rustyBucketBay Aug 02 '19 at 19:07