Automatic way to test performance optimizations



Hello,
In my code, I must sometimes implement peformance optimizations. By
definition, the functional behavior does not change through this,
which can be tested through the public interface (regression tests).
But I would also like to test, that the optimization is used in the
correct places. Even using reflection to gain private access to
fields, I find this is difficult without introducing code (e.g. flags)
which only use is testing.
As an example consider the below code found in Arrays.mergeSort of the
Sun JDK. How would you go about testing, that the insertion sort is
used in the correct cases?

Is there any specific tool available for JUnit (or any other) to test
performance automatically? Can we test that the code passes at certain
points or that certain methods have been called? If not, is there a
nice way to check in the log for a sign indicating the use of the
optimization?

Best regards
Phil
PS: Does Sun remove all log statements from its JDK src before
distribution or are they developing without logs (and should thus be
considered half-gods)?



--- Code from Arrays.java in Sun JDK 1.6 ---

private static void mergeSort(Object[] src,
Object[] dest,
int low,
int high,
int off) {
int length = high - low;

// Insertion sort on smallest arrays
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest, j, j-1);
return;
}

// Recursively sort halves of dest into src
int destLow = low;
int destHigh = high;
low += off;
high += off;
int mid = (low + high) >>> 1;
mergeSort(dest, src, low, mid, -off);
mergeSort(dest, src, mid, high, -off);

// If list is already sorted, just copy from src to dest. This is
an
// optimization that results in faster sorts for nearly ordered
lists.
if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
}

// Merge sorted halves (now in src) into dest
for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])
<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
.



Relevant Pages

  • Re: What is the wrong with this SQL?
    ... Insert a "GO" before the alter table lines ... > create table dest ... > (col1 int ... > insert into src values ...
    (microsoft.public.sqlserver.programming)
  • Re: Implementing my own memcpy
    ... > src, int bytes) { ... > Now if I want to copy the bytes from src to dest, ... Are you trying to achieve the exact same behaviour as memcpy? ...
    (comp.lang.c)
  • Re: Function call evaluation order
    ... char *strcpy(char *dest, char *src) ... dest and src are different objects. ... int main ...
    (microsoft.public.vc.language)
  • Re: Implementing my own memcpy
    ... I am trying to simulate a memcpy like this void* mem_cpy(void* dest, void* src, int bytes) ... Now if I want to copy the bytes from src to dest, how do I copy these bytes. ... then don't allocate memory - memcpy requires that the memory is already allocated. ... Also the type of the third parameter is size_t not int - this requires that you include stddef.h. ...
    (comp.lang.c)
  • Re: Range query optimization help?
    ... (src, amp, asof) ... >unique clustered index asof_src on MeterEvent ... >table StdAmp, minamp int, maxamp int, stdval int) ...
    (microsoft.public.sqlserver.programming)