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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:05:03+00:00 2026-05-30T22:05:03+00:00

So I was trying to measure the time two different algorithm implementations took to

  • 0

So I was trying to measure the time two different algorithm implementations took to accomplish a given task, and here is the result:

i    alg1  alg2
4   0.002   0.0
5   0.001   0.0
6   0.003   0.002
7   0.023   0.01
8   0.055   0.041
9   0.056   0.0
10  0.208   0.101
11  1.767   0.694
12  18.581  7.784

being i just some input parameter.

I’ve measured the performance of the algorithms making use of the following (naive) function:

private double getDuration() {
    return (double)(System.currentTimeMillis() - startTime) / (double)1000;
}

What would be the preferable way of getting more real results (other than 0.0, which obviously isn’t true!) than using System.currentTimeMillis()? I know I could just run the algorithms over and over again and sum their results, but I have this gut feeling that there is probably some more robust way of measuring passed time in Java (both real, user and sys, if possible!).

Thanks

  • 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-30T22:05:04+00:00Added an answer on May 30, 2026 at 10:05 pm

    For basic timing, you can use Guava’s Stopwatch class (or just grab its source code if you don’t want to pull in the whole Guava library). For a more complete benchmarking solution look at Caliper by the same team.

    Both of these are based on System.nanoTime(), which you should prefer over System.currentTimeMillis() for measuring elapsed time. The basic reason why is that System.currentTimeMillis() is a “clock” (which tries to return wall-time) whereas System.nanoTime() is a “timer” (which tries to return time since some arbitrary point).

    You want a clock when you’re trying to figure out when a single event happened, so you can line it up with your watch or the clock on your wall (or the clock in some other computer). But it’s not appropriate for measuring the elapsed time between two events on the same system, since the computer will occasionally adjust its notion of how its own internal clock corresponds to wall-time. For instance, if you do

    long timeA = System.currentTimeMillis();
    doStuff();
    long timeB = System.currentTimeMillis();
    System.out.println("Elapsed time: " + (timeB - timeA));
    

    it’s possible to get a negative result if NTP adjusts backwards while doStuff() is executing. System.nanoTime(), being a timer instead of a clock, should ignore that adjustment thus avoid this problem.

    (Note that all of the above is conceptual; unfortunately things can get messy at the implementation level. But this doesn’t change the recommendation: System.nanoTime() is supposed to be the best timer you can get on your platform, and System.currentMilliseconds() is supposed to be the best clock you can get.)

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

Sidebar

Related Questions

I am trying to measure short time intervals, in order few of milliseconds. This
I am trying to measure CPU time. It works great on Win 32, but
I am trying to measure the time of raw_queries(...) , unsuccessfully so far. I
I'm trying to determine the relative performance of two different queries and have two
I'm trying to measure the elapsed time in milliseconds between keypress events using a
I am trying to measure the execution time for several methods. so I was
I am trying to measure the computation time of a function in R using
I am trying to calculate the measure time of html 5 video. I use
I'm trying to measure the execution time of some bits of code as accurately
I am trying to measure the execution time taken by some functions in my

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.