Created
July 28, 2024 19:13
-
-
Save alexaleluia12/bbb2ef1f3614e91bc998abb44266e988 to your computer and use it in GitHub Desktop.
Prova de conceito (poc) Stream - java/kotlin
This file contains hidden or 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
# Perguntas: | |
- o uso de stream/array deixa mais lento, consome mais recursos ? | |
- em kotlin como seria ? | |
* tarefa: verificar se existe um elemento null num array de tipo T | |
# Respostas: | |
Em java o uso de Stream e reference methods é até um pouco mais rápido. | |
O consumo de memório entra as duas formas é igual. | |
Em Kotlin não tem necesside de usar um tipo espeical como Stream pois no | |
próprio Array já é possivel adicionar operações de verificação e transformações nos elementos. | |
Notei que o consumo de memória em kt foi menor que java, mas o tempo foi maior. |
This file contains hidden or 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 com.alexaleluia12; | |
import java.time.Duration; | |
import java.time.LocalTime; | |
import java.util.Objects; | |
import java.util.stream.Stream; | |
public class PocDesempenho { | |
// o uso de stream/array deixa mais lento, consome mais recursos ? | |
// em kotlin como seria ? | |
// tarefa: verificar se existem um elemento num num lista de tipo T | |
public static void main(String[] args) { | |
long beforeUsedMem=Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory(); | |
var now = LocalTime.now(); | |
verifyForlst(); | |
var after = LocalTime.now(); | |
var diff = Duration.between(after, now); | |
long afterUsedMem=Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory(); | |
long usedMemory = afterUsedMem - beforeUsedMem; | |
System.out.println("time operation: " + diff.getNano() / 1000_000.0f + ", nano:" + diff.getNano() + " mem: " + usedMemory); | |
} | |
public static void verifyForstream() { | |
/* | |
Com stream foi mais rapido tempo de 972.5919 Milisegundos | |
memoria: 638296 | |
*/ | |
Stream<Integer> l1 = Stream.of(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50); | |
Stream<Integer> l2 = Stream.of(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 9, 3); | |
Stream<Integer> l3 = Stream.of(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50); | |
Stream<Integer> l4 = Stream.of(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50); | |
Stream<Integer> l5 = Stream.of(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50); | |
Stream<Integer> l6 = Stream.of(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50); | |
Stream<Integer> l7 = Stream.of(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50); | |
Stream<Integer> l8 = Stream.of(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50); | |
Stream<Integer> l9 = Stream.of(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50); | |
Stream<Integer> lA = Stream.of(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50); | |
Stream[] td = new Stream[]{l1, l2, l3, l4, l5, l6, l7, l8, l9, lA}; | |
for (Stream e: td) { | |
Worker.verifyNull(e); | |
} | |
} | |
public static void verifyForlst() { | |
/* | |
o tempo de uma lista e todas essas eh quase o msm: 998.6295 Milisegundos | |
memory: 638296 | |
*/ | |
Integer[] l1 = {1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50}; | |
Integer[] l2 = {1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 9, 3,}; | |
Integer[] l3 = {1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50}; | |
Integer[] l4 = {1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50}; | |
Integer[] l5 = {1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50}; | |
Integer[] l6 = {1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50}; | |
Integer[] l7 = {1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50}; | |
Integer[] l8 = {1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50}; | |
Integer[] l9 = {1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50}; | |
Integer[] lA = {1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50}; | |
Integer[][] td = {l1, l2, l3, l4, l5, l6, l7, l8, l9, lA}; | |
for (Integer[] e: td) { | |
Worker.verifyNull(e); | |
} | |
} | |
} | |
class Worker { | |
public static <T> boolean verifyNull(T[] lst) { | |
for (T e: lst) { | |
if (e == null) | |
return true; | |
} | |
return false; | |
} | |
public static <T> boolean verifyNull(Stream<T> lst) { | |
return lst.anyMatch(Objects::isNull); | |
} | |
} |
This file contains hidden or 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 com.alexaleluia12 | |
import java.time.Duration | |
import java.time.LocalTime | |
import java.util.* | |
fun main() { | |
val beforeUsedMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() | |
val now = LocalTime.now() | |
verifyFor() | |
val after = LocalTime.now() | |
val diff = Duration.between(after, now) | |
val afterUsedMem = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() | |
val usedMemory = afterUsedMem - beforeUsedMem | |
println("time operation: " + diff.nano / 1000000.0f + ", nano:" + diff.nano + " mem: " + usedMemory) | |
} | |
fun verifyFn() { | |
// time operation: 995.1205 ms, nano seg:995120542 mem: 545640 | |
val l1 = arrayOf(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,500) | |
val l2 = arrayOf(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 9, 3) | |
val l3 = arrayOf(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,43) | |
val l4 = arrayOf(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,543) | |
val l5 = arrayOf(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,43) | |
val l6 = arrayOf(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,45) | |
val l7 = arrayOf(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,5) | |
val l8 = arrayOf(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50) | |
val l9 = arrayOf(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50) | |
val lA = arrayOf(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50) | |
val td = arrayOf(l1, l2, l3, l4, l5, l6, l7, l8, l9, lA) | |
for (k in td) { | |
vefiyNullViaFn(k) | |
} | |
} | |
fun verifyFor() { | |
// time operation: 996.43567, nano:996435633 mem: 563744 | |
val l1 = arrayOf(1, 2, 3, 4, 5,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,500) | |
val l2 = arrayOf(1, 2, 3, 4, 51,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 9, 3) | |
val l3 = arrayOf(1, 2, 3, 4, 52,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,43) | |
val l4 = arrayOf(1, 2, 3, 4, 53,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,543) | |
val l5 = arrayOf(1, 2, 3, 4, 54,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,43) | |
val l6 = arrayOf(1, 2, 3, 4, 55,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,45) | |
val l7 = arrayOf(1, 2, 3, 4, 56,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,5) | |
val l8 = arrayOf(1, 2, 3, 4, 57,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50) | |
val l9 = arrayOf(1, 2, 3, 4, 58,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50) | |
val lA = arrayOf(1, 2, 3, 4, 59,6,9, 2, 2, 3,323,523,371,1,1,1,1,12,3,3,34,54,45,3,5,23,2,123,6,null, 3,50) | |
val td = arrayOf(l1, l2, l3, l4, l5, l6, l7, l8, l9, lA) | |
for (k in td) { | |
vefiyNullViaFor(k) | |
} | |
} | |
fun <T> vefiyNullViaFn(list: Array<T>): Boolean { | |
return list.any(Objects::isNull) | |
} | |
fun <T> vefiyNullViaFor(list: Array<T>): Boolean { | |
for (e in list) { | |
if (e == null) | |
return true | |
} | |
return false | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment