Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better MovieV3 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 57 additions & 2 deletions src/org/paumard/jdk8/MovieV3.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IntSummaryStatistics;
import java.util.Map;
Expand All @@ -12,10 +13,12 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javafx.util.Pair;
import org.paumard.model.Actor;
import org.paumard.model.Movie;

Expand Down Expand Up @@ -83,7 +86,59 @@ public static void main(String... args) throws Exception {
.summaryStatistics() ;
System.out.println("From " + stats.getMin() + " to " + stats.getMax()) ;

long debut = System.currentTimeMillis() ;
long debut = System.currentTimeMillis();

// Duo actor who played in more movies => simple and faster
// Time on my machine :
// - New algo : ~100 seconds
// - New algo old fashion : ~50 seconds (just foreach and map, no parallel)
// - Old algo : 200 seconds
movies.stream().flatMap(
movie -> movie.actors().parallelStream().flatMap(actor1 ->
movie.actors().stream().filter(actor2 -> !actor1.equals(actor2)).map(actor2 ->
new Pair<>(actor1, actor2)
))
).collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting()
)).entrySet().stream().max(Map.Entry.comparingByValue()).ifPresent(result ->
System.out.println("Most seen actor duo = " + result)
);

long fin = System.currentTimeMillis() ;
System.out.println("T = " + (fin - debut) + "ms");


debut = System.currentTimeMillis() ;
// Same algo with old fashion
Map<Pair<Actor, Actor>, AtomicLong> moviesByPairOfActors = new HashMap<>();
long max = 0;
Pair<Actor, Actor> bestPairOfActors = null;
for (Movie movie : movies) {
for (Actor actor1 : movie.actors()) {
for (Actor actor2 : movie.actors()) {
if (!actor1.equals(actor2)) {
Pair<Actor, Actor> pair = new Pair<>(actor1, actor2);
if (!moviesByPairOfActors.containsKey(pair)) {
moviesByPairOfActors.put(pair, new AtomicLong(0));
}

long count = moviesByPairOfActors.get(pair).incrementAndGet();
if (count > max) {
max = count;
bestPairOfActors = pair;
}
}
}
}
}
System.out.println("Most seen actor duo = " + bestPairOfActors + "=" + max) ;
fin = System.currentTimeMillis() ;
moviesByPairOfActors.clear();
System.out.println("T = " + (fin - debut) + "ms");


debut = System.currentTimeMillis() ;

// Duo actor who played in more movies
NavigableSet<Actor> keyActors =
Expand Down Expand Up @@ -173,7 +228,7 @@ public static void main(String... args) throws Exception {
.get() ;
System.out.println("Most seen actor duo = " + e) ;

long fin = System.currentTimeMillis() ;
fin = System.currentTimeMillis() ;
System.out.println("T = " + (fin - debut) + "ms");
}
}