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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:29:53+00:00 2026-05-13T01:29:53+00:00

I have an algorithm of which I’m using System.Diagonstics to time – via the

  • 0

I have an algorithm of which I’m using System.Diagonstics to time – via the Stopwatch.

It works great but one thing I have noticed is that the first time I run the algorithm it takes around 52 milliseconds which is great.

The second time I run the algorithm it takes only a fraction of that time.

Is this due to the nature of .NET?

Each time I run the algorithm with a new set of data I re-initalise it. In other words I create a new object rather than re-use the old reference so I’m not sure why this still occurs. Normally I wouldn’t care about something like this, but for this assignment I must measure the efficency and speed of my algorithms so it is important for myself to get an understanding to why this is happening.

Pseudo code of how I’m using the timer is below:

 Algorithm class

 Stopwatch get/set

 Method A
     Start stopwatch
     // Do work.
     Stop stopwatch
 End

 Method B
     Start stopwatch
     // Do work.
     Stop stopwatch
 End

End

After both methods are called in my runner, I get the stopwatch and inspect the time.

The algorithm

The algorithm is tactical waypoint reasoning for computer controlled A.I opponnents. I tried to keep it as simple as possible in the above example.

Results

19.7847
0.0443
0.0102
0.0159
0.0091
0.0073
0.0079
0.0079
0.0079
0.0079
0.0079
0.0079
0.0136
0.0079
0.0073
0.0079
0.0079
0.0079
0.0079
0.0073
...

Should I just ignore the first time the algorithm is run? Otherwise I’ll end up with an average that is essentially the same as the value when its first run.

  • 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-13T01:29:53+00:00Added an answer on May 13, 2026 at 1:29 am

    If you’re only timing for 52 milliseconds, any number of things could be happening – that’s a very small amount of time to measure.

    It could well be that it’s due to JIT compilation of the method and everything it touches, for example.

    In general, to get useful measurements you should time multiple iterations to get a longer period – this reduces the noise due to (for example) some other event in your operating system taking the CPU away briefly.

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

Sidebar

Related Questions

I have an algorithm which currently allocates a very large array of doubles, which
I have an algorithm which can find if a point is in a given
If I have an algorithm which is comprised of (let's say) three sub-algorithms, all
Say I have an algorithm which operates on an input of size n and
I have a recursive algorithm which steps through a string, character by character, and
I have a small algorithm which replaces the position of characters in a String:
I have written an algorithm which iteratively solves the problem. The first iteration consists
First: The problem's name in Wikipedia is ordered partition of a set. I have
I want to write a String to a Stream (a MemoryStream in this case)

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.