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

  • SEARCH
  • Home
  • 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 7594425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:24:45+00:00 2026-05-30T21:24:45+00:00

I have three methods in c# that runs the same code but with a

  • 0

I have three methods in c# that runs the same code but with a little bit difference, my first code block is

Stopwatch s = new Stopwatch();
object o = new object();
s.Start();
for (int i = 0; i < 100000000; i++)
{
    o.ToString();
    o.GetType();
    o.GetHashCode();
}
s.Stop();
Console.WriteLine(s.ElapsedMilliseconds); //3100ms

and this costs 3100ms to run. Then if I make the object initialization inside the for that is increased to 7200ms my code block looks like this;

Stopwatch s = new Stopwatch();
s.Start();
for (int i = 0; i < 100000000; i++)
{
    object o = new object();
    o.ToString();
    o.GetType();
    o.GetHashCode();
}
s.Stop();
Console.WriteLine(s.ElapsedMilliseconds);//7200ms

However, if I initialize my object but not using any method inside this costs for 652ms. And my code is just like this one,

Stopwatch s = new Stopwatch();
s.Start();
for (int i = 0; i < 100000000; i++)
{
    object o = new object();
}
s.Stop();
Console.WriteLine(s.ElapsedMilliseconds);//625ms

So I wonder, 3100ms + 625ms is not comparable with 7200ms. What causes that much difference between the first two?

  • 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-30T21:24:47+00:00Added an answer on May 30, 2026 at 9:24 pm

    In your second block, you’re calling GetHashCode() on lots of new objects. From what I remember, the first time a non-overridden GetHashCode() method is called on an object, a syncblock is allocated for the object. That’s relatively expensive, although subsequent calls to GetHashCode for the same object (as per your first code) are cheap.

    So there are three things to bear in mind:

    • Cost of object allocation (and any GC)
    • Cost of the first call to a method
    • Cost of subsequent calls to methods

    That’s a generalization of course – many methods take the same amount of time however many times you call them, and other ones may be slow for (say) the first 10 calls and fast thereafter. I believe that in the case of GetHashCode() it’s “first call is expensive” territory though. Try it with a type which overrides GetHashCode() in some simple way and I suspect you’ll find the time taken plummets.

    Additionally it’s possible that GetType() takes a while to construct the Type for object the very first time it’s called – I’m not sure. Basically you’re measuring a bunch of different things together here, which always leads to difficult analysis.

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

Sidebar

Related Questions

Are there any methods/systems that you have in place to incentivize your development team
I have a C# application that runs on a computer connected to a large
I have a data structure that represents C# code like this: class Namespace: string
I have an EJB JAR + JSF WAR that runs fine on local GlassFish
I have a perl script that runs a series of batch scripts for regression
I have a Greasemonkey userscript which runs most of its code in an unprivileged
I have the following situation. I have an application that runs mostly on one
I have some code I inherited which has a lot of warnings that I
Is there builtin method that would do this or do I always have to
I have a method that posts HTTP data and displays a UIAlertView if there

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.