[Unity] Performance Test – String Comparison

Recommended to use string.Equals() for better performance.

Overview

Recommended to use string.Equals() for better performance.
Test times: 10,000,000
1. EqualityOperator: 464
2. StringEquals: 406
3. StringEqualsByIgnoreCase: 13557
4. AnimatorHash: 1614

Testing Functions
private void EqualityOperator()
{
for (int i = 0; i < m_testTimes; i++)
{
if (m_lString == m_rString)
{
}
}
}

private void StringEquals()
{
for (int i = 0; i < m_testTimes; i++)
{
if (string.Equals(m_lString, m_rString))
{
}
}
}

private void StringEqualsByIgnoreCase()
{
for (int i = 0; i < m_testTimes; i++)
{
if (string.Equals(m_lString, m_rString, StringComparison.CurrentCultureIgnoreCase))
{
}
}
}

private void AnimatorHash()
{
for (int i = 0; i < m_testTimes; i++)
{
var lHash = Animator.StringToHash(m_lString);
var rHash = Animator.StringToHash(m_rString);
if (lHash == rHash)
{
}
}
}

發佈留言

AllEscort