Announcement

Collapse
No announcement yet.

Quick vector initialization efficiency question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Quick vector initialization efficiency question

    Just wondering which of these two lines I should be using.

    Code:
    FVector Size = (PaperAxisX * BoxSize2D.X) + (PaperAxisY * BoxSize2D.Y) + (PaperAxisZ);
    FVector Size = FVector(BoxSize2D.X, 1, BoxSize2D.Y);
    The first is used a lot in Paper2D (PaperAxisX/Y/Z are just unit vectors to convert the given 2D direction to 3D). I was wondering if skipping the vector math and using the constructor instead would be faster, slower or too small to matter?

    #2
    Assuming they actually give the same result, the second one would likely be faster (you can never be sure until you profile it, because compiler optimizations), but by such a small margin that it wouldn't matter unless you have to run this millions of times.

    Comment


      #3
      It's also legal under C++11 to use uniform universal initialization (brace-initialization) to initialize vectors:
      Code:
       
       FVector Size{ BoxSize2D.X, 1, BoxSize2D.Y };
      As Zeblote pointed out, I wouldn't expect any noticeable benefit, but I personally find brace-initialization cleaner to read and prefer to keep variable initialization visually distinct from assignments.

      Comment

      Working...
      X