Kwker

Use Kwker from Java

kwker.jar holds the class io.kwker.Kwker and the native library for Linux x86-64 and arm64, macOS arm64 and Windows x64. On Java 22 and later it calls the library through the Foreign Function & Memory API; Java 11 to 21 use JNI, whose library the jar carries for Linux x86-64 only. Primitive arrays are sorted in place.

The program

The three slowest requests, without sorting the whole array:

JavaPut kwker.jar on the class path.
import io.kwker.Kwker;

public class Example {
    public static void main(String[] args) {
        double[] latencyMs = {12.1, 250.4, 8.9, 97.0, 310.2, 15.5, 260.0};
        int[] positions = Kwker.topK(latencyMs, 3, Kwker.DESCENDING);
        for (int i : positions) System.out.println(latencyMs[i] + " at " + i);
    }
}
Output
310.2 at 4
260.0 at 6
250.4 at 1

Compile and run

ShellOn your machine.
javac -cp kwker.jar Example.java
java -cp kwker.jar:. Example                 # on Windows: -cp "kwker.jar;."

On Java 22 and later, add --enable-native-access=ALL-UNNAMED to java to silence the native-access warning.

Errors

Invalid arguments throw IllegalArgumentException, and null arrays NullPointerException; the arrays are left unchanged.

Next steps