Archive for Maggio, 2011

Maggio 15, 2011

Benchmark in

Trying to better understand the functioning of the Java language (in particular of processing) I decided to build a small class of benchmark with which to keep monitored the duration of some operations computationally expensive. The usage of the class is very simple: copy the file. pde in the folder of your sketch and open the main file (for example mySketch.pde in a folder with the same name “mySketch”). The usage that I suggested (but you are free to adapt it to your taste and your needs) is to declare an instance of the class benchmark at the beginning of the code, then the instance will be initialized at the beginning of phase to be monitored specifying the subject of control (a simple string that will be reported in the console) and successively is close. Will then appear on the console the duration of the operation (or loop of operations), in milliseconds. You don’t need to create a new instance of the benchmark class for each control, just overwrite the same every time.

usage example:

benchmark myBenchmark;

... code ...

myBenchmark = new benchmark("myOperation time");
myBenchmark.end(); //return in console "myOperation time: ###ms"


myBenchmark = new benchmark("myAnotherOperation time");
...another operation...
myBenchmark.end(); //return in console "myAnotherOperation time: ###ms"

This is really simple, but I hope useful.

download benchmark.pde

I improved my benchmark using class method static instead of default instance. In this way it is not possible to output nested benchmark controls (you can have a nested control copying the class with a different name for every hierarchy level) on the other hand it is possible to access to benchmark methods without having to instantiate the generic class benchmark.
I also insert a memory control that display total memory used by virtual machine and a delta memory that, after the calling of garbage collector, evaluate the memory used by myOperation.

usage example:

... operations ...


example of benchmark output

download benchmark_static.pde