Tech Off Thread

8 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Performance Optimization in C#

Back to Forum: Tech Off
  • User profile image
    gicio

    Hi to all C# developers,

    (This thread is posted to 2 forums: MSDN Forums - Visual C# General and to channel9.msdn.com - Techoff
    ... the reson is to get as much as possible support on this thread Smiley)


    With this thread I will give you some performance optimization in C#
    and get also some from you. I hope you will support this thread and write down you performence
    optimization in C#. I hope also to discusse some of the issue with you all.


    Here are some of my first optimizations from my site:

    1. Used always sealed classes when the class haven't any derived classes.
    Some additional infos from this point:

    The sealed modifier is primarily used to prevent unintended derivation,
    but it also enables certain run-time optimizations. In particular, because a sealed class is
    known to never have any derived classes, it is possible to transform virtual function member
    invocations on sealed class instances into non-virtual invocations.

    2. Do not loop in a foreach loop over DataRows use the Select() method of the DataTable.

    this code costs tooooo much:

                foreach(DataRow currentDataRow in currentDataTable.Rows)
                {
                    newDataTable.ImportRow(currentDataRow);
                }


    this code runs over 300 % faster (depends on rows count):

     

                DataRow[] allRows = currentDataTable.Select();

                foreach (DataRow currentDataRow in allRows)
                {
                    newDataTable.ImportRow(currentDataRow);
                }


    I hope this is helpfully for you Smiley


    Regards

  • User profile image
    Dr Herbie

    gicio wrote:
    
    this code runs over 300 % faster (depends on rows count):

     

                DataRow[] allRows = currentDataTable.Select();

                foreach (DataRow currentDataRow in allRows)
                {
                    newDataTable.ImportRow(currentDataRow);
                }



    That's actually pretty interesting, but it;s a shame that Select() doesn't return typed dataRows (which is how I prefer to work).

    Is there a (fast) way to get an array of typed data rows? 
    I don't have time to experiment at the moment (need to optimise my speed Smiley )


    Herbie

  • User profile image
    gicio

    Dr Herbie are you looking for something like this?:

    MyTypedDataRow[] allRows = currentMyTypedDataTable.Select() as MyTypedDataRow[];

    foreach (MyTypedDataRow currentDataRow in allRows)

    {

    newMyTypedDataTable.ImportRow(currentDataRow);

    }

  • User profile image
    Dr Herbie

    gicio wrote:
    Dr Herbie are you looking for something like this?:

    MyTypedDataRow[] allRows = currentMyTypedDataTable.Select() as MyTypedDataRow[];

    foreach (MyTypedDataRow currentDataRow in allRows)

    {

    newMyTypedDataTable.ImportRow(currentDataRow);

    }



    Yes, but what's the impact of the cast?  I don't know if there's anything special about casting arrays.

    I'll try and do some micro-benchmarking to see the impact when I can get the time.

    Herbie

  • User profile image
    JohnAskew

    gicio wrote:
    
    1. Used always sealed classes when the class haven't any derived classes.

    2. Do not loop in a foreach loop over DataRows use the Select() method of the DataTable.




    1. I've heard better coders than me complain about sealed classes. How can anyone 'insure' consumers will never desire a derivative?

    2. Is this optimization particular to iteration with foreach or is it the 'ImportRow()' method? Have you tried your 300% test with a different method?


    Good info...

  • User profile image
    Dr Herbie

    JohnAskew wrote:
    
    gicio wrote: 
    1. Used always sealed classes when the class haven't any derived classes.

    2. Do not loop in a foreach loop over DataRows use the Select() method of the DataTable.




    1. I've heard better coders than me complain about sealed classes. How can anyone 'insure' consumers will never desire a derivative?



    Hmmm.  Architecture issue.  Fowler blogged about it recently. I haven't made my mind up which I prefer.

    Herbie

  • User profile image
    gicio

    JohnAskew wrote:
    

    1. I've heard better coders than me complain about sealed classes. How can anyone 'insure' consumers will never desire a derivative?



    You are right. But you should make it sealed when YOU have control over the class. Thats mean until none will inherited from your class you should make it sealed.



    JohnAskew wrote:
    

    2. Is this optimization particular to iteration with foreach or is it the 'ImportRow()' method? Have you tried your 300% test with a different method?



    The optimization is only for the foreach... just ignor the line with the 'ImportRow()' method.

    A loop over a 'lite object' like an Array is much more faster as a loop over a 'heavy object' like a DataRowCollection.


    Regards

  • User profile image
    amotif

    gicio wrote:
    

    1. Used always sealed classes when the class haven't any derived classes.
    Some additional infos from this point:

    The sealed modifier is primarily used to prevent unintended derivation,
    but it also enables certain run-time optimizations. In particular, because a sealed class is
    known to never have any derived classes, it is possible to transform virtual function member
    invocations on sealed class instances into non-virtual invocations.



    You didn't say which version of the compiler you're using, but... well, no. The compiler can use call rather than callvirt if it chooses to. Sealing the class doesn't "enable" this. Ildasm is your friend here. Besides, sealed is a design-level tool, not a particularly handy perf technique.

    As better developers than I have said for a long time, first profile your application's performance; then when you know which parts are slow you can fix them. No sense putting the cart before the horse.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.