Skip to content

Instantly share code, notes, and snippets.

@alexandreaquiles
Last active April 8, 2022 14:52
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexandreaquiles/10609788 to your computer and use it in GitHub Desktop.
Save alexandreaquiles/10609788 to your computer and use it in GitHub Desktop.
Programa (em Java 8 vs grep) que pega as informações disponíveis no Portal da Transparência e sumariza a carga horária dos servidores públicos federais. Fonte dos dados: https://www.portaltransparencia.gov.br/download-de-dados/servidores/201402_Servidores_SIAPE
#!/bin/sh
grep -a -o -E -w '[0-9]{2}(,[0-9])? HORAS SEMANAIS|DEDICACAO EXCLUSIVA' ~/Downloads/201402_Servidores/20140228_Cadastro.csv | sort | uniq -c
package cargaHorariaDosServidores;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class CargaHorariaServidores {
private static final Pattern HORAS = Pattern
.compile(".*([0-9]{2}(,[0-9])? HORAS SEMANAIS|DEDICACAO EXCLUSIVA).*");
public static void main(String[] args) throws IOException {
Path caminho = Paths.get(System.getProperty("user.home"),
"Downloads/201402_Servidores/20140228_Cadastro.csv");
Files.lines(caminho, StandardCharsets.ISO_8859_1)
.map(linha -> {
Matcher matcher = HORAS.matcher(linha);
return matcher.matches() ? matcher.group(1) : "";
})
.filter(horaSemanal -> !horaSemanal.isEmpty())
.sorted()
.collect(
Collectors.groupingBy(Function.identity(),
LinkedHashMap::new, Collectors.counting()))
.forEach((k, v) -> System.out.println(v + "\t" + k));
}
}
$ wc -m CargaHorariaServidores.java
1127 CargaHorariaServidores.java
$ wc -m carga_horaria_servidores.sh
150 carga_horaria_servidores.sh
Ou seja, o Java tem 7x mais caracteres.
$ time ./carga_horaria_servidores.sh
real 1m27.790s
user 1m28.832s
sys 0m0.192s
$ time java -jar cargaHorariaServidores.jar
real 0m5.965s
user 0m7.353s
sys 0m0.278s
# com .parallel() logo após o File::lines
$ time java -jar cargaHorariaServidores.jar
real 0m4.223s
user 0m12.669s
sys 0m0.619s
___
Total do grep (user+sys) = 1m29.024s
Total do java (user+sys) = 0m7.631s
Total do java paralelo (user+sys) = 0m13.288s
Ou seja, grep levou cerca de 12x mais tempo.
Já a comparação de streams paralelas com streams sequencias é interessante. Consome 1,7x mais tempo usando recursos do computador (user+sys) mas leva cerca de 30% a menos de tempo para o usuário (real).
Outra coisa interessante de notar é que, mesmo para streams sequencias, parece haver um pouco de paralelismo, já que o tempo para o usuário (real) é menor que o tempo consumindo recursos do computador (user+sys).
69 12 HORAS SEMANAIS
23069 20 HORAS SEMANAIS
2216 24 HORAS SEMANAIS
1052 25 HORAS SEMANAIS
5509 30 HORAS SEMANAIS
2 32,5 HORAS SEMANAIS
1538 36 HORAS SEMANAIS
571777 40 HORAS SEMANAIS
7434 44 HORAS SEMANAIS
9325 60 HORAS SEMANAIS
308 66 HORAS SEMANAIS
115359 DEDICACAO EXCLUSIVA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment