[Unity] Performance Test – String Comparison



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)
        {
        }
    }
}



歡迎您留言與分享!(Welcome for comments or sharing!)

Related Post:

Tagged on: ,