The following code gave me the results below:
isinst Total: 00:34:42.5468750
castclass Total: 00:35:05.6875000
Total Difference: 00:00:23.1406250

It appears that "as" and then testing for null is a little bit quicker, but
the results could be an anomaly and may not be reproducible on someone
else's machine.  There are a million other factors that could lead one to
readily dismiss my results as inconclusive (as Ian has pointed out in the
past).  For now though, I think I'm going to stick with "as" and test for
null.

I've included the code that I used.  Please feel free to help me improve it.


using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.CompilerServices;

#endregion

namespace ConsoleApplication1
{
    class Program
    {
        [MethodImpl(MethodImplOptions.NoInlining)]
        static void Main(string[] args)
        {
            Collection<TimeSpan> isinstTestResults = new
Collection<TimeSpan>();
            Collection<TimeSpan> castclassTestResults = new
Collection<TimeSpan>();

            long length = 100000000;
            TimeSpan timeSpan = new TimeSpan(0);
            DateTime startDateTime = DateTime.Now;

            for (int counter = 0; counter < 100; counter++)
            {
                startDateTime = DateTime.Now;
                for (long i = 0; i < length; i++)
                {
                    Base b = new Derived();

                    Derived d = b as Derived;

                    if (b != null)
                    {
                        Console.Write("");
                    }
                    else
                    {
                        throw new InvalidCastException();
                    }
                }

                timeSpan = DateTime.Now - startDateTime;
                isinstTestResults.Add(timeSpan);
                Console.WriteLine("isinst test {0}      --> {1}", counter,
timeSpan);

                startDateTime = DateTime.Now;
                for (long i = 0; i < length; i++)
                {
                    Base b = new Derived();

                    Derived d = (Derived)b;

                    Console.Write("");
                }

                timeSpan = DateTime.Now - startDateTime;
                castclassTestResults.Add(timeSpan);
                Console.WriteLine("castclass test {0}   --> {1}", counter,
timeSpan);
            }

            TimeSpan isinstTotal = new TimeSpan(0);
            TimeSpan castclassTotal = new TimeSpan(0);
            for (int i = 0; i < isinstTestResults.Count; i++)
            {
                isinstTotal += isinstTestResults[i];
                castclassTotal += castclassTestResults[i];
            }

            Console.WriteLine("isinst Total: {0}", isinstTotal);
            Console.WriteLine("castclass Total: {0}", castclassTotal);

            if (isinstTotal < castclassTotal)
            {
                Console.WriteLine("Total Difference: {0}", castclassTotal -
isinstTotal);
            }
            else
            {
                Console.WriteLine("Total Difference: {0}", isinstTotal -
castclassTotal);
            }
        }
    }

    public class Base
    {
    }

    public class Derived : Base
    {
    }
}


Thanks,
Fred

> -----Original Message-----
> From: Unmoderated discussion of advanced .NET topics. [mailto:ADVANCED-
> [EMAIL PROTECTED] On Behalf Of Ted Neward
> Sent: Sunday, February 20, 2005 3:29 AM
> To: [email protected]
> Subject: Re: [ADVANCED-DOTNET] IL internals (castclass and isinst)
>
> Have you tried running a benchmark? Try each 100k times and see what the
> timing differences are?
>
> From looking at the Rotor code, it looks like they're both basically
> implemented in the same terms; that is, they both end up consulting the
> object's MethodTable to see if there's correlation and/or goodness.
> Assuming the CLR itself isn't too terribly different from Rotor on this
> score, you can see the gory details in sscli\clr\src\vm\jitinterface.cpp;
> do a search for "IsInst" and "ChkClass", as there are a couple of helpers
> that get invoked during the process.
>
> Ted Neward
> Co-Author, SSCLI Essentials
> http://www.neward.net/ted
>
> ---------- Original Message ----------------------------------
> From:         Palmer Fred <[EMAIL PROTECTED]>
> Reply-To:     "Unmoderated discussion of advanced .NET topics."
> ===================================
> This list is hosted by DevelopMentorR  http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

===================================
This list is hosted by DevelopMentor�  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to