Amazon.com Widgets

Pref: Convert.ToString() or Int32.ToString()

A reader recently asked me if there is any perf difference between Convert.ToString() or Int32.ToString(). 

 

If you search for Convert.cs you will find the Rotor source for Convert.cs which helps answer the question…

 

02066         public static string ToString(int value) {
02067             return value.ToString();
02068         }

 

As you can see, all Convert.ToString() does it call value.ToString().  The call is simple enough that it is likely to get inlined.  So I can’t imagine their being any noticeable differences.

 

But we should of course, measure… so on a very noise laptop running the latest Whidbey CTP I ran this test:

 

ToString Test: 00:00:16.0299278

Convert Test: 00:00:16.4424654

Press any key to continue . . .

 

As you can see, there is almost no difference between these two.  So net: you should use which ever one feels more natural to you in your project.  

 

 

Here is the code if you want to play with it:

 

using System;

using System.Collections.Generic;

using System.Diagnostics;

 

class Program

{

    static int iterations = 50000000;

    static void Main(string[] args)

    {

        //do one round just to worm up...

        for (int i = 0; i < iterations; i++)

        {

            i.ToString();

            Convert.ToString(i);

        }

 

        Stopwatch sw = new Stopwatch();

        //Do the ToString() test

        sw.Start();

        for (int i = 0; i < iterations; i++)

        {

            i.ToString();

        }

 

        sw.Stop();

        Console.WriteLine("ToString Test: {0}", sw.Elapsed);

        sw.Reset();

 

        //Do the Convert() test

        sw.Start();

        for (int i = 0; i < iterations; i++)

        {

            Convert.ToString(i);

        }

        sw.Stop();

        Console.WriteLine("Convert Test: {0}", sw.Elapsed);

    }

}

 

 

update: just fixed some formatting

Published 10 March 05 10:34 by BradA
Filed under:

Comments

# T H said on March 10, 2005 11:10 AM:
that's totally off.
Convert class is doing null checking, so it is much safe to use Convert class.
# Jer said on March 10, 2005 11:31 AM:
T.H.

The only type that Convert.ToString() takes that can even be null is object. So for Brad's example there is no null checking as value types cannot be null anyways.
# Kristoffer Henriksson said on March 10, 2005 11:32 AM:
Ints can't be null. Are you thinking of something else?
# James Hancock said on March 10, 2005 11:44 AM:
He's thinking database cells converted I'm sure.... which of course is from object and will get a null check.
# Brad Abrams [MSFT] said on March 11, 2005 12:42 PM:
Looks like a couple of comments got lost... adding them back here:

# re: Pref: Convert.ToString() or Int32.ToString() 3/11/2005 2:17 AM Zohar
what can possibly be a good reason for having this code in the framework?

public static string ToString(int value) {
return value.ToString();
}




# re: Pref: Convert.ToString() or Int32.ToString() 3/11/2005 6:22 AM Steve
So people can have a one stop shop to do all of their type conversions. Less confusion.
# Leonardo Constantino said on March 12, 2005 6:25 AM:
I've always thought Rotor was a reference implementation, right? So how can we make assumptions about Microsoft's implementation based on Rotor's?

TIA
# Brad Abrams [MSFT] said on March 12, 2005 8:07 AM:
With the exception of the JIT and the GC, ROTOR is built off the identical sources we use to build the product.
New Comments to this post are disabled

Search

Go

This Blog

Syndication

Page view tracker