title: Recording Program's CPU Time
body: <p>I want to record the time taken by my Java program. Most forums suggest something like:</p>
  
  <pre><code>int starttime=System.nanoTime();  
  //program code  
  int endTime=System.nanoTime();  
  int timetaken=starttime-endTime;  
  </code></pre>
  
  <p>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?</p>