For evaluating an algorithm I have to count how often the items of a byte-array are read/accessed. The byte-array is filled with the contents of a file and my algorithm can skip over many of the bytes in the array (like for example the Boyer–Moore string search algorithm). I have to find out how often an item is actually read. This byte-array is passed around to multiple methods and classes.
My ideas so far:
-
Increment a counter at each spot where the byte-array is read. This seems error-prone since there are many of these spots. Additionally I would have to remove this code afterwards such that it does not influence the runtime of my algorithm.
-
Use an ArrayList instead of a byte-array and overwrite its “get” method. Again, there are a lot of methods that would have to be modified and I suspect that there would be a performance loss.
-
Can I somehow use the Eclipse debug-mode? I see that I can specify a hit-count for watchpoints but it does not seem to be possible to output the hit count?!
-
Can maybe the Reflection API help me somehow?
-
Somewhat like 2), but in order to reduce the effort: Can I make a Java method accept an ArrayList where it wants an array such that it transparently calls the “get” method whenever an item is read?
There might be an out-of-the-box solution but I’d probably just wrap the byte array in a simple class.
Something along these lines. Of course this does influence the running time but not very much. You could try it and time the difference, if you find it is significant, we’ll have to find another way.