Which is fast comparison: Convert.ToInt32(stringValue)==intValue or stringValue==intValue.ToString()

后端 未结 8 2058
执念已碎
执念已碎 2021-02-13 18:34

While developing my application i came across some comparison stuff here was it:

    string str = \"12345\";
    int j = 12345;
    if (str == j.ToString())
             


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-13 19:02

    Okay, i further move one step ahead and test in this way:

        int j = 123;
        for (var i = 0; i < iterationCount; i++)
        {
            j.ToString();
        }
    

    second one: string str = "123";

            for (var i = 0; i < iterationCount; i++)
            {
                Convert.ToInt32(str);
            }
    

    In this case i found every time second one is performing a little better. In my case the number would be like 100000 only and not in the form of 100,000. Your comments on this test that i did in this post ??

提交回复
热议问题