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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:01:17+00:00 2026-05-31T19:01:17+00:00

I wrote some code which monitors the output of Stopwatch using a tight loop.

  • 0

I wrote some code which monitors the output of Stopwatch using a tight loop. This loop tracks the number of ticks which have elapsed since the last iteration. I’m observing jumps of 500 microseconds 20 times a second, while most other iterations take <1µs.

Can someone explain why I’m seeing these jumps?

I’ve tried:

  1. Setting processor affinity (no effect)
  2. Changing thread priority (Highest/AboveNormal makes things worse)
  3. Running outside of the debugger
  4. Release build with optimizations

My code is below:

        Stopwatch sw = new Stopwatch();
        int crossThresholdCount = 0;
        long lastElapsedTicks = 0;
        long lastPrintTicks = 0;
        Console.WriteLine("IsHighResolution: " + Stopwatch.IsHighResolution);
        Console.WriteLine("Frequency: " + Stopwatch.Frequency);

        sw.Start();

        long thresholdTicks = 5000;  // 10000 ticks per ms
        while (true)
        {
            long tempElapsed = sw.ElapsedTicks;
            long sincePrev = tempElapsed - lastElapsedTicks;
            lastElapsedTicks = tempElapsed;

            if (sincePrev > thresholdTicks)
                crossThresholdCount++;

            // print output 
            if (crossThresholdCount > 0 && tempElapsed - lastPrintTicks > TimeSpan.TicksPerSecond)
            {
                lastPrintTicks = tempElapsed;
                Console.WriteLine("crossed " + crossThresholdCount + " times");
                crossThresholdCount = 0;
            }
        }
  • 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-31T19:01:18+00:00Added an answer on May 31, 2026 at 7:01 pm

    Most likely you are seeing preemptive task switching. This is when the operating system suspends your program, and goes off to execute other programs. This is how things have been since Windows 95 (Win 3.1 and earlier had cooperative multitasking, whereby you could hold the CPU for as long as you want).

    By the way, there is a better way to time your execution accurately: QueryThreadCycleTime, which counts CPU cycles only when your code is executing, and so excludes such pauses.

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

Sidebar

Related Questions

I have been refactoring throwaway code which I wrote some years ago in a
I wrote some test code like this which compiled and worked fine... void threadtest()
For a homework assignment I wrote some scala code in which I have the
Using latest CTP5 with async/await keywords, I wrote some code, which apparently cannot compile:
i wrote a little addin, which does some formatting of my C# code. in
i'm using DbSimple, but there is some code which i could write into another
I'm very new to C#. My boss asked me to wrote some code using
As been requested, I wrote some code which basically change the document's date (the
I wrote some C++ code in which I used Templates. Since I used templates,
I have a dynamic link library written in VC++6. I wrote some code with

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.