Last active
August 29, 2015 13:59
-
-
Save alexandreaquiles/10475548 to your computer and use it in GitHub Desktop.
Comparando implementações que obtem os dias úteis de um ano de maneira iterativa, com streams/flatMap e com streams/collectToMap. Uma medição simples de 100 iterações em um i5 com 4 processadores teve o seguinte resultado: iterativo ~ 47 ms, com flatMap ~ 77 ms, com collect toMap ~ 51 ms.
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
import java.time.DayOfWeek; | |
import java.time.Month; | |
import java.time.YearMonth; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class DiasUteisComCollectToMap { | |
public static void comCollectToMap(int ano) { | |
Stream.of(Month.values()) | |
.map(month -> YearMonth.of(ano, month)) | |
.collect(Collectors.toMap(Function.identity(), | |
ym -> Stream.iterate(ym.atDay(1), date -> date.plusDays(1)).limit(ym.lengthOfMonth()) | |
.filter(date -> !date.getDayOfWeek().equals(DayOfWeek.SATURDAY) && !date.getDayOfWeek().equals(DayOfWeek.SUNDAY)) | |
.collect(Collectors.toList()))); | |
} | |
} |
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
import java.time.DayOfWeek; | |
import java.time.Month; | |
import java.time.YearMonth; | |
import java.util.stream.Collectors; | |
import java.util.stream.Stream; | |
public class DiasUteisComFlatMap { | |
public static void comFlatMap(int ano) { | |
Stream.of(Month.values()) | |
.map(month -> YearMonth.of(ano, month)) | |
.flatMap(ym -> Stream.iterate(ym.atDay(1), date -> date.plusDays(1)).limit(ym.lengthOfMonth())) | |
.filter(date -> !date.getDayOfWeek().equals(DayOfWeek.SATURDAY) && !date.getDayOfWeek().equals(DayOfWeek.SUNDAY)) | |
.collect(Collectors.groupingBy(date -> YearMonth.of(date.getYear(), date.getMonth()))); | |
} | |
} |
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
import java.time.DayOfWeek; | |
import java.time.LocalDate; | |
import java.time.Month; | |
import java.time.YearMonth; | |
import java.util.ArrayList; | |
import java.util.LinkedHashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class DiasUteisIterativo { | |
public static void iterativo(int ano) { | |
Map<YearMonth, List<LocalDate>> iterativo = new LinkedHashMap<>(); | |
for(Month mes : Month.values()) { | |
YearMonth anoMes = YearMonth.of(ano, mes); | |
List<LocalDate> diasUteisDoAno = new ArrayList<>(); | |
for(int dia=1; dia <= anoMes.lengthOfMonth(); dia++){ | |
LocalDate date = anoMes.atDay(dia); | |
if(!date.getDayOfWeek().equals(DayOfWeek.SATURDAY) && !date.getDayOfWeek().equals(DayOfWeek.SUNDAY)){ | |
diasUteisDoAno.add(date); | |
} | |
} | |
iterativo.put(anoMes, diasUteisDoAno); | |
} | |
} | |
} |
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
import java.time.Duration; | |
import java.time.Instant; | |
public class MedidorDeTempo { | |
public static long mede(Runnable algoritmo){ | |
Instant inicio = Instant.now(); | |
for(int i = 0; i < 100; i++){ | |
algoritmo.run(); | |
} | |
Instant fim = Instant.now(); | |
return Duration.between(inicio, fim).toMillis(); | |
} | |
} |
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
import java.util.stream.Stream; | |
import static DiasUteisIterativo.*; | |
import static DiasUteisComFlatMap.*; | |
import static DiasUteisComCollectToMap.*; | |
public class Principal { | |
public static void main(String[] args) { | |
Stream.<Runnable>of( | |
() -> iterativo(2014), // ~ 47 ms | |
() -> comFlatMap(2014), // ~ 77 ms | |
() -> comCollectToMap(2014)) // ~ 51 ms | |
.map(MedidorDeTempo::mede) | |
.forEach(System.out::println); | |
//by @paulo_caelum | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment