Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3320780
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:59:03+00:00 2026-05-17T22:59:03+00:00

During a recent code review a colleague suggested that, for class with 4 int

  • 0

During a recent code review a colleague suggested that, for class with 4 int properties, assigning each to zero in the constructor would result in a performance penalty.

For example,

    public Example()
    {
        this.major = 0;
        this.minor = 0;
        this.revision = 0;
        this.build = 0;
    }

His point was that this is redundant as they will be set to zero by default and you are introducing overhead by essentially performing the same task twice. My point was that the performance hit would be negligible if one existed at all and this is more readable (there are several constructors) as the intention of the state of the object after calling this constructor is very clear.

What do you think? Is there a performance gain worth caring about here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:59:04+00:00Added an answer on May 17, 2026 at 10:59 pm

    I don’t believe they’re the same operation, and there is a performance difference. Here’s a microbenchmark to show it:

    using System;
    using System.Diagnostics;
    
    class With
    {
        int x;
    
        public With()
        {
            x = 0;
        }
    }
    
    
    class Without
    {
        int x;
    
        public Without()
        {
        }
    }
    
    
    class Test
    {
        static void Main(string[] args)
        {
            int iterations = int.Parse(args[0]);
            Stopwatch sw = Stopwatch.StartNew();
            if (args[1] == "with")
            {
                for (int i = 0; i < iterations; i++)
                {
                    new With();
                }
            }
            else
            {
                for (int i = 0; i < iterations; i++)
                {
                    new Without();
                }
            }
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);
        }
    }
    

    Results:

    c:\Users\Jon\Test>test 1000000000 with
    8427
    
    c:\Users\Jon\Test>test 1000000000 without
    881
    
    c:\Users\Jon\Test>test 1000000000 with
    7568
    
    c:\Users\Jon\Test>test 1000000000 without
    819
    

    Now, would that make me change the code? Absolutely not. Write the most readable code first. If it’s more readable with the assignment, keep the assignment there. Even though a microbenchmark shows it has a cost, that’s still a small cost in the context of doing any real work. Even though the proportional difference is high, it’s still creating a billion instances in 8 seconds in the “slow” route. My guess is that there’s actually some sort of optimization for completely-empty constructors chaining directly to the completely empty object() constructor. The difference between assigning to two fields and only assigning to one field is much smaller.

    In terms of while the compiler can’t optimize it out, bear in mind that a base constructor could be modifying the value by reflection, or perhaps a virtual method call. The compiler could potentially notice those, but it seems a strange optimization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

During the most recent Google IO, there was a presentation about implementing restful client
where could I place code to be run after every commit I make with
it gives me this error: Exception in thread Thread-163: Traceback (most recent call last):
Will using GetComInterfaceForObject and passing the returned IntPtr to unmanaged code keep the managed
PHP provides a mechanism to register a shutdown function: register_shutdown_function('shutdown_func'); The problem is that
I have a Controller that pulls images from a database, re-sizes them, caches the
I need to create an install for my app that executes the following actions:
I have a little app that downloads stock prices and was working perfectly (for
I have a database that can have different types of relatinships based upon what
How to make a multi-thread python program response to Ctrl+C key event? Edit: The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.