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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:16:00+00:00 2026-06-03T06:16:00+00:00

I am trying to measure the complexity of an algorithm using a timer to

  • 0

I am trying to measure the complexity of an algorithm using a timer to measure the execution time, whilst changing the size of the input array.

The code I have at the moment is rather simple:

public void start() {
    start = System.nanoTime();
}

public long stop() {
    long time = System.nanoTime() - start;
    start = 0;
    return time;
}

It appears to work fine, up until the size of the array becomes very large, and what I expect to be an O(n) complexity algorithm turns out appearing to be O(n^2). I believe that this is due to the threading on the CPU, with other processes cutting in for more time during the runs with larger values for n.

Basically, I want to measure how much time my process has been running for, rather than how long it has been since I invoked the algorithm. Is there an easy way to do this in Java?

  • 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-03T06:16:02+00:00Added an answer on June 3, 2026 at 6:16 am

    Measuring execution time is a really interesting, but also complicated topic. To do it right in Java, you have to know a little bit about how the JVM works. Here is a good article from developerWorks about benchmarking and measuring. Read it, it will help you a lot.

    The author also provides a small framework for doing benchmarks. You can use this framework. It will give you exaclty what you needs – the CPU consuming time, instead of just two time stamps from before and after. The framework will also handle the JVM warm-up and will keep track of just-in-time-compilings.

    You can also use a performance monitor like this one for Eclipse. The problem by such a performance monitor is, that it doesn’t perform a benchmark. It just tracks the time, memory and such things, that your application currently uses. But that’s not a real measurement – it’s just a snapshot at a specific time.

    • 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 CPU time using following code. timespec time1, time2, temp_time;
So I was trying to measure the time two different algorithm implementations took to
I am trying to measure the execution time for several methods. so I was
I'm trying to measure the elapsed time in milliseconds between keypress events using a
I am trying to measure the computation time of a function in R using
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 am trying to measure the CPU time of following code - struct timespec

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.