Download

Quick vector initialization efficiency question

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



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?

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.

It’s also legal under C++11 to use uniform universal initialization (brace-initialization) to initialize vectors:


 
 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.