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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:55:59+00:00 2026-06-14T00:55:59+00:00

While writing on a program I currently work on, I noticed some change in

  • 0

While writing on a program I currently work on, I noticed some change in perfomance and wanted to check the calculation times in some loops I am using to explain this.

I wrote a very simple program that does a simple addition calculation in a loop and outputs the time it took to do it and I noticed two for me at the moment unexplainable effects.

First the code:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        int a = 1, b = 2, c = 0;

        Stopwatch sw = new Stopwatch();

        sw.Start();
        for (int i = 0; i < 100; i++)
        {
            c += (a + b);
        }
        sw.Stop();
        c = 0;
        textBlock1.Text = 100 + ": " + sw.ElapsedTicks;

        sw.Restart();
        for (int i = 0; i < 1000; i++)
        {
            c += (a + b);
        }
        sw.Stop();
        c = 0;
        textBlock2.Text = 1000 + ": " + sw.ElapsedTicks;

        sw.Restart();
        for (int i = 0; i < 10000; i++)
        {
            c += (a + b);
        }
        sw.Stop();
        c = 0;
        textBlock3.Text = 10000 + ": " + sw.ElapsedTicks;

        sw.Restart();
        for (int i = 0; i < 1000000; i++)
        {
            c += (a + b);
        }
        sw.Stop();
        c = 0;
        textBlock4.Text = 1000000 + ": " + sw.ElapsedTicks;

        sw.Restart();
        for (int i = 0; i < 100000000; i++)
        {
            c += (a + b);
        }
        sw.Stop();

        textBlock5.Text = 100000000 + ": " + sw.ElapsedTicks;
    }
}

Now I could oberserve the following:

  1. The first time after starting the program clicking on the button and calculating, the short loop of 100 takes around twice/three times (6-9 ticks) as long as the second time I run it (2-4). After that it will remain at around the same length (2-4 ticks).

  2. There are weird proportions between the loop times which remain quite constant when I keep clicking the button which are about as follows:

    • 100: 3
    • 1000: 15
    • 10000: 150
    • 1000000: 17000
    • 100000000: 840000

Is there an explanation why the 100 loop takes aroud 1/5 of the time of the 1000 loop, even though it is 1/10 times as many calculations, and also why does the 100 Mio. loop take only around 50 times as long as the 1 Mio. loop even though it is 100 times as many calculations?
And does anybody have an idea why the time (especially for the 100 loop) change after I ran the calculation once after starting the program and then remains quite constant?

  • 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-06-14T00:56:00+00:00Added an answer on June 14, 2026 at 12:56 am

    Is there an explanation why the 100 loop takes aroud 1/5 of the time of the 1000 loop, even though it is 1/10 times as many calculations

    You’re measuring three ticks that’s a tiny, tiny amount of time. If you happened to run into a context switch, you’d probably lose much more time than that, even if your thread was scheduled again straight away. It’s basically not a large enough amount of time to be measured sensibly, even with Stopwatch.

    And does anybody have an idea why the time (especially for the 100 loop) change after I ran the calculation once after starting the program and then remains quite constant?

    My guess is that it’s to do with JIT compilation and/or garbage collection.

    This is why typically benchmarks run a “warmup” round first, and they test a sensible amount of work so that the tests will take a reasonable amount of time, smoothing little bumps in timing.

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

Sidebar

Related Questions

I'm currently writing a little GUI program that does some work and exits afterwards.
Let we explain what I mean. Some time ago, while writing a program in
Writing a python program, and I came up with this error while using the
I've run into this while writing a Traveling Salesman program. For an inner loop,
While writing some C code, I decided to compile it to assembly and read
I am currently writing a program's main function(the bare-bone function so far) so far
I'm currently writing a little program in c++ on my 64bit Ubuntu Pc. By
I am writing a program in which at some point a graph is plotted
I'm currently writing a program to shut down a computer when over a period
I am currently writing a program in Java that uses a query populated jcombobox.

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.