Last active
June 3, 2016 02:27
-
-
Save saudet/4de601b9ef25d78b3474c997c368a870 to your computer and use it in GitHub Desktop.
Testing overhead and crashability of passing a `Pointer` vs its address to native methods
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
void processLongPtr(float* ptr, long long size) { | |
for (int i = 0; i < size; i++) { | |
ptr[i] = i; | |
} | |
} | |
void processPointer(float* ptr, long long size) { | |
for (int i = 0; i < size; i++) { | |
ptr[i] = i; | |
} | |
} |
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
import java.util.concurrent.*; | |
import org.bytedeco.javacpp.*; | |
import org.bytedeco.javacpp.annotation.*; | |
@Platform(include="testgc.h") | |
public class TestGC { | |
static { Loader.load(); } | |
static native void processLongPtr(@Cast("float*") long ptr, long size); | |
static native void processPointer(FloatPointer ptr, long size); | |
static Runnable longPtrRunnable = new Runnable() { | |
@Override public void run() { | |
for (int i = 0; i < 1000; i++) { | |
FloatPointer ptr = new FloatPointer(1000000); | |
processLongPtr(ptr.address(), ptr.capacity()); | |
} | |
} | |
}; | |
static Runnable pointerRunnable = new Runnable() { | |
@Override public void run() { | |
for (int i = 0; i < 1000; i++) { | |
FloatPointer ptr = new FloatPointer(1000000); | |
processPointer(ptr, ptr.capacity()); | |
} | |
} | |
}; | |
public static void main(String[] args) throws Exception { | |
System.out.println("Computing call overhead..."); | |
FloatPointer nullPtr = new FloatPointer((Pointer)null); | |
int count = 100000000; | |
for (int i = 0; i < count; i++) { | |
processLongPtr(0, 0); | |
processPointer(nullPtr, 0); | |
} | |
long time0 = System.nanoTime(); | |
for (int i = 0; i < count; i++) { | |
processLongPtr(0, 0); | |
} | |
long time1 = System.nanoTime(); | |
for (int i = 0; i < count; i++) { | |
processPointer(nullPtr, 0); | |
} | |
long time2 = System.nanoTime(); | |
System.out.println("processLongPtr(): " + (time1 - time0) / count + " ns"); | |
System.out.println("processPointer(): " + (time2 - time1) / count + " ns"); | |
System.out.println("Testing crashability... (Make sure to execute with -XX:+UseConcMarkSweepGC)"); | |
System.out.println("processPointer()..."); | |
ExecutorService pool = Executors.newFixedThreadPool(10); | |
for (int i = 0; i < 100; i++) { | |
pool.execute(pointerRunnable); | |
} | |
pool.shutdown(); | |
pool.awaitTermination(1, TimeUnit.HOURS); | |
System.out.println("processLongPtr()..."); | |
pool = Executors.newFixedThreadPool(10); | |
for (int i = 0; i < 100; i++) { | |
pool.execute(longPtrRunnable); | |
} | |
pool.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment