Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it more efficient to compare ints and ints or strings and strings

Tags:

performance

c#

I've got a program written in c# where there are a lot of comparisons between ints and strings.

So for performance reasons, I would just like 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) { }
like image 889
The_Butcher Avatar asked Oct 29 '09 10:10

The_Butcher


2 Answers

I actually profiled this using a few examples and timed loops. It turns out Parse wins for small integers, and ToString wins for large ones. This difference is so small it should not be a concern however, as others have mentioned, you're likely to make a better choice by thinking about the cases where the string does not represent an integer at all.

Edit: For those interested, here's the source, quick 'n' dirty:

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();
        }
    }
}
like image 150
SoftMemes Avatar answered Nov 10 '22 02:11

SoftMemes


A few comments mentioned running a profiling tool to prove which has better performance.

This is a ok, but the simplest way to check performance of specific statements is to put them in a loop and use the Stopwatch class.

Jeff Atwood asked about making this sort of timing even simpler in this question. In that question and answer you will also find some good code examples and background details.

Heres a very simple working example:

    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);

On my computer it outputs:

a == int.Parse(b) milliseconds: 521

a.ToString() == b milliseconds: 697

So in this simple scenario int.Parse() is slightly faster, but not enough to really worry about.

like image 20
Ash Avatar answered Nov 10 '22 01:11

Ash