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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:53:21+00:00 2026-05-22T14:53:21+00:00

So, I am trying to measure the execution time of some sorting methods. Here

  • 0

So, I am trying to measure the execution time of some sorting methods.

Here is my code:

public static void main(String[] args)
{
    ...

    MeasureExecutionTime(new Runnable() { public void run() { insertionSort(C); } }, "insertionSort()");
}

=====================

private static void MeasureExecutionTime(Runnable r, String s)
{
    startTime = System.nanoTime();
    try
    {
        r.run();
    }
    finally
    {
        endTime = System.nanoTime();
    }
    elapsedTime = endTime - startTime;
    System.out.println(s + " takes " + elapsedTime + " nano-seconds which is " + formatTime(elapsedTime));
}

=====================

public static String formatTime(long nanoSeconds)
{
    long hours, minutes, remainder, totalSecondsNoFraction;
    double totalSeconds, seconds;

    totalSeconds = (double) nanoSeconds / 1000000000.0;
    String s = Double.toString(totalSeconds);
    String [] arr = s.split("\\.");
    totalSecondsNoFraction = Integer.parseInt(arr[0]);
    hours = totalSecondsNoFraction / 3600;
    remainder = totalSecondsNoFraction % 3600;
    minutes = remainder / 60;
    seconds = remainder % 60;
    seconds = Double.parseDouble(Long.toString((long)seconds) + Double.parseDouble("." + arr[1]));

    StringBuilder result = new StringBuilder(".");
    String sep = "", nextSep = " and ";
    if(seconds > 0)
    {
        if(seconds > 1) result.insert(0, " seconds").insert(0, seconds);
        else result.insert(0, " second").insert(0, seconds);
        sep = nextSep;
        nextSep = ", ";
    }
    if(minutes > 0)
    {
        if(minutes > 1) result.insert(0, sep).insert(0, " minutes").insert(0, minutes);
        else result.insert(0, sep).insert(0, " minute").insert(0, minutes);
        sep = nextSep;
        nextSep = ", ";
    }
    if(hours > 0)
    {
        if(hours > 1) result.insert(0, sep).insert(0, " hours").insert(0, hours);
        else result.insert(0, sep).insert(0, " hour").insert(0, hours);
    }
    return result.toString();
}

My problem is:

After run this program, and I enter int[1000000] as an input, it executes insertionSort() in about 12-13 minutes and then returns:

insertionSort() takes 767186856920 nano-seconds which is 12 minutes and 470.18685692 seconds.

why it gives 470 seconds? what is wrong in my code?

=========================

EDIT:

After replacing seconds = Double.parseDouble(Long.toString((long)seconds) + Double.parseDouble("." + arr[1])); with seconds = seconds + Double.parseDouble("." + arr[1]);, the previous issue is gone, but another issue showed up:

insertionSort() takes 22864 nano-seconds which is 2.000002864 seconds.

It should be 0.000022864 seconds.

=========================

EDIT2:

I might discover the error. when nanoSeconds is large, arr[1] will be ok but when nanoSeconds is small, arr[1] will convert to exponential form, i.e 14931 nano-seconds => 4.931E-6 seconds.. How can I solve this issue?

==========================

EDIT3:

Ok, I found the solution:

if(arr[1].contains("E")) seconds = Double.parseDouble("." + arr[1]);
else seconds += Double.parseDouble("." + arr[1]);
  • 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-22T14:53:21+00:00Added an answer on May 22, 2026 at 2:53 pm

    The problem is here:

    seconds = Double.parseDouble(Long.toString((long)seconds) + 
                                 Double.parseDouble("0." + arr[1]));
    

    Say seconds is 12 entering this line and arr[1] is "456". Then

    seconds = Double.parseDouble("12" + Double.parseDouble("0.456"));
    seconds = Double.parseDouble("12" + 0.456);
    seconds = Double.parseDouble("12" + "0.456");
    seconds = Double.parseDouble("120.456");
    seconds = 120.456.
    

    Why not just do:

    seconds = seconds + Double.parseDouble("0." + arr[1]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to measure the execution time of some bits of code as accurately
I am trying to measure the execution time for several methods. so I was
I am trying to measure the execution time taken by some functions in my
I'm trying to use following code with System.nanoTime() to measure the elapsed time of
I'm trying to measure time between taps. I've gathered code from a couple other
I'm trying to measure how long it takes read then encrypt some data (independently).
I am trying to use EMMA to measure coverage of some JUnit tests that
I am trying to measure short time intervals, in order few of milliseconds. This
I am trying to measure the time of raw_queries(...) , unsuccessfully so far. I
Is there a easy way to measure execution time of all sql statements that

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.