Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Systtem.nanoTime() and the CPU clock

807603Dec 11 2007 — edited Dec 13 2007
Hello,

I have tried to do some tests on 5 algorithms commparison with System.nanoTime(). So I did the same procedure on every algorithm. Now I wonder are the results really reliable.
1. So lets say I want to measure the time of process A. And "|" mean 1ns will the following
long start = System.nanoTime();
<the code that stands for A >
System.outprintln(System.nanoTime() - start);
 A                                  B
A_start
 |                                    
 |
.....................................|
                                     |
                                     |
.....................................|
|
|
A_end
result with 4ns or 8 ns??

Linux 2.6.23.8 jdk 1.6 u 3
2.
If the ansewer is 4 then in what way can i measure the the exact time of a process, becouse then that would mean that it measures only distance in time from A_start to A_end not the code execution time itself (the 4ns from B process)

3. Would this be the solution:
http://www.javaworld.com/javaworld/javatips/jw-javatip92.html?page=2
Afterall it's 7 years old so maybe now thre is some work around?

Edited by: DefTower on Dec 11, 2007 1:50 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 10 2008
Added on Dec 11 2007
14 comments
966 views