Some script changes which might help you write more performance code.
Speed comparison:
Code:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Profiling;
using System.Linq;
public class Benchmark : MonoBehaviour
{
int count = 1_000_000;
string var1 = "abcE";
List<int> var3;
int[] var4;
// Start is called before the first frame update
void Start()
{
var3 = Enumerable.Range(0, 1000).ToList();
var4 = Enumerable.Range(0, 5).ToArray();
Profiler.BeginSample("1 SetTransformDouble");
for (int i = 0; i < count; i++)
{
transform.position = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100));
transform.rotation = Random.rotation;
}
Profiler.EndSample();
Profiler.BeginSample("1 SetTransformCombined");
for (int i = 0; i < count; i++)
{
transform.SetPositionAndRotation(new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100)), Random.rotation);
}
Profiler.EndSample();
Profiler.BeginSample("1 DoubleTransformLocal");
for (int i = 0; i < count; i++)
{
transform.localPosition = new Vector3(Random.Range(-100, 100), Random.Range(-100, 100), Random.Range(-100, 100));
transform.localRotation = Random.rotation;
}
Profiler.EndSample();
AudioSource mf;
bool temp = false;
Profiler.BeginSample("2 tryGetComponentFalse");
for (int i = 0; i < count; i++)
{
if (TryGetComponent<AudioSource>(out mf))
{
temp = true;
}
}
Profiler.EndSample();
Profiler.BeginSample("2 GetComponentFalse");
for (int i = 0; i < count; i++)
{
mf = GetComponent<AudioSource>();
if (mf != null)
{
temp = true;
}
}
Profiler.EndSample();
gameObject.AddComponent<AudioSource>();
Profiler.BeginSample("2 tryGetTrue");
for (int i = 0; i < count; i++)
{
if (TryGetComponent<AudioSource>(out mf))
{
temp = true;
}
}
Profiler.EndSample();
Profiler.BeginSample("2 GetComponentTrue");
for (int i = 0; i < count; i++)
{
mf = GetComponent<AudioSource>();
if (mf != null)
{
temp = true;
}
}
Profiler.EndSample();
Profiler.BeginSample("3 string: ==");
for (int i = 0; i < count; i++)
{
if (var1 == string.Empty)
{
}
}
Profiler.EndSample();
Profiler.BeginSample("3 string: == blank");
for (int i = 0; i < count; i++)
{
if (var1 == "")
{
}
}
Profiler.EndSample();
Profiler.BeginSample("3 string .isnullorempty");
for (int i = 0; i < count; i++)
{
if (string.IsNullOrEmpty(var1))
{
}
}
Profiler.EndSample();
Profiler.BeginSample("3 string .length null check");
for (int i = 0; i < count; i++)
{
if (var1 != null && var1.Length == 0)
{
}
}
Profiler.EndSample();
Profiler.BeginSample("3 string ?.lenght");
for (int i = 0; i < count; i++)
{
if (var1?.Length == 0)
{
}
}
Profiler.EndSample();
Profiler.BeginSample("3 string .lenght");
for (int i = 0; i < count; i++)
{
if(var1.Length == 0)
{
}
}
Profiler.EndSample();
Profiler.BeginSample("4 native count");
for (int i = 0; i < count; i++)
{
int count = var3.Count;
}
Profiler.EndSample();
Profiler.BeginSample("4 LINQ count");
for (int i = 0; i < count; i++)
{
int count = var3.Count();
}
Profiler.EndSample();
Profiler.BeginSample("5 AddRange");
for (int i = 0; i < count; i++)
{
var3.AddRange(var4);
}
Profiler.EndSample();
Profiler.BeginSample("5 Add looped");
for (int i = 0; i < count; i++)
{
for (int j = 0; j < var4.Length; j++)
{
var3.Add(var4[j]);
}
}
Profiler.EndSample();
System.GC.Collect();
}
}
Feel free to add to discuss in replies