Skip to content

Instantly share code, notes, and snippets.

@hugithordarson
Last active August 29, 2015 13:57
Show Gist options
  • Save hugithordarson/9819847 to your computer and use it in GitHub Desktop.
Save hugithordarson/9819847 to your computer and use it in GitHub Desktop.
package sam;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import com.webobjects.appserver.WOActionResults;
import com.webobjects.appserver.WORequest;
import com.webobjects.appserver.WOResponse;
import com.webobjects.eocontrol.EOEditingContext;
import com.webobjects.eocontrol.EOFetchSpecification;
import com.webobjects.foundation.NSArray;
import com.webobjects.foundation.NSDictionary;
import com.webobjects.foundation.NSMutableArray;
import concept.data.SWTransaction;
import er.extensions.appserver.ERXDirectAction;
public class LoopsAction extends ERXDirectAction {
public LoopsAction(WORequest r) {
super(r);
}
@Override
public WOActionResults defaultAction() {
EOEditingContext ec = session().defaultEditingContext();
EOFetchSpecification fs = new EOFetchSpecification( SWTransaction.ENTITY_NAME, null, null );
fs.setFetchLimit( 75000 );
fs.setFetchesRawRows( true );
fs.setRawRowKeyPaths( new NSArray<> ( SWTransaction.ACTION_KEY ) );
NSArray<NSDictionary> unfiltered = ec.objectsWithFetchSpecification(fs);
int iterations = 100;
long forEachTotal = 0;
long streamTotal = 0;
long parallelStreamTotal = 0;
long qualifierTotal = 0;
for( int i = iterations ; i>0 ; i-- ) {
forEachTotal += forEach(unfiltered);
streamTotal += stream( unfiltered );
parallelStreamTotal += parallelStream(unfiltered);
qualifierTotal += qualifier(unfiltered);
}
System.out.println( "-" );
System.out.println( "forEach ms: " + TimeUnit.NANOSECONDS.toMillis( forEachTotal ) );
System.out.println( "stream ms: " + TimeUnit.NANOSECONDS.toMillis( streamTotal ) );
System.out.println( "parallelStream ms: " + TimeUnit.NANOSECONDS.toMillis( parallelStreamTotal ) );
System.out.println( "qualifier ms: " + TimeUnit.NANOSECONDS.toMillis( qualifierTotal ) );
System.out.println( "-" );
return new WOResponse();
}
private static long forEach( NSArray<NSDictionary> unfiltered ) {
long start = System.nanoTime();
NSMutableArray<NSDictionary> filtered = new NSMutableArray<>();
for( NSDictionary d : unfiltered ) {
if( "I".equals( d.objectForKey( SWTransaction.ACTION_KEY ) ) ) {
filtered.addObject( d );
}
}
long time = System.nanoTime() - start;
filtered.size();
return time;
}
private static long stream( NSArray<NSDictionary> unfiltered ) {
long start = System.nanoTime();
List<NSDictionary> filtered = unfiltered.stream()
.filter(c -> "I".equals( c.objectForKey(SWTransaction.ACTION_KEY) ) )
.collect(Collectors.toList());
long time = System.nanoTime() - start;
filtered.size();
return time;
}
private static long parallelStream( NSArray<NSDictionary> unfiltered ) {
long start = System.nanoTime();
List<NSDictionary> filtered = unfiltered.parallelStream()
.filter(c -> "I".equals( c.objectForKey(SWTransaction.ACTION_KEY) ) )
.collect(Collectors.toList());
long time = System.nanoTime() - start;
filtered.size();
return time;
}
private static long qualifier( NSArray<NSDictionary> unfiltered ) {
long start = System.nanoTime();
List<NSDictionary> filtered = SWTransaction.ACTION.eq("I").filtered( unfiltered );
long time = System.nanoTime() - start;
filtered.size();
return time;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment