How to perform a good test of comparative effectiveness?

To write a good comparison test test, you need to run it several thousand (million) times. It will influence the influence of other programs (in most cases).

But if the JVM can influence the results. For instance:

First decision:

final StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(getStrOne()); stringBuilder.append(getStrTwo()); final String result1 = stringBuilder.toString(); 

And the second:

  final String result2 = getStrOne() + getStrTwo(); 

I do not know which one is better, because the JVM can influence the results. How to find out which one is better?

UPDATE . I do not mean exactly what the company test adds. I ask for such a difficult situation to test.

+6
source share
2 answers

I recently conducted several benchmarks that were based on an excellent IBM article found here: http://www.ibm.com/developerworks/java/library/j-benchmark1/index.html .

The article describes many pitfalls that can affect the accuracy of the results, for example:

  • Runtime optimization / recompilation of your code.
  • Elimination of dead code (i.e., unused results can lead to deletion of test code)
  • Garbage collection
  • Caching
  • ...

Finally, the article links to a site where the framework can be downloaded . Within this framework, very good work is in buffering the test method, looking for evidence of recompilation, and waiting for execution time.

+2
source

In the case of 2 lines, the performance differences are slight, but try the following:

 String s = ""; for( int i = 0; i < 10000; i++ ) { s += i; } 

vs.

 StringBuilder b = new StringBuilder(); for( int i = 0; i < 10000; i++ ) { b.append(i); } 

You will find that the second cycle is faster. What for? Because string concatenation will create a new String object at each iteration, which takes up processor cycles as well as memory.

I will give you:

To write a test test of good comparisons, you need to test it several thousand (million) times. It will influence the influence of other programs (in most cases).

The same applies to tests in one virtual machine: check your code several times, use big data, big loops, etc. Comparison of only small parts does not make sense due to errors in the accuracy of time and other influences (for example, garbage collection working between them). A.

0
source

All Articles