I understand that the deep profile may be misleading so I made a new benchmark, using the stopwatch.
The function is this, running 100 million times. x,y,z replaced by “i”.
stopwatch.Start();
for (int i=0; i<100000000; i++)
{
CalculateFaceVertex(ref FaceVertex, 0, i, i, i);
}
stopwatch.Stop();
UnityEngine.Debug.Log(stopwatch.Elapsed.Milliseconds);
Here’s the first version of the function
Run time for 100 million iterations: around 900 ms
public void CalculateFaceVertex(ref Vector3[] FaceVertex, int ThisFaceId, int x, int y, int z)
{
Vector3 Origin = new Vector3(x, y, z);
for (int i = 0; i < 4; i++)
{
FaceVertex[i] = FaceVertexHelp[ThisFaceId, i] + Origin; //Find out why this is inefficient. Wrote topic on Unity forums.
}
}
Here’s the second version of the function
Run time for 100 million iterations: around 900 ms
public void CalculateFaceVertex(ref Vector3[] FaceVertex, int ThisFaceId, int x, int y, int z)
{
Vector3 Origin = new Vector3(x, y, z);
for (int i = 0; i < 4; i++)
{
//FaceVertex[i] = FaceVertexHelp[ThisFaceId, i] + Origin; //Find out why this is inefficient. Wrote topic on Unity forums.
FaceVertex[i].x = FaceVertexHelp[ThisFaceId, i].x + Origin.x;
FaceVertex[i].y = FaceVertexHelp[ThisFaceId, i].y + Origin.y;
FaceVertex[i].z = FaceVertexHelp[ThisFaceId, i].z + Origin.z;
}
}
Here’s the third version of the function
Run time for 100 million iterations: around 700 ms
public void CalculateFaceVertex(ref Vector3[] FaceVertex, int ThisFaceId, int x, int y, int z)
{
// Vector3 Origin = new Vector3(x, y, z);
for (int i = 0; i < 4; i++)
{
//FaceVertex[i] = FaceVertexHelp[ThisFaceId, i] + Origin; //Find out why this is inefficient. Wrote topic on Unity forums.
FaceVertex[i].x = FaceVertexHelp[ThisFaceId, i].x + x;
FaceVertex[i].y = FaceVertexHelp[ThisFaceId, i].y + y;
FaceVertex[i].z = FaceVertexHelp[ThisFaceId, i].z + z;
}
}
Here’s the fourth version of the function
Run time for 100 million iterations: around 600 ms
public void CalculateFaceVertex(ref Vector3[] FaceVertex, int ThisFaceId, int x, int y, int z)
{
Vector3 Origin = new Vector3(x, y, z);
for (int i = 0; i < 4; i++)
{
//FaceVertex[i] = FaceVertexHelp[ThisFaceId, i] + Origin; //Find out why this is inefficient. Wrote topic on Unity forums.
//FaceVertex[i].x = FaceVertexHelp[ThisFaceId, i].x + x;
//FaceVertex[i].y = FaceVertexHelp[ThisFaceId, i].y + y;
//FaceVertex[i].z = FaceVertexHelp[ThisFaceId, i].z + z;
Vector3Add(ref FaceVertex[i], FaceVertexHelp[ThisFaceId, i],Origin);
}
}
public void Vector3Add(ref Vector3 res, Vector3 a, Vector3 b)
{
res = a + b;
}
Here’s the fifth version of the function
Run time for 100 million iterations: around 400 ms
public void CalculateFaceVertex(ref Vector3[] FaceVertex, int ThisFaceId, int x, int y, int z)
{
//Vector3 Origin = new Vector3(x, y, z);
for (int i = 0; i < 4; i++)
{
//FaceVertex[i] = FaceVertexHelp[ThisFaceId, i] + Origin; //Find out why this is inefficient. Wrote topic on Unity forums.
//FaceVertex[i].x = FaceVertexHelp[ThisFaceId, i].x + x;
//FaceVertex[i].y = FaceVertexHelp[ThisFaceId, i].y + y;
//FaceVertex[i].z = FaceVertexHelp[ThisFaceId, i].z + z;
Vector3Add(ref FaceVertex[i], FaceVertexHelp[ThisFaceId, i],x,y,z);
}
}
public void Vector3Add(ref Vector3 res, Vector3 a, int x, int y, int z)
{
res.x = a.x + x;
res.y = a.y + y;
res.z = a.z + z;
}
Here’s the sixth version of the function
Run time for 100 million iterations: around 400 ms
public void CalculateFaceVertex(ref Vector3[] FaceVertex, int ThisFaceId, int x, int y, int z)
{
//Vector3 Origin = new Vector3(x, y, z);
for (int i = 0; i < 4; i++)
{
//FaceVertex[i] = FaceVertexHelp[ThisFaceId, i] + Origin; //Find out why this is inefficient. Wrote topic on Unity forums.
//FaceVertex[i].x = FaceVertexHelp[ThisFaceId, i].x + x;
//FaceVertex[i].y = FaceVertexHelp[ThisFaceId, i].y + y;
//FaceVertex[i].z = FaceVertexHelp[ThisFaceId, i].z + z;
Vector3Add(ref FaceVertex[i], FaceVertexHelp[ThisFaceId, i],ref x,ref y,ref z);
}
}
public void Vector3Add(ref Vector3 res, Vector3 a, ref int x, ref int y, ref int z)
{
res.x = a.x + x;
res.y = a.y + y;
res.z = a.z + z;
}
FYI, the missing code, but it won’t make a difference:
private static readonly Vector3[,] FaceVertexHelp = new Vector3[,]
{
{new Vector3(0, 1, 0), new Vector3(0, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 0)}, //top
{new Vector3(0, 0, 0), new Vector3(0, 0, 1), new Vector3(1, 0, 1), new Vector3(1, 0, 0)}, //bottom
{new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector3(1, 1, 0), new Vector3(1, 0, 0)}, //front
{new Vector3(0, 0, 1), new Vector3(0, 1, 1), new Vector3(1, 1, 1), new Vector3(1, 0, 1)}, //back
{new Vector3(0, 0, 1), new Vector3(0, 1, 1), new Vector3(0, 1, 0), new Vector3(0, 0, 0)}, //left
{new Vector3(1, 0, 1), new Vector3(1, 1, 1), new Vector3(1, 1, 0), new Vector3(1, 0, 0)}, //right
};
In the end, I still got a 2.25x improvement…
ps. 7th version, 100 million iterations in 330 ms, 2.72x improvement
pps. Adding aggressive inline to Vector3Add function leads to worse timings.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CalculateFaceVertex(ref Vector3[] FaceVertex, int ThisFaceId, int x, int y, int z)
{
//Vector3 Origin = new Vector3(x, y, z);
for (int i = 0; i < 4; i++)
{
//FaceVertex[i] = FaceVertexHelp[ThisFaceId, i] + Origin; //Find out why this is inefficient. Wrote topic on Unity forums.
//FaceVertex[i].x = FaceVertexHelp[ThisFaceId, i].x + x;
//FaceVertex[i].y = FaceVertexHelp[ThisFaceId, i].y + y;
//FaceVertex[i].z = FaceVertexHelp[ThisFaceId, i].z + z;
Vector3Add(ref FaceVertex[i], FaceVertexHelp[ThisFaceId, i],ref x,ref y,ref z);
}
}