Created
March 30, 2016 11:49
-
-
Save apangin/af70e39b25e578d13484e937c66c7985 to your computer and use it in GitHub Desktop.
JNI array access benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <jni.h> | |
static int sum(jbyte* array, int length) { | |
int result = 0; | |
int i; | |
for (i = 0; i < length; i++) { | |
result += array[i]; | |
} | |
return result; | |
} | |
JNIEXPORT jint JNICALL | |
Java_bench_Natives_arrayRegionImpl(JNIEnv* env, jclass cls, jbyteArray array) { | |
static jbyte buf[1048576]; | |
jint length = (*env)->GetArrayLength(env, array); | |
(*env)->GetByteArrayRegion(env, array, 0, length, buf); | |
return sum(buf, length); | |
} | |
JNIEXPORT jint JNICALL | |
Java_bench_Natives_arrayElementsImpl(JNIEnv* env, jclass cls, jbyteArray array) { | |
jboolean isCopy; | |
jint length = (*env)->GetArrayLength(env, array); | |
jbyte* buf = (*env)->GetByteArrayElements(env, array, &isCopy); | |
jint result = sum(buf, length); | |
(*env)->ReleaseByteArrayElements(env, array, buf, JNI_ABORT); | |
return result; | |
} | |
JNIEXPORT jint JNICALL | |
Java_bench_Natives_arrayElementsCriticalImpl(JNIEnv* env, jclass cls, jbyteArray array) { | |
jboolean isCopy; | |
jint length = (*env)->GetArrayLength(env, array); | |
jbyte* buf = (jbyte*) (*env)->GetPrimitiveArrayCritical(env, array, &isCopy); | |
jint result = sum(buf, length); | |
(*env)->ReleasePrimitiveArrayCritical(env, array, buf, JNI_ABORT); | |
return result; | |
} | |
JNIEXPORT jint JNICALL | |
Java_bench_Natives_javaCriticalImpl(JNIEnv* env, jclass cls, jbyteArray array) { | |
return Java_bench_Natives_arrayElementsCriticalImpl(env, cls, array); | |
} | |
JNIEXPORT jint JNICALL | |
JavaCritical_bench_Natives_javaCriticalImpl(jint length, jbyte* buf) { | |
return sum(buf, length); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bench; | |
import org.openjdk.jmh.annotations.*; | |
@State(Scope.Benchmark) | |
public class Natives { | |
byte[] array; | |
@Param({"16", "256", "4096", "65536", "1048576"}) | |
int length; | |
@Setup | |
public void setup() { | |
array = new byte[length]; | |
} | |
@Benchmark | |
public int arrayRegion() { | |
return arrayRegionImpl(array); | |
} | |
@Benchmark | |
public int arrayElements() { | |
return arrayElementsImpl(array); | |
} | |
@Benchmark | |
public int arrayElementsCritical() { | |
return arrayElementsCriticalImpl(array); | |
} | |
@Benchmark | |
public int javaCritical() { | |
return javaCriticalImpl(array); | |
} | |
static native int arrayRegionImpl(byte[] array); | |
static native int arrayElementsImpl(byte[] array); | |
static native int arrayElementsCriticalImpl(byte[] array); | |
static native int javaCriticalImpl(byte[] array); | |
static { | |
System.loadLibrary("natives"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment