Is there a cost to casting?

Last post 09-30-2008 2:59 AM by Svante. 6 replies.

Sort Posts:

  • Is there a cost to casting?

    09-29-2008, 7:02 AM
    • Member
      37 point Member
    • maxp
    • Member since 04-19-2007, 11:30 AM
    • Posts 50

    Only taking into account performance, is there a performance downside to using this:

    1            public static object GetDataset()
    2            {
    3                DataSet ds=null;
    4                ///....dataset code
    5                return (object)ds;
    6            }
    7            protected void AssignDataset()
    8            {
    9                DataSet ds = (DataSet)GetDataset();
    10          }
    
     Instead of this: 
    1    public static DataSet GetDataset()
    2            {
    3                DataSet ds = null;
    4                ///....dataset code
    5                return ds;
    6            }
    7            protected void AssignDataset()
    8            {
    9                DataSet ds = GetDataset();
    10          }
    11   
    
    Thanks

      
    Filed under:
  • Re: Is there a cost to casting?

    09-29-2008, 7:22 AM
    Answer
    • All-Star
      90,127 point All-Star
    • SGWellens
    • Member since 01-02-2007, 9:27 PM
    • Twin Cities, MN
    • Posts 7,355
    • Moderator
      TrustedFriends-MVPs

    Yes, there is a cost.  I modified your source code a bit:

            static void Main(string[] args)
            {
                DataSet MyDataSet1;
                DataSet MyDataSet2;
    
                MyDataSet1 = (DataSet)GetDataset1();
                MyDataSet2 = GetDataset2();
            }
    
            public static object GetDataset1()
            {
                DataSet ds = null;
                ///....dataset code
                return (object)ds;
            }
            public static DataSet GetDataset2()
            {
                DataSet ds = null;
                ///....dataset code
                return ds;
            }
     The IL code for the two functions is the same but there is one extra line in the calling function:
    .method private hidebysig static void  Main(string[] args) cil managed
    {
      .entrypoint
      // Code size       19 (0x13)
      .maxstack  1
      .locals init ([0] class [System.Data]System.Data.DataSet MyDataSet1,
               [1] class [System.Data]System.Data.DataSet MyDataSet2)
      IL_0000:  nop
      IL_0001:  call       object ConsoleApplication1.Program::GetDataset1()
      IL_0006:  castclass  [System.Data]System.Data.DataSet
      IL_000b:  stloc.0
      IL_000c:  call       class [System.Data]System.Data.DataSet ConsoleApplication1.Program::GetDataset2()
      IL_0011:  stloc.1
      IL_0012:  ret
    } // end of method Program::Main
     
    Steve Wellens

    My blog
  • Re: Is there a cost to casting?

    09-29-2008, 7:23 AM
    • Member
      430 point Member
    • morpheus_24
    • Member since 10-15-2007, 5:49 AM
    • Pune
    • Posts 90

     Hi,

    Yes,  performance downside is there in code snippet 1 because of boxing and unboxing.

    Please go through the link mention below:

    http://msdn.microsoft.com/en-us/library/ms173196(VS.80).aspx


  • Re: Is there a cost to casting?

    09-29-2008, 7:57 AM
    • Star
      14,342 point Star
    • booler
    • Member since 08-15-2005, 2:22 PM
    • Brighton, England
    • Posts 2,205

    morpheus_24:

     Hi,

    Yes,  performance downside is there in code snippet 1 because of boxing and unboxing.

    Please go through the link mention below:

    http://msdn.microsoft.com/en-us/library/ms173196(VS.80).aspx

     

    Sorry to be pedantic- but there is no boxing/unboxing in the code posted. This occurs when you convert a value type to a reference type and vice versa- not when casting one reference type to another.

  • Re: Is there a cost to casting?

    09-29-2008, 7:57 AM
    • Member
      684 point Member
    • naveen_j80
    • Member since 05-11-2005, 7:44 AM
    • Hyderabad, India
    • Posts 117

    Hi,

     Casting dataset to object definitely decreases the performance. Please look at the url http://www.codeproject.com/KB/cs/csharpcasts.aspx that explains diagrammatical representation of performance information.

  • Re: Is there a cost to casting?

    09-29-2008, 8:07 AM
    • Member
      37 point Member
    • maxp
    • Member since 04-19-2007, 11:30 AM
    • Posts 50
    Yikes so there is no 'free' way out - even  
    DataSet ds = ReturnDataSetAsObject() as DataSet
      has its cost.
  • Re: Is there a cost to casting?

    09-30-2008, 2:59 AM
    Answer
    • All-Star
      18,335 point All-Star
    • Svante
    • Member since 02-12-2007, 7:15 AM
    • Stockholm, Sweden
    • Posts 2,298
    • Moderator

    maxp:
    Yikes so there is no 'free' way out
     

    Right. Think about it. C# is a 'safe' language. When you want to upcast an object to anything at all, the run-time must ascertain that the object really is an instance of whatever you're trying to upcast to, so it can access the object safely. There must be a run-time cost associated with determining this, it can't be done at compile time. If the test was done when actually accessing the object, the cost would be much, much higher, so it must be done when doing the assignment.

    C# is not C++ where the corresponding cast operator is a compile-time construct to tell the compiler to just change it's notion about what a bunch of bits represent. In C++ the cast operator doesn't change anything in run-time. That's also why in C++ you have a whole bunch of different cast operators for different needs, some with run-time costs.

    The conclusion is that you should not be returning an 'object' in the first place, and that's why there's a win-win with type safety in C#. Generics is one way to overcome some of the issues. Using interfaces and common base classes is another. Just stay away from 'object' as much as you can.

    Finally - does this really matter? The cost of a cast is, to say the least, rather small. Especially when comparing the cost of doing anything at all with a DataSet. It's not exactly a light-weight type... The discussion may be for academic purposes, but for real performance enhancement start looking for quicker wins.

    Svante
    AxCrypt - Free Open Source File Encryption & Online Password Manager - http://www.axantum.com
    [Disclaimer: Code snippets usually uncompiled, beware typos.]
    ______
    Don't forget to click "Mark as Answer" on the post(s) that helped you.
Page 1 of 1 (7 items)