I have been looking into benchmarking lately, I have always been interested in logging program data etc. I was interested in knowing if we can implement our own memory usage code and implement our own time consumption code efficently inside our program. I know how to check long it takes for a code to run: 

```
public static void main(String[]args){
        long start = System.currentTimeMillis();
        // code
        System.out.println(System.currentTimeMillis() - start);
    }

```

I also looked into [Robust Java benchmarking, Part 1: Issues](http://www.ibm.com/developerworks/java/library/j-benchmark1/index.html), this tutorial is very comprehensive. Displays the negative effects of `System.currentTimeMillis();`. The tutorial then suggests that we use `System.nanoTime();` (making it more accurate?).

I also looked at [Determining Memory Usage in Java](http://www.roseindia.net/javatutorials/determining_memory_usage_in_java.shtml) for memory usage. The website shows how you can implement it. The code that has been provided looks inefficent because the person is calling 

`long L = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();`

After this he calls `System.gc();` (4 \* 4) = 16 times. Then repeating the process again.
Doesn't this also take up memory? 

So in conlusion, is it possible to implement an efficent benchmarking code inside your java program?