Results 1 to 7 of 7
  1. #1
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Gain performance by not initializing variables (VS.NET2003)

    I don't venture in here much, so I don't even know how relevant this article is:
    The Code Project - Gain performance by not initializing variables - .NET

    "Explicitly initializing class member variables and method variables actually reduces performance in .NET. This article explores the different types of variable initialization schemes available in .NET and the performance impact of each initialization scheme. Measurements indicate that expressly initializing variables slows object initialization by 10% and slows method calls by about 20%."

    Typical of the results obtained:
    Using Visual Studio .NET 2003 in "Release" mode (with the optimizer using default settings) and running on my Win2K 1.6GHz Pentium-M machine:

    Creating an object and not initializing variables ~503mSec (100%)
    Creating an object and initializing on definition ~557mSec (111%)
    Creating an object and initializing in the constructor ~582mSec (116%)
    Calling a method and not initializing local variables ~253mSec (100%)
    Calling a method and initializing variables ~316mSec (125%)

    Alan

  2. #2
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Gain performance by not initializing variables (VS.NET2003)

    That's interesting. Although, I would generally take a more explicitly readable code approach over a few extra milliseconds any day...

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Gain performance by not initializing variables

    Being a VBA-oriented person, I only ever initialized variable values as part of their declaration on rare occasions, such as in a function's arguments list, for optional parameters:

    <pre>Private Function WildReplace(strExpression As String, strFind As String, _
    strReplace As String, Optional bolReplaceAll As Boolean = True, _
    Optional bolCaseSensitive As Boolean = False) As String</pre>


    I generally trusted VB to reliably assign the default values of 0 to integers and other numbers, and "" to strings. Generally, but sometimes I assigned those values myself because occasionally a variable lingered in memory longer than I expected, causing unexpected results.

    If the linked article is correct, all .Net languages now do what VB has been doing, so people explicitly setting those initial values are wasting cycles. Laziness rules! But as Mark says, if you want to KNOW (rather than GUESS) the value, it is useful to go ahead and set it yourself.

  4. #4
    Silver Lounger
    Join Date
    Jan 2001
    Location
    Indianapolis, Indiana, USA
    Posts
    1,862
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Gain performance by not initializing variables

    I think the biggest payoff for explicit initializing comes when you're using custom data types, classes, or structures. You never know what code is running behind the scenes (within the object being initialized), so it's a helpful habit to add that extra insurance.

    Just my 2 cents.

  5. #5
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Gain performance by not initializing variables

    Coming from a C++ background, I'd always had correct initialization "drummed in", and the maximized use of initialization lists in class constructors is (usually) considered "best practice". In fact, it's a far bigger deal generally in C++ than it is in VB. I think the article was geared more to VC, where it represents more of a departure, than it would be from "standard" VB practices.

    That said, I've done nothing (practical) in .NET myself so far, so I remain eminently unqualified to comment! But as Jefferson surmized, I get the impression that .NET has a lot of features that attempt to combine the best of all worlds... and it looks pretty good to me, from the little I've read so far.

    Alan

  6. #6
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Gain performance by not initializing variables

    Me too.

    And I would not want to worry about some bug in the compiler/interpreter not initializing variables properly.

    Not to mention, that explicit initialization MIGHT be evidence that the programmer THOUGHT about things.

  7. #7
    Platinum Lounger
    Join Date
    Nov 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    5,016
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Gain performance by not initializing variables

    All depending on the scope of the application of course. If we're talking saving a few milliseconds millions of times over, on a large distributed database, then those savings DO add up and become significant. With low demand applications, it's probably better practice to go the explicit initialization route.

    Alan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •