Created
May 15, 2016 21:10
-
-
Save lutovich/941d6f92813c2c34e660ba4fe687d330 to your computer and use it in GitHub Desktop.
Measures performance of spevialized vs reflection based array equality check
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 org.neo4j.testing.array_equality; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.BenchmarkMode; | |
import org.openjdk.jmh.annotations.Fork; | |
import org.openjdk.jmh.annotations.Measurement; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.OutputTimeUnit; | |
import org.openjdk.jmh.annotations.Param; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.Warmup; | |
import java.lang.reflect.Array; | |
import java.util.Arrays; | |
import java.util.Objects; | |
import java.util.concurrent.ThreadLocalRandom; | |
import java.util.concurrent.TimeUnit; | |
@Fork( value = 1, jvmArgs = {"-Xmx8g", "-XX:+UseG1GC"} ) | |
@State( Scope.Benchmark ) | |
@BenchmarkMode( Mode.Throughput ) | |
@OutputTimeUnit( TimeUnit.MILLISECONDS ) | |
@Warmup( iterations = 25, time = 1, timeUnit = TimeUnit.SECONDS ) | |
@Measurement( iterations = 25, time = 1, timeUnit = TimeUnit.SECONDS ) | |
public class ArrayEqualityBenchmark | |
{ | |
private static final int ARRAYS_COUNT = 10_000; | |
@Param( {"1", "10", "100", "1000", "100000"} ) | |
public int elementsCount; | |
private Object[] arrays; | |
@Setup | |
public void setUp() | |
{ | |
this.arrays = generateArrays( ARRAYS_COUNT, elementsCount ); | |
} | |
@Benchmark | |
public boolean specialized() | |
{ | |
int[] array1 = randomArray(); | |
int[] array2 = randomArray(); | |
return ArrayEquality.specialized( array1, array2 ); | |
} | |
@Benchmark | |
public boolean reflective() | |
{ | |
int[] array1 = randomArray(); | |
int[] array2 = randomArray(); | |
return ArrayEquality.reflective( array1, array2 ); | |
} | |
private int[] randomArray() | |
{ | |
return (int[]) arrays[ThreadLocalRandom.current().nextInt( arrays.length )]; | |
} | |
private static Object[] generateArrays( int arraysCount, int elementsCount ) | |
{ | |
Object[] arrays = new Object[arraysCount]; | |
int[] prototype = generateArray( elementsCount ); | |
for ( int i = 0; i < arraysCount; i++ ) | |
{ | |
arrays[i] = createArray( prototype ); | |
} | |
return arrays; | |
} | |
private static int[] generateArray( int elementsCount ) | |
{ | |
int[] array = new int[elementsCount]; | |
for ( int i = 0; i < elementsCount; i++ ) | |
{ | |
array[i] = ThreadLocalRandom.current().nextInt(); | |
} | |
return array; | |
} | |
private static int[] createArray( int[] prototype ) | |
{ | |
int[] array = Arrays.copyOf( prototype, prototype.length ); | |
array[array.length - 1] = ThreadLocalRandom.current().nextInt(); | |
return array; | |
} | |
} | |
class ArrayEquality | |
{ | |
public static boolean specialized( int[] expected, int[] actual ) | |
{ | |
if ( expected == actual ) | |
{ | |
return true; | |
} | |
if ( expected == null || actual == null ) | |
{ | |
return false; | |
} | |
int length = expected.length; | |
if ( actual.length != length ) | |
{ | |
return false; | |
} | |
for ( int i = 0; i < length; i++ ) | |
{ | |
if ( expected[i] != actual[i] ) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
public static boolean reflective( Object expected, Object actual ) | |
{ | |
if ( expected == actual ) | |
{ | |
return true; | |
} | |
if ( expected == null || actual == null ) | |
{ | |
return false; | |
} | |
int length = Array.getLength( expected ); | |
if ( Array.getLength( actual ) != length ) | |
{ | |
return false; | |
} | |
for ( int i = 0; i < length; i++ ) | |
{ | |
if ( !Objects.equals( Array.get( expected, i ), Array.get( actual, i ) ) ) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment