Skip to content

Instantly share code, notes, and snippets.

@deanveloper
Created April 12, 2015 14:36
Show Gist options
  • Save deanveloper/bfc422f66be6e25660a1 to your computer and use it in GitHub Desktop.
Save deanveloper/bfc422f66be6e25660a1 to your computer and use it in GitHub Desktop.
2 Tests involving the speeds of Streams, Parallel Streams, with and without lambdas, and comparing it to doing it the normal way (without streams). It's also worth noting that this only compares .filter() and .map() operations. TL;DR Streams w/o lambdas are fastest on a smaller scale, Streams w/ lambdas are faster on a large scale, doing it the …
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class Tests{
static Random rand = new Random();
static List<Integer> someIntsIdk = new ArrayList<>(10000);
static List<Long> streamTimes = new ArrayList<>(10000);
static List<Long> parallelStreamTimes = new ArrayList<>(10000);
static List<Long> streamNoLambdaTimes = new ArrayList<>(10000);
static List<Long> parallelStreamNoLambdaTimes = new ArrayList<>(10000);
static List<Long> normalWayTimes = new ArrayList<>(10000);
public static void main(String[] args){
System.out.printf("Starting streams with lambdas! ");
for(int i = 0; i < 10000; i++){
putNumbers(1000);
doStreamThing();
}
printAvg(streamTimes);
System.out.printf("Starting parallel streams with lambdas! ");
for(int i = 0; i < 10000; i++){
putNumbers(1000);
doParallelStreamThing();
}
printAvg(parallelStreamTimes);
System.out.printf("Starting streams without lambdas! ");
for(int i = 0; i < 10000; i++){
putNumbers(1000);
doStreamThingNoLambda();
}
printAvg(streamNoLambdaTimes);
System.out.printf("Starting parallel streams without lambdas! ");
for(int i = 0; i < 10000; i++){
putNumbers(1000);
doParallelStreamThingNoLambda();
}
printAvg(parallelStreamNoLambdaTimes);
System.out.printf("Starting normal way! ");
for(int i = 0; i < 10000; i++){
putNumbers(1000);
doNormalThing();
}
printAvg(normalWayTimes);
/*
OUTPUT:
----------------------------------------------------
Starting streams with lambdas! Resulting average: 40469 nanos
Starting parallel streams with lambdas! Resulting average: 42483 nanos
Starting streams without lambdas! Resulting average: 25897 nanos
Starting parallel streams without lambdas! Resulting average: 22150 nanos
Starting normal way! Resulting average: 35980 nanos
----------------------------------------------------
Here, doing the operations seemed to have lambdas with streams be slower than
the normal way of doing it, but streams without lambdas are significantly faster than
doing it the normal way.
Now, let's do the same thing with 10000 numbers instead of 1000!
*/
System.out.printf("Starting streams with lambdas! ");
for(int i = 0; i < 10000; i++){
putNumbers(10000);
doStreamThing();
}
printAvg(streamTimes);
System.out.printf("Starting parallel streams with lambdas! ");
for(int i = 0; i < 10000; i++){
putNumbers(10000);
doParallelStreamThing();
}
printAvg(parallelStreamTimes);
System.out.printf("Starting streams without lambdas! ");
for(int i = 0; i < 10000; i++){
putNumbers(10000);
doStreamThingNoLambda();
}
printAvg(streamNoLambdaTimes);
System.out.printf("Starting parallel streams without lambdas! ");
for(int i = 0; i < 10000; i++){
putNumbers(10000);
doParallelStreamThingNoLambda();
}
printAvg(parallelStreamNoLambdaTimes);
System.out.printf("Starting normal way! ");
for(int i = 0; i < 10000; i++){
putNumbers(10000);
doNormalThing();
}
printAvg(normalWayTimes);
/*
OUTPUT:
----------------------------------------------------
Starting streams with lambdas! Resulting average: 64956 nanos
Starting parallel streams with lambdas! Resulting average: 75635 nanos
Starting streams without lambdas! Resulting average: 91631 nanos
Starting parallel streams without lambdas! Resulting average: 73209 nanos
Starting normal way! Resulting average: 1175437 nanos
----------------------------------------------------
Now, the lambdas are faster than anonymous inner classes! I'm not sure
how this is possible, but I guess it's a thing. Doing it the normal way
is also over 10x slower than using streams. Parallel streams w/ lambdas are actually
slower than streams for some reason, although I'm guessing if you have a computer with
more cores, it will be MUCH faster than normal streams.
*/
}
public static void putNumbers(int size){
someIntsIdk.clear();
int random = rand.nextInt();
for(int i = random; i < random + size; i++){
someIntsIdk.add(i);
}
}
public static void doStreamThing(){
long start = System.nanoTime();
someIntsIdk.stream()
.filter(i -> i > -1)
.map(i -> i)
.collect(Collectors.toList());
streamTimes.add(System.nanoTime() - start);
}
public static void doParallelStreamThing(){
long start = System.nanoTime();
someIntsIdk.parallelStream()
.filter(i -> i > -1)
.map(i -> i)
.collect(Collectors.toList());
parallelStreamTimes.add(System.nanoTime() - start);
}
public static void doStreamThingNoLambda(){
long start = System.nanoTime();
someIntsIdk.stream()
.filter(new Predicate<Integer>(){
@Override
public boolean test(Integer integer){
return integer > -1;
}
})
.map(new Function<Integer, Object>(){
@Override
public Object apply(Integer integer){
return integer;
}
})
.collect(Collectors.toList());
streamNoLambdaTimes.add(System.nanoTime() - start);
}
public static void doParallelStreamThingNoLambda(){
long start = System.nanoTime();
someIntsIdk.parallelStream()
.filter(new Predicate<Integer>(){
@Override
public boolean test(Integer integer){
return integer > -1;
}
})
.map(new Function<Integer, Object>(){
@Override
public Object apply(Integer integer){
return integer;
}
})
.collect(Collectors.toList());
parallelStreamNoLambdaTimes.add(System.nanoTime() - start);
}
public static void doNormalThing(){
long start = System.nanoTime();
for(int i = 0; i < someIntsIdk.size(); i++){
if(someIntsIdk.get(i) <= -1){
someIntsIdk.remove(i);
}
someIntsIdk.set(i, someIntsIdk.get(i));
}
normalWayTimes.add(System.nanoTime() - start);
}
public static void printAvg(List<Long> toGet){
long sum = 0;
for(Long l : toGet){
sum += l;
}
System.out.printf("Resulting average: %d nanos%n%n", sum / toGet.size());
}
}
@sylvia43
Copy link

(TL;DR Streams w/o lambdas are fastest on a smaller scale, Streams w/ lambdas are faster on a large scale, doing it the "normal way" is never most efficient)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment