Created
March 10, 2017 07:07
-
-
Save philipjkim/ac45307ad174c3a85bccbd47220eae1a to your computer and use it in GitHub Desktop.
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 bar.foo; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.ForkJoinPool; | |
import java.util.concurrent.ThreadLocalRandom; | |
import static com.google.common.collect.ImmutableList.toImmutableList; | |
import static java.util.Comparator.*; | |
public class StreamSample { | |
public static void main(String[] args) throws Exception { | |
int cnt = 10000000; | |
List<Dish> menu = new ArrayList<>(); | |
for (int i = 0; i < cnt; i++) { | |
menu.add(Dish.newRandomDish()); | |
} | |
// parallel stream | |
long startTime = System.currentTimeMillis(); | |
menu.parallelStream() | |
.filter(d -> d.getCalorie() < 400) | |
.sorted(comparing(Dish::getCalorie)) | |
.map(Dish::getName) | |
.collect(toImmutableList()); | |
long endTime = System.currentTimeMillis(); | |
System.out.printf("parallelStream elapsed %dms.\n", (endTime - startTime)); | |
ForkJoinPool pool = new ForkJoinPool(4); | |
startTime = System.currentTimeMillis(); | |
pool.submit(() -> { | |
menu.parallelStream() | |
.filter(d -> d.getCalorie() < 400) | |
.sorted(comparing(Dish::getCalorie)) | |
.map(Dish::getName) | |
.collect(toImmutableList()); | |
}).get(); | |
endTime = System.currentTimeMillis(); | |
System.out.printf("parallelStream(4) elapsed %dms.\n", (endTime - startTime)); | |
pool = new ForkJoinPool(8); | |
startTime = System.currentTimeMillis(); | |
pool.submit(() -> { | |
menu.parallelStream() | |
.filter(d -> d.getCalorie() < 400) | |
.sorted(comparing(Dish::getCalorie)) | |
.map(Dish::getName) | |
.collect(toImmutableList()); | |
}).get(); | |
endTime = System.currentTimeMillis(); | |
System.out.printf("parallelStream(8) elapsed %dms.\n", (endTime - startTime)); | |
// stream | |
startTime = System.currentTimeMillis(); | |
menu.stream() | |
.filter(d -> d.getCalorie() < 400) | |
.sorted(comparing(Dish::getCalorie)) | |
.map(Dish::getName) | |
.collect(toImmutableList()); | |
endTime = System.currentTimeMillis(); | |
System.out.printf("stream elapsed %dms.\n", (endTime - startTime)); | |
} | |
} | |
class Dish { | |
private int calorie; | |
private String name; | |
Dish(int calorie) { | |
this.calorie = calorie; | |
this.name = String.format("name%d", calorie); | |
} | |
public int getCalorie() { | |
return calorie; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setCalorie(int calorie) { | |
this.calorie = calorie; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public static Dish newRandomDish() { | |
return new Dish(ThreadLocalRandom.current().nextInt(1, 1000)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment