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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:26:16+00:00 2026-06-16T21:26:16+00:00

I need to get the execution time for a function in Java. I am

  • 0

I need to get the execution time for a function in Java. I am aware of two methods I can use:
currentTimeMillis(); and nanoTime(); But I have learned that currentTimeMillis(); is more accurate if I need the wall-clock time (i.e, as if I am measuring from the wall-clock how much time the execution took. Not the processing time).

But currentTimeMillis(); can not give me small fraction number. For example, if the execution time is less than 1 millisecond, it returns 0. I need the time even if it is less than 1, say, 0.05 ms. This is a simple example when the method returns 0.

long startTime=System.currentTimeMillis();
for(int x=0; x<10;x++)
{
    System.out.println("-");
}

long execTime=System.currentTimeMillis() - startTime;

Even if it returns time, it return it as 30 or 40. But, I need a more precise number, say, 30.00012. Also, the method return type is long but I changed it to double as I want the number in a floating point, is there any harm in this? Can you tell me to the proper way by which I can measure my java method execution wall-clock time in small fraction number (e.g. not 8.0 but 8.287335)

  • 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-16T21:26:17+00:00Added an answer on June 16, 2026 at 9:26 pm

    Although you can and should use nanoTime to get the most precise available execution time for a block of code. However, you should use it with caution, because there are many outside issues that can affect execution time.

    For example, any time a method is called without having run recently, there will be overhead for getting it into cache. Depending on what else is in the same page, there may even be a disk read to get it into memory.

    A JVM may treat a method differently if it only runs once compared to running it many times.

    Generally, when you care about microseconds it is because the thing you are measuring is going to run many times in a job or transaction. To get an accurate measure of that you need to measure across many calls. Doing so also reduces the impact of the overhead time for calling nanoTime.

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

Sidebar

Related Questions

I have two tables with a weak relation. I need get a text value
Hii! I want to get the time execution of my function ( test(G) ).
I was looking at execution time of some functions, but i have found that
I need get all items these have no categories int? categoryId = null; var
I have linq request. I need get item.Title in select. how do this? var
I need to get the contents from this URL http://google.fr/ok in a NSString can
I have a query in java string. What i need to do is fire
How can I get true URL ASAP ? I need to check big number
I need to construct linq, but day (date time type, i.AllowedTime.AllowedDate.Day), hours and minutes
I need get the id of an images which is place inside a datatemplate..

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.