[Unity] Performance Test – String Concat



Overview

Recommended to use string.Concat() or directly use + operator for better performance.

Test times: 10,000,000
1. PlusOperator: 6825
2. Interpolation: 10037
3. StringFormat: 9940
4. StringConcat: 6822
5. StringJoin: 8255
6. StringBuilder: 7157

[Unity] C# string concat performance

Testing Functions
        private void PlusOperator()
        {
            for (int i = 0; i < m_testTimes; i++)
            {
                var concat = "testing" + i;
            }
        }

        private void Interpolation()
        {
            for (int i = 0; i < m_testTimes; i++)
            {
                var concat = $"testing{i}";
            }
        }

        private void StringFormat()
        {
            for (int i = 0; i < m_testTimes; i++)
            {
                var concat = string.Format("testing{0}", i);
            }
        }

        private void StringConcat()
        {
            for (int i = 0; i < m_testTimes; i++)
            {
                var concat = string.Concat("testing", i);
            }
        }

        private void StringJoin()
        {
            for (int i = 0; i < m_testTimes; i++)
            {
                var concat = string.Join("testing", i);
            }
        }

        private void StringBuilder()
        {
            for (int i = 0; i < m_testTimes; i++)
            {
                var builder = new StringBuilder("testing");
                builder.Append(i);
            }
        }


this is default footer data
歡迎您留言與分享!(Welcome for comments or sharing!)

Related Post:

Tagged on: ,