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 3323740
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:20:18+00:00 2026-05-17T23:20:18+00:00

Stopwatch sw = new Stopwatch(); for (int i = 0; i < lines.Length; i++)

  • 0
            Stopwatch sw = new Stopwatch();
            for (int i = 0; i < lines.Length; i++)
            {
                sw.Start();
                fn(); //call function
                sw.Stop();

            }
            Console.WriteLine(sw.ElapsedMilliseconds);


            long total =0;
            for (int i = 0; i < lines.Length; i++)
            {
                Stopwatch sw = Stopwatch.StartNew();
                fn(); //call function
                sw.Stop();
                total += sw.ElapsedMilliseconds;

            }
            Console.WriteLine(total);

The output is not the same, do you have any explanation for that?

  • 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-17T23:20:19+00:00Added an answer on May 17, 2026 at 11:20 pm

    Leaving aside things like the fact that you’re creating loads of objects in the second loop, which could easily cause garbage collection within fn() or something else to actually make it take longer while timing, you’re also just taking the elapsed milliseconds each iteration in the second case.

    Suppose each iteration takes 0.1 milliseconds. Your total for the second loop would be 0, because on each iteration it would round down the elapsed time to 0 milliseconds. The first loop keeps track of the elapsed ticks.

    Leaving all this aside, you shouldn’t be starting and stopping the timer this frequently anyway – it will mess with your results. Instead, start the stopwatch once before the loop, and stop it after the loop.

    If you want to take out the overhead of the looping, simply time an empty loop to find the overhead, and subtract that from the time taken with a loop containing actual work. Now it’s not really quite that simple, because of the various complexities of real world CPUs – things like cache misses – but microbenchmarking is frankly never particularly accurate in that respect. It should be used as a guide more than anything else.

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

Sidebar

Related Questions

Hola check this code. var watch = new Stopwatch(); watch.Start(); var request = HttpWebRequest.Create(new
Debug.WriteLine(Timer is high-resolution: {0}, Stopwatch.IsHighResolution); Debug.WriteLine(Timer frequency: {0}, Stopwatch.Frequency); Result: Timer is high-resolution: True
I'm totally confused between these 4. What is the difference between ElapsedMilliseconds (long), ElapsedTicks
public partial class _Default : System.Web.UI.Page { private static Stopwatch timer = new Stopwatch();
I have a little snag , I ought to Convert my Stopwatch into Int
Possible Duplicates: Is DateTime.Now the best way to measure a function's performance? Stopwatch vs.
At MSDN page for Stopwatch class I discovered link to interesting article which makes
I need to replace Stopwatch to avoid using getters for its properties. I am
QueryPerformanceCounter() and Stopwatch.GetTimestamp() both return the tick count from one of the processors. I'd
For my stopWatch project, i want to run the timer, when application goes to

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.