0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Vector3の一部を変更する時にnew Vector3を避けるべきか?

Last updated at Posted at 2021-10-01

transform.position等を操作する時、「z軸だけを変更したい」といったケースが多い。

その場合
org = new Vector3(org.x, org.y, org.z + XX);
が良いのか、
org += Vector3.right * XX;
とするべきなのか。

あるいは「xz軸だけ取り出したい(y=0にしたい)」場合にも

org = new Vector3(org.x, 0f, org.z);
が良いのか、
org -= Vector3.up * org.y;
とするべきなのか。

new Vector3は処理が重いので乱用してはならないという話は良く聞くが、Vector3.up/right/forwardも重いという話を聞くので、実際に比べてみた。

(10/2追加) Vector3.rightは、内部的にnew Vector3をやっているとのコメントを受けて、あらかじめメモリ内にVector3.right結果を保管して使い回すパターンも追加。

float b = 1f;
Vector3 a;

var sw = new System.Diagnostics.Stopwatch();

for (int j = 0; j < 10; j++)
{
	sw = new System.Diagnostics.Stopwatch();
	a = new Vector3(1f, 2f, 3f);
	sw.Start();
	for (int i = 0; i < 100000f; i++)
		a = new Vector3(a.x, a.y, a.z + b);  //毎回new Vector3を実行

	TimeSpan res1 = sw.Elapsed;
	sw.Stop();

	sw = new System.Diagnostics.Stopwatch();
	a = new Vector3(1f, 2f, 3f);
	sw.Start();
	for (int i = 0; i < 100000f; i++)
		a += Vector3.right * b;  //Vector3.rightを使って計算

	TimeSpan res2 = sw.Elapsed;
	sw.Stop();

	sw = new System.Diagnostics.Stopwatch();
	Vector3 right = Vector3.right;  //あらかじめ用意したベクトルを使い回す
	a = new Vector3(1f, 2f, 3f);
	sw.Start();
	for (int i = 0; i < 100000f; i++)
		a += right * b;

	TimeSpan res3 = sw.Elapsed;
	sw.Stop();

	Debug.Log("new Vector3:" + res1 + "   +Vector3.right:" + res2 + "   +right:" + res3);
}

10回測定した結果は
new Vector3:00:00:00.0024888 +Vector3.right:00:00:00.0050720 +right:00:00:00.0058805
new Vector3:00:00:00.0018079 +Vector3.right:00:00:00.0048435 +right:00:00:00.0046762
new Vector3:00:00:00.0018118 +Vector3.right:00:00:00.0045480 +right:00:00:00.0050064
new Vector3:00:00:00.0018308 +Vector3.right:00:00:00.0044770 +right:00:00:00.0047784
new Vector3:00:00:00.0020278 +Vector3.right:00:00:00.0046388 +right:00:00:00.0046788
new Vector3:00:00:00.0018587 +Vector3.right:00:00:00.0045241 +right:00:00:00.0046999
new Vector3:00:00:00.0018140 +Vector3.right:00:00:00.0044989 +right:00:00:00.0051078
new Vector3:00:00:00.0019138 +Vector3.right:00:00:00.0044852 +right:00:00:00.0045006
new Vector3:00:00:00.0020559 +Vector3.right:00:00:00.0044618 +right:00:00:00.0044280
new Vector3:00:00:00.0017473 +Vector3.right:00:00:00.0044658 +right:00:00:00.0044725

new Vector3を使う方が、Vector3.rightを加算するより2倍早い。
Vector3.rightを変数に格納して使い回す方法でも変わらない様子。

計算に使うaやbを毎回ループ内で定義しなおしてみたり、乱数を入れた後に計算させてみても変わらなかった。
上記はWindows上での結果だが、android上でも同様。

これは同じframe内でループした結果であって、多数のオブジェクトがUpdate()内で毎frameに1回ずつ実行するような場合は変わるのだろうか?

0
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?