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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:19:57+00:00 2026-06-18T12:19:57+00:00

I want to record the time taken by my Java program. Most forums suggest

  • 0

I want to record the time taken by my Java program. Most forums suggest something like:

int starttime=System.nanoTime();  
//program code  
int endTime=System.nanoTime();  
int timetaken=starttime-endTime;  

The problem with this approach is that if there are multiple programs running, then the difference between endTime and starttime is not the time taken by my program. It is also the time taken during scheduling of different programs in the CPU etc. However, I am only interested to record the time taken by my program. How do I get this time?

  • 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-18T12:19:58+00:00Added an answer on June 18, 2026 at 12:19 pm

    you can find the answer in this article.
    to quote from the article:

    Call ManagementFactory . getThreadMXBean() to get a ThreadMXBean that
    describes current JVM threads. The bean’s getCurrentThreadCpuTime()
    method returns the CPU time for the current thread. The
    getCurrentThreadUserTime() method returns the thread’s user time. Both
    of these report times in nanoseconds

    if your application is multi-threaded you’d need to record the cpu time per application thread and sum them up to get the total cpu time taken up by your application across all of its threads.

    having said that – if youre really serious about profiling your cpu consumption you should probably be using a profiler. you can start with a free open source one like visualvm (also comes bundled with the jdk) which will (among many other things) answer this exact question very easily.

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

Sidebar

Related Questions

I want to record the time using System.currentTimeMillis() when a user begins something in
I want to record Cpu usage ,cpu time ,VM size in notepad per minute
I want to select one record at a time from a MySQL table. I
I want to record my most recent cd across any one of my terminals.
I want to build a 3-tiers system for EHS(Electronic Health Record) or EMS(Electronic Medical
I'm looking for a simple, reusable way to record the time taken to execute
I want to record voice as long as holding the record button and save
I want to record the handshake messages between server and client into the file
i want to record video through webcam using Directshow.Net.i can able to record the
I want to record what various sources have to say about a historical figure.

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.