tags:

views:

209

answers:

8

Hi,

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

    string str = "12345";
    int j = 12345;
    if (str == j.ToString())
    {
        //do my logic
    }

I was thinking that the above stuff can also be done with:

    string str = "12345";
    int j = 12345;
    if (Convert.ToInt32(str) == j)
    {
        //do my logic
    }

So i developed a sample code to test in terms of performance which one is better

        var iterationCount = 1000000;
        var watch = new Stopwatch();
        watch.Start();
        string str = "12345";
        int j = 12345;
        for (var i = 0; i < iterationCount; i++)
        {
            if (str == j.ToString())
            {
                //do my logic
            }
        }
        watch.Stop();

And second one:

  var iterationCount = 1000000;
    var watch = new Stopwatch();
    watch.Start();
    string str = "12345";
    int j = 12345;
    for (var i = 0; i < iterationCount; i++)
    {
        if (Convert.ToInt32(str) == j)
        {
            //do my logic
        }
    }
    watch.Stop();

On running the above two tests i found the above tests were giving nearly the same time elapsed. I would like to discuss which one is the better approach? And is there any other approach better than two above two?

+1  A: 

If performance is near identical, go with the version that is more readable.

Personally, I find the .ToString() approach to be easier to understand and less prone to possible casting issues that the other approach has.

Oded
+5  A: 

I prefer i.ToString() == str since nothing guarantees that Convert.ToInt32(str) doesn't fail.

Mehrdad Afshari
If "000123" != 123, this is definitely better.
Reed Copsey
+2  A: 

Yikes, you show your domain logic as being inside your profiling loop so you weren't testing the time difference between the Convert and ToString versions of your code; you were testing the combined time of the conversion plus the execution of your business logic. If your business logic is slow and dominates the conversion time, of course you will see the same time in each version.

Now, with that out of the way, even worrying about this until you know that it's a performance bottleneck is premature optimization. In particular, if executing your domain logic dominates the conversion time, the difference between the two will never matter. So, choose the one that is most readable.

Now, as for which version to use: You need to start by specifying exactly what you're testing. What are your inputs? Is "007" ever going to be an input? Is "007" different than the integer 7? Is "1,024" ever going to be an input? Are there localization concerns?

Jason
I did not test with domain logic inside my profile, it was just comment written inside it, as i have shown.
Raghav Khunger
You show `//do my logic`. Everyone will interpret that as you are doing your logic but don't want to give us the details of it, not that you commented out the logic. To make it clear, you'd be better off writing the test as `bool b = Convert.ToInt32(str) == j;` etc.
Jason
@Raghav Khunger: Further, even if you did test the performance of the two methods without executing your domain logic, my point still stands: do not worry about this until you know it's a performance bottleneck. Instead, specify fully what you are trying to implement and write the most readable maintainable code that does the job.
Jason
A: 

Well for starters the first one which converts the int to a string won't throw an error if the string the int is being compared to isn't convertible to an int.

If you are doing a lot of tests in a batch converting to a string will remove the problem of potentially having exceptions throw, because of conversion errors. Raising exceptions takes time and would slow down the second test.

Kevin
+11  A: 

Well - performance should not be the only thing that matters.

You should ask whether you want to compare the actual value or only the representation of the number.

Take the following example: Does "00001" equal 1? If you want it to convert the string to int using Int.TryParse in combination and then compare them.

There might be other differences, too depending of the local settings. Maybe the user has set to format numbers like "1,000,000" - if you'd compare that string to 1000000.ToString() the result would be false.

winSharp93
A: 

Semantics are a little different. "01" == 1.ToString() is false, 1 == Convert.ToInt32("01") is true.

If the parsing can go wrong (The string isn't a valid number) than Int32.TryParse is faster than to use Convert.ToInt32().

helium
A: 

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 ??

Raghav Khunger
+1  A: 

Your test is fundamentally flawed. The compiler and run-time are really clever beasts and will optimise the code both at compile time and run time (JIT-ing). In this case, you are doing the same thing every time which will be spotted by the compiler and optimised out, hence the timing will be similar for each method.

Try this version (I've only got .Net 2.0, hence the slight changes):

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace ToStringTest
{
    class Program
    {
        const int
            iterationCount = 1000000;

        static TimeSpan Test1()
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            string str = "12345";
            int j = 12345;
            for (int i = 0; i < iterationCount; i++)
            {
                if (str == i.ToString())
                {
                    //do my logic
                }
            }
            watch.Stop();
            return watch.Elapsed;
        }

        static TimeSpan Test2()
        {
            Stopwatch watch = new Stopwatch();
            watch.Start();
            string str = "12345";
            int j = 12345;
            for (int i = 0; i < iterationCount; i++)
            {
                if (Convert.ToInt32(i) == j)
                {
                    //do my logic
                }
            }
            watch.Stop();
            return watch.Elapsed;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("ToString = " + Test1().TotalMilliseconds);
            Console.WriteLine("Convert = " + Test2().TotalMilliseconds);
        }
    }
}

and you will see a huge difference. One is two orders of magnitude faster than the other. And it really is obvious which one it is.

You need to know what the various operations are doing in order to know which is fundamentally faster.

Converting a string to an int requires the following:

total = 0
for each character in string
  total = total * 10 + value of charater

and the ToString requires:

string = ""
while value != 0
  string.AddToFront value % 10
  value /= 10

Multiplication is far easier, and faster, for a CPU to do than division. Given the choice of an algorithm with lots of multiplies versus an algorithm with lots of divides, always go for the former as it will always be faster.

Then there's the comparison, an int - int comparison is simple, load each value into a register and compare - a couple of machine instructions and you're done. A comparison between two strings requires testing each character in the strings one at a time - in the example you gave it was 5 bytes (an int is probably 4 bytes) which is more memory accesses.

Skizz