... I don't want a function or a library that will take 10 ms to get the time
Firstly, you are exaggerating. System.nanoTime()
does not take 10 milliseconds.
Secondly, I don't think there is a faster way in pure Java. You might possibly be able to access a system clock faster from native code, but your code will be platform specific. (Besides, my gut feeling is that System.nanoTime()
will do it as fast as possible.)
Accessing a variable may take as little as a couple of machine instruction, depending on the context and the kind of variable you are accessing. This likely to be swamped by the time taken access a system timer.
Furthermore the insertion of method calls to a clock method before a variable access may make the variable access take longer. The method call is liable to invalidate the content of registers, causing the code generator to insert instructions to refetch the variable from memory.
In short, insertion of the code to fetch the clock times is liable to give you unreliable and misleading timing numbers. You can potentially get more meaningful numbers by timing LONG sequences of instructions ... but even that can give you bogus numbers if you make mistakes in your benchmarking methodology.