Created
October 16, 2015 15:06
-
-
Save edefazio/f4badf9aaaece94d0b58 to your computer and use it in GitHub Desktop.
Performance of SWAR query scan verses List Lambda (stream() and parallelStream())
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 swar.subword; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
public class SWAR_v_ListLambda_Perf | |
{ | |
public static class XY | |
{ | |
public final int x; | |
public final int y; | |
public XY( int x, int y ) | |
{ | |
this.x = x; | |
this.y = y; | |
} | |
public boolean bothOdd() | |
{ | |
return ( x & 1 ) == 1 && | |
( y & 1 ) == 1; | |
} | |
} | |
private static final int ROW_COUNT = 100000; | |
private static final int SCAN_ITERATIONS = 1000; | |
public static void main (String[] args) | |
{ | |
/** Generate Random Test Data */ | |
long[] xyCompound = new long[ ROW_COUNT ]; | |
List<XY> xyList = new ArrayList<XY>( ROW_COUNT ); | |
Random r = new Random(); | |
for ( int i = 0; i < ROW_COUNT; i++ ) | |
{ | |
int x = r.nextInt(); | |
int y = r.nextInt(); | |
//we need this & MASK to avoid Java sign extension | |
xyCompound[ i ] = ( x + 0L << 32 ) | ( y & ( -1L >>> 32 ) ); | |
xyList.add( new XY( x, y ) ); | |
} | |
/** lets try a GC BEFORE running the timings*/ | |
System.gc(); | |
timeLambdaScan( xyList, SCAN_ITERATIONS ); | |
timeLambdaParallelScan( xyList, SCAN_ITERATIONS ); | |
timeSWARMask ( xyCompound, SCAN_ITERATIONS ); | |
} | |
public static void timeLambdaScan ( List<XY> xyList, int iterations ) | |
{ | |
int found = 0; | |
long start = System.currentTimeMillis(); | |
found = listLambdaScan(xyList, iterations ); | |
long end = System.currentTimeMillis(); | |
System.out.println ("List Lambda "+(end-start)+"ms for ("+iterations+"x) scanned "+xyList.size()+" found "+found); | |
} | |
public static void timeLambdaParallelScan ( List<XY> xyList, int iterations ) | |
{ | |
int found = 0; | |
long start = System.currentTimeMillis(); | |
found = listLambdaParallelScan(xyList, iterations ); | |
long end = System.currentTimeMillis(); | |
System.out.println ("List Lambda (Parallel) "+(end-start)+"ms for ("+iterations+"x) scanned "+xyList.size()+" found "+found); | |
} | |
public static int listLambdaScan ( List<XY> xyList, int iterations ) | |
{ | |
int found = 0; | |
for (int it = 0; it < iterations; it++ ) | |
{ | |
found+= xyList.stream() | |
.filter(s -> s.bothOdd()) | |
.count(); | |
} | |
return found; | |
} | |
public static int listLambdaParallelScan ( List<XY> xyList, int iterations ) | |
{ | |
int found = 0; | |
for (int it = 0; it < iterations; it++ ) | |
{ | |
found+= xyList.parallelStream() | |
.filter(s -> s.bothOdd()) | |
.count(); | |
} | |
return found; | |
} | |
private static void timeSWARMask ( long[] xyCompound, int iterations ) | |
{ | |
long start = System.currentTimeMillis(); | |
int found = compoundMaskBothOdd( xyCompound, iterations ); | |
long end = System.currentTimeMillis(); | |
System.out.println ("Compound SWAR Bit Mask "+(end-start)+"ms for ("+iterations+"x) scanned "+xyCompound.length+" found "+found); | |
} | |
public static final long BOTH_ODD_MASK = ( 1L << 32 ) + 1; | |
public static int compoundMaskBothOdd(long[] xyCompound, int iterations ) | |
{ | |
int found =0; | |
for (int it=0; it<iterations; it++) | |
{ | |
for (int i=0; i<xyCompound.length; i++) | |
{ | |
if ( ( xyCompound[ i ] & BOTH_ODD_MASK ) == BOTH_ODD_MASK ) | |
{ | |
found++; | |
} | |
} | |
} | |
return found; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compares performance of full row query scan (to check if x,y are BOTH ODD)