Is comparing ints and ints or strings and strings more efficient

I have a program written in C # where there are many comparisons between ints and strings.

So, for performance reasons, I just wanted to know which is more efficient?

If we have:

int a  = 5;
string b = "5";

if(a == int.Parse(b)) { }

OR

if(a.ToString() == b) { }
+5
source share
11 answers

A few points that are mentioned in the article use the profiling tool to prove that it has the best performance.

This is normal, but the easiest way to test the performance of specific operators is to loop them and use the stopwatch class.

, . .

:

    System.Diagnostics.Stopwatch sw=new System.Diagnostics.Stopwatch();


    int a  = 5;
    string b = "5";

    sw.Start();

    for (int i=0;i<1000000;i++)
    {
        if(a == int.Parse(b))
        {

        } 
    }

    sw.Stop();

    Console.WriteLine("a == int.Parse(b) milliseconds: " + sw.ElapsedMilliseconds);

    sw.Reset();

    sw.Start();

    for (int i=0;i<1000000;i++)
    {
        if(a.ToString() == b)
        {

        }       
    }       

    sw.Stop();

    Console.WriteLine("a.ToString() == b milliseconds: " + sw.ElapsedMilliseconds);

:

a == int.Parse(b) : 521

a.ToString() == b : 697

, int.Parse() , , .

+4

. , , ToString . , , , , , , , , .

: , , 'n' :

using System;
using System.Diagnostics;

namespace CompareTest
{
    static class Program
    {
        static void Main(string[] args)
        {
            int iterations = 10000000;
            int a = 5;
            string b = "5";

            Stopwatch toStringStopwatch = new Stopwatch();
            toStringStopwatch.Start();

            for (int i = 0; i < iterations; i++) {
                bool dummyState = a.ToString() == b;
            }

            toStringStopwatch.Stop();

            Stopwatch parseStopwatch = new Stopwatch();
            parseStopwatch.Start();

            for (int i = 0; i < iterations; i++) {
                bool dummyState = a == int.Parse(b);
            }

            parseStopwatch.Stop();

            Console.WriteLine("ToString(): {0}", toStringStopwatch.Elapsed);
            Console.WriteLine("Parse(): {0}", parseStopwatch.Elapsed);
            Console.ReadLine();
        }
    }
}
+7


A

int a = 5;
string b = "5";
//Assuming these two values are input received by the application at runtime
int bInt;
if (int.TryParse(b, NumberStyles.None, CultureInfo.InvariantCulture, out bInt) 
    && a.Equals(bInt))
{

}

int a = 5;
string b = "5"; 
//Assuming these two values are input received by the application at runtime
if (string.Compare(b, a.ToString(), StringComparison.Ordinal) != -1)
{

}

( ) , A A . B !

A if(a == int.Parse(b))

+4

, ToString Parse :

value = 0
for each char in string
  value = value * 10 + valueof(char) // i.e. '0' -> 0, '7' -> 7

ToString

string=""
while value > 0
  string.insert_at_front value % 10 // so that 0 -> '0' and 6 -> '6'
  value /= 10

// on IA32, the % and / can be done at the same time but requires
// a 64bit source for 32bit values

ToString , Parse, , . - , Parse ToString (.. , ), , , .

, , , .

+3

, .

  • - , (, IM), (, ).

  • , : " ? -?" ( ), "... int * [sic] * ", , (, )? , int , ?

+2

, , , , "5" == 5 , 0 9, , : (int)b[0] == 53 "5" (b [0]) ACSII, 53 . : a == int.Parse(b) milliseconds: 194 a.ToString() == b milliseconds: 142 a == (int)(b[0]) milliseconds: 8 , , :

: . , . , , ASCII, : switch((int)b[0]) { case 48: Console.WriteLine("0"); break; case 49: Console.WriteLine("1"); break; case 50: Console.WriteLine("2"); break; case 51: Console.WriteLine("3"); break; case 52: Console.WriteLine("4"); break; case 53: Console.WriteLine("5"); break; case 54: Console.WriteLine("6"); break; case 55: Console.WriteLine("7"); break; case 56: Console.WriteLine("8"); break; case 57: Console.WriteLine("9"); break; } , , : a == int.Parse(b) milliseconds: 184 a.ToString() == b milliseconds: 135 a + 48 ==(int)b[0] milliseconds: 8 , , .

+2

, . . b , , . . .

+1

, , - .

, int.ToString() , int.Parse().

int.ToString() CLR (comnumber). int.Parse() BCL, Number.ParseInt32()Number.StringToNumber()Number.ParseNumber().

ParseNumber , , , int.ToString() . , StopWatch. : , .

++, CLR ToString() :

  • NumberToString ( ToString() ), FCIMPL3, int.ToString() extern.
  • Int32ToDecStr "D".

#

var x = 5.ToString("D");
var y = 5.ToString();

FCIMPL3, , , .

+1

Int32 . , Int32.Parse . '=='. .Equals(), .

if(b.Equals(a))
{

}
0

somwhere (MSDN), , , ==

StringA.ToUpperInvariant() == StringB.ToUpperInvariant()
0

, ...

0
source

All Articles