Created
January 15, 2019 10:12
-
-
Save msklywenn/8a8c561b0d2345e9d176c8685075514b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.Profiling; | |
using System.Runtime.CompilerServices; | |
using System.Collections.Generic; | |
public class VectorTest : MonoBehaviour | |
{ | |
Vector3[] array; | |
List<Vector3> list; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
array = new Vector3[313370]; | |
list = new List<Vector3>(array.Length); | |
for (int i = 0; i < array.Length; i++) | |
{ | |
array[i] = new Vector3(Random.value, Random.value, Random.value); | |
list.Add(new Vector3(Random.value, Random.value, Random.value)); | |
} | |
} | |
static Vector3 FastScalarMul(Vector3 v, float f) | |
{ | |
Vector3 result; | |
result.x = v.x * f; | |
result.y = v.y * f; | |
result.z = v.z * f; | |
return result; | |
} | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | |
static Vector3 FastInlinedScalarMul(Vector3 v, float f) | |
{ | |
Vector3 result; | |
result.x = v.x * f; | |
result.y = v.y * f; | |
result.z = v.z * f; | |
return result; | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
Profiler.BeginSample("Unity"); | |
for (int i = 0; i < array.Length; i++) | |
array[i] *= 42f; | |
Profiler.EndSample(); | |
Profiler.BeginSample("Fast"); | |
for (int i = 0; i < array.Length; i++) | |
array[i] = FastScalarMul(array[i], 42f); | |
Profiler.EndSample(); | |
Profiler.BeginSample("Inlined"); | |
for (int i = 0; i < array.Length; i++) | |
array[i] = FastInlinedScalarMul(array[i], 42f); | |
Profiler.EndSample(); | |
Profiler.BeginSample("Direct"); | |
for (int i = 0; i < array.Length; i++) | |
{ | |
array[i].x *= 42f; | |
array[i].y *= 42f; | |
array[i].z *= 42f; | |
} | |
Profiler.EndSample(); | |
Profiler.BeginSample("ListUnity"); | |
for (int i = 0; i < list.Count; i++) | |
list[i] *= 42f; | |
Profiler.EndSample(); | |
Profiler.BeginSample("ListFast"); | |
for (int i = 0; i < list.Count; i++) | |
list[i] = FastScalarMul(list[i], 42f); | |
Profiler.EndSample(); | |
Profiler.BeginSample("ListInlined"); | |
for (int i = 0; i < list.Count; i++) | |
list[i] = FastInlinedScalarMul(list[i], 42f); | |
Profiler.EndSample(); | |
Profiler.BeginSample("ListDirect"); | |
for (int i = 0; i < list.Count; i++) | |
{ | |
Vector3 v = list[i]; | |
v.x *= 42f; | |
v.y *= 42f; | |
v.z *= 42f; | |
list[i] = v; | |
} | |
Profiler.EndSample(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment