〇、前言
Unity 性能分析工具的使用
??为了更好地分析程序的运行情况或者代码的运行性能,以后的文章都尽量使用 Unity 自带的 Profiler 窗口进行深入的性能分析! ??通过按键 ctrl + 7 或者 以下方式调出,Profiler 窗口: 
- 一定要选择 Deep Profile(深度分析)模式
 ??接下来写完脚本,开始运行后,打开如下下拉框即可看到代码中详细的运行情况:  ??不过要注意:进行深度性能分析时会造成项目运行帧率严重下降,若要暂时关闭性能分析请按下红色箭头指向的按钮(即暂停按钮⊙ ) 
一、关于 int3 与 Vector3Int 的问题
1. Vector3Int 是什么?
Vector3Int 是 使用整数的 3D 向量和点表示形式 此结构用于在某些情况下表示不需要浮点精度的 3D 位置和向量。
通俗易懂地来说,Vector3Int 就是 一个三维的向量或点:
n
?
=
(
x
,
y
,
z
)
?
或
?
P
(
x
,
y
,
z
)
\vec n=(x,y,z) \ 或\ P(x,y,z)
n
=(x,y,z)?或?P(x,y,z)
其中
x , y , z 都是整数。
更详细的见 Unity脚本API: Vector3Int .
特点如下:
- Vector3Int 实现了
IEquatable<int3> , IFormattable 接口。 - 其全部成员属性如下:
public static Vector3Int right { get; }
public static Vector3Int left { get; }
public static Vector3Int down { get; }
public static Vector3Int up { get; }
public static Vector3Int one { get; }
public static Vector3Int zero { get; }
public static Vector3Int forward { get; }
public static Vector3Int back { get; }
public int x { get; set; }
public int y { get; set; }
public int z { get; set; }
public float magnitude { get; }
public int sqrMagnitude { get; }
public static Vector3Int CeilToInt(Vector3 v);
public static Vector3Int FloorToInt(Vector3 v);
public static Vector3Int RoundToInt(Vector3 v);
public static float Distance(Vector3Int a, Vector3Int b);
public static Vector3Int Max(Vector3Int lhs, Vector3Int rhs);
public static Vector3Int Min(Vector3Int lhs, Vector3Int rhs);
public static Vector3Int Scale(Vector3Int a, Vector3Int b);
public void Clamp(Vector3Int min, Vector3Int max);
public void Scale(Vector3Int scale);
public void Set(int x, int y, int z);
2. int3 是什么?
严谨的来说,int3 是一个 1 × 3 矩阵,可以表示为:
A
=
[
?
x
???
y
???
z
?
]
\bold{A} = \begin{bmatrix}\ x\ \ \ y\ \ \ z\ \end{bmatrix}
A=[?x???y???z??]
其中
x , y , z 都是整数。
特点如下:
- int3 实现了
IEquatable<int3> , IFormattable 接口。 - 其全部成员属性如下:
public static readonly int3 zero;
public int x;
public int y;
public int z;
public int2 xx { get; }
public int3 yzx { get; set; }
public int4 zzzz { get; }
....
加 | 减 | 乘 | 除 | 求余 | 自增 | 自减 | 与 | 或 | 非 | 异或 | 左移 | 右移 |
---|
+ | - | * | / | % | ++ | -- | & | | | ~ | ^ | << | >> |
3. 两者有什么异同?
<共同点>
- 1.都实现了
IEquatable<int3> , IFormattable 接口; - 2.两者在数据储存方面是一样的,都是储存三个 System.Int32 类型的数据;
- 3.都可以通过索引器访问内部成员;
- 4.都能进行基础的加减乘除运算;
<不同点>
- 1.
int3 可以进行的运算操作比Vector3Int 丰富; - 2.
int3 可以使用的静态成员比Vector3Int 少; - 3.
int3 可以通过多种方式进行初始化操作,而Vector3Int 只能通过输入 x , y , z 进行初始化;
<适用范围>
int3 适用于矩阵运算当中;Vector3Int 适用于向量和点的运算当中。
二、正式测试部分
新建一个脚本,写入如下代码:
using UnityEngine;
using Unity.Mathematics;
public class TestTimerScript : MonoBehaviour
{
public int count = 10_000;
void Update()
{
Test_int3_1();
Test_Vector3Int_1();
Test_int3_2();
Test_Vector3Int_2();
Test_int3_3();
Test_Vector3Int_3();
Test_int3_4();
Test_Vector3Int_4();
UnityEditor.EditorApplication.isPaused = true;
}
public int3 testNum;
public Vector3Int testVec;
public void Test_int3_1()
{
for (int i = 0; i < count; ++i)
{
testNum = new int3(1, 2, 2);
}
}
public void Test_Vector3Int_1()
{
for (int i = 0; i < count; ++i)
{
testVec = new Vector3Int(1, 2, 2);
}
}
public void Test_int3_2()
{
for (int i = 0; i < count; ++i)
{
int3 testNum = new int3(1, 2, 2);
}
}
public void Test_Vector3Int_2()
{
for (int i = 0; i < count; ++i)
{
Vector3Int testVec = new Vector3Int(1, 2, 2);
}
}
public void Test_int3_3()
{
for (int i = 0; i < count; ++i)
{
testNum = int3.zero;
}
}
public void Test_Vector3Int_3()
{
for (int i = 0; i < count; ++i)
{
testVec = Vector3Int.zero;
}
}
public void Test_int3_4()
{
for (int i = 0; i < count; ++i)
{
testNum += int3.zero;
}
}
public void Test_Vector3Int_4()
{
for (int i = 0; i < count; ++i)
{
testVec += Vector3Int.zero;
}
}
}
运行代码后:  可以发现:
- 1.
int3 和 Vector3Int 的赋值和初始化的时间消耗没有太大的区别; - 2.
int3 的运算时间消耗是 Vector3Int 的一半,说明用 int3 进行三个数的运算会比 Vector3Int 快不少;
为了进一步比较出 int3 和 Vector3Int 之间的区别,对代码进行如下修改:
using UnityEngine;
using Unity.Mathematics;
public class TestTimerScript : MonoBehaviour
{
public int count = 10_000;
void Update()
{
Test_int3_1();
Test_Vector3Int_1();
Test_int3_2();
Test_Vector3Int_2();
Test_int3_3();
Test_Vector3Int_3();
Test_int3_4();
Test_Vector3Int_4();
UnityEditor.EditorApplication.isPaused = true;
}
public int3 testNum;
public Vector3Int testVec;
public int num;
public void Test_int3_1()
{
for (int i = 0; i < count; ++i)
{
testNum = new int3(1, 2, 2);
}
}
public void Test_Vector3Int_1()
{
for (int i = 0; i < count; ++i)
{
testVec = new Vector3Int(1, 2, 2);
}
}
public void Test_int3_2()
{
for (int i = 0; i < count; ++i)
{
testNum += new int3(4, 4, 3);
}
}
public void Test_Vector3Int_2()
{
for (int i = 0; i < count; ++i)
{
testVec += new Vector3Int(4, 4, 3);
}
}
public void Test_int3_3()
{
for (int i = 0; i < count; ++i)
{
num = testNum.x;
}
}
public void Test_Vector3Int_3()
{
for (int i = 0; i < count; ++i)
{
num = testVec.x;
}
}
public void Test_int3_4()
{
for (int i = 0; i < count; ++i)
{
testNum = new int3(testVec.x, testVec.y, testVec.z);
}
}
public void Test_Vector3Int_4()
{
for (int i = 0; i < count; ++i)
{
testVec = new Vector3Int(testNum.x, testNum.y, testNum.z);
}
}
}
继续测试代码: 可以发现:
- 1.
int3 和 Vector3Int 获取成员的时间消耗都是一样的; - 2.获取
Vector3Int 成员并转化为 int3 类型的时间消耗是获取 int3 成员并转化为 Vector3Int 类型的4倍之多;
可以得出结论:
用 int3 进行简单的初始化、赋值和运算操作的时间消耗相比于 Vector3Int 是快一点,并且转化为 Vector3Int 的时间消耗也不大,可以先用 int3 操作完后再转化为 Vector3Int ; 如果经常使用向量运算,还是不要用 int3 ,int3 可以作为一个辅助工具来使用。
新人博主,请大家多多光照~~如果有什么不正确或不足的地方请在评论区积极指出哟,一起学习一起进步~
|