Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Sequence x Iterable em Kotlin

Sequence x Iterable em Kotlin

Nessa apresentação trouxe a diferença do processamento de coleções em Kotlin entre Iterable e Sequence.
Além da comparação da performance entre as duas formas utilizando Jetpack Benchmark library.

Angélica Oliveira

May 28, 2020
Tweet

More Decks by Angélica Oliveira

Other Decks in Programming

Transcript

  1. Vamos imaginar uma lista de itens To-do Precisamos de um

    método que retorne apenas os itens marcados como feitos ordenados pelo nome 6
  2. 7 List<TodoItem> getDoneItemOrderedTitle() { List<TodoItem> resultList = new ArrayList<>(); for(TodoItem

    todoItem: todoItemList) { if(todoItem.getDone()) { resultList.add(todoItem); } } Collections.sort(resultList, new Comparator<TodoItem>() { @Override public int compare(TodoItem todoItem1, TodoItem todoItem2) { return todoItem1.getName().compareTo(todoItem2.getName()); } }); return resultList; }
  3. Iterable ▪ Cada operação retorna uma lista intermediária Diferentes formas

    de executar operações Sequence ▪ Collection retornada apenas na operação final 12
  4. Iterable ▪ Cada operação retorna uma lista intermediária ▪ Todas

    as operações são executadas na lista completa Diferentes formas de executar operações Sequence ▪ Collection retornada apenas na operação final ▪ Todas operações são executadas, um item por vez 13
  5. 14 fun getFirstFromList() { LIST .map { println("map $it"); it

    * 5 } .filter { println("filter $it"); it % 2 == 0 } .take(1) .forEach { println("list $it") } }
  6. 23 fun getFirstFromList() { LIST .map { println("map $it"); it

    * 5 } .filter { println("filter $it"); it % 2 == 0 } .take(1) .forEach { println("list $it") } }
  7. 24 fun getFirstFromSequence() { SEQUENCE .map { println("map $it"); it

    * 5 } .filter { println("filter $it"); it % 2 == 0 } .take(1) .forEach { println("sequence $it") } }
  8. 28 fun getFirstFromSequence() { SEQUENCE .map { println("map $it"); it

    * 5 } .filter { println("filter $it"); it % 2 == 0 } .take(1) .forEach { println("sequence $it") } }
  9. Jetpack Benchmark ▪ Comparação de performance de código de aplicativos

    Android ▪ Execução de testes instrumentados 31
  10. Jetpack Benchmark ▪ Comparação de performance de código de aplicativos

    Android ▪ Execução de testes instrumentados ▪ Resultados no output do Android Studio 32
  11. Implementar testes instrumentados 35 @RunWith(AndroidJUnit4::class) class CompareIterableSequenceBenchmarkTest { @get:Rule val

    benchmarkRule = BenchmarkRule() @Test fun benchmarkListOperations() = benchmarkRule.measureRepeated { getFirstFromList(LARGE_LIST) } @Test fun benchmarkSequenceOperations() = benchmarkRule.measureRepeated { getFirstFromSequence(LARGE_SEQUENCE) } }
  12. However, the lazy nature of sequences adds some overhead which

    may be significant when processing smaller collections or doing simpler computations. Hence, you should consider both Sequence and Iterable and decide which one is better for your case. 39
  13. Conclusões ▪ Se tiver dúvida do que usar: □ Meça

    e compare ▪ Você pode usar a biblioteca Jetpack Benchmark pra isso! 42
  14. Fontes ▪ Livro Effective Kotlin (Marcin Moskala) ▪ Documentação oficial

    Kotlin e Android Developers ▪ Template da apresentação SlidesCarnival 43
  15. Obrigada! Alguma pergunta? Você pode me achar em: ▪ Twitter:

    @AngOliveiraa ▪ Linkedin: @angelica-oliv 45
  16. Instructions for use EDIT IN POWERPOINT® Click on the button

    under the presentation preview that says "Download as PowerPoint template". You will get a .pptx file that you can edit in PowerPoint. Remember to download and install the fonts used in this presentation (you’ll find the links to the font files needed in the Presentation design slide) More info on how to use this template at www.slidescarnival.com/help-use-presentation-template This template is free to use under Creative Commons Attribution license. You can keep the Credits slide or mention SlidesCarnival and other resources used in a slide footer. EDIT IN GOOGLE SLIDES Click on the button under the presentation preview that says "Use as Google Slides Theme". You will get a copy of this document on your Google Drive and will be able to edit, add or delete slides. You have to be signed into your Google account. 46
  17. Instructions for use EDIT IN POWERPOINT® Click on the button

    under the presentation preview that says "Download as PowerPoint template". You will get a .pptx file that you can edit in PowerPoint. Remember to download and install the fonts used in this presentation (you’ll find the links to the font files needed in the Presentation design slide) More info on how to use this template at www.slidescarnival.com/help-use-presentation-template This template is free to use under Creative Commons Attribution license. You can keep the Credits slide or mention SlidesCarnival and other resources used in a slide footer. EDIT IN GOOGLE SLIDES Click on the button under the presentation preview that says "Use as Google Slides Theme". You will get a copy of this document on your Google Drive and will be able to edit, add or delete slides. You have to be signed into your Google account. 47
  18. Quotations are commonly printed as a means of inspiration and

    to invoke philosophical thoughts from the reader. 49
  19. This is a slide title ▪ Here you have a

    list of items ▪ And some text ▪ But remember not to overload your slides with content Your audience will listen to you or read the content, but won’t do both. 50
  20. BIG CONCEPT Bring the attention of your audience over a

    key concept using icons or illustrations 51
  21. BIG CONCEPT Bring the attention of your audience over a

    key concept using icons or illustrations 52
  22. In two or three columns Yellow Is the color of

    gold, butter and ripe lemons. In the spectrum of visible light, yellow is found between green and orange. Blue Is the colour of the clear sky and the deep sea. It is located between violet and green on the optical spectrum. Red Is the color of blood, and because of this it has historically been associated with sacrifice, danger and courage. 53
  23. A picture is worth a thousand words A complex idea

    can be conveyed with just a single still image, namely making it possible to absorb large amounts of data quickly. 54
  24. And tables to compare data A B C Yellow 10

    20 7 Blue 30 15 10 Orange 5 24 16 57
  25. Let’s review some concepts Yellow Is the color of gold,

    butter and ripe lemons. In the spectrum of visible light, yellow is found between green and orange. Blue Is the colour of the clear sky and the deep sea. It is located between violet and green on the optical spectrum. Red Is the color of blood, and because of this it has historically been associated with sacrifice, danger and courage. Yellow Is the color of gold, butter and ripe lemons. In the spectrum of visible light, yellow is found between green and orange. Blue Is the colour of the clear sky and the deep sea. It is located between violet and green on the optical spectrum. Red Is the color of blood, and because of this it has historically been associated with sacrifice, danger and courage. 62
  26. Android project Show and explain your web, app or software

    projects using these gadget templates. Place your screenshot here 64
  27. Place your screenshot here iPhone project Show and explain your

    web, app or software projects using these gadget templates. 65
  28. Place your screenshot here Tablet project Show and explain your

    web, app or software projects using these gadget templates. 66
  29. Place your screenshot here Desktop project Show and explain your

    web, app or software projects using these gadget templates. 67
  30. Credits Special thanks to all the people who made and

    released these awesome resources for free: ▪ Presentation template by SlidesCarnival ▪ Photographs by Unsplash 69
  31. Presentation design This presentation uses the following typographies: ▪ Titles:

    Work sans bold ▪ Body copy: Work sans light You can download the fonts on this page: https://github.com/weiweihuanghuang/Work-Sans/tree/master/fonts/desktop You don’t need to keep this slide in your presentation. It’s only here to serve you as a design guide if you need to create new slides or download the fonts to edit the presentation in PowerPoint® 70
  32. SlidesCarnival icons are editable shapes. This means that you can:

    • Resize them without losing quality. • Change fill color and opacity. • Change line color, width and style. Isn’t that nice? :) Examples: 71
  33. Now you can use any emoji as an icon! And

    of course it resizes without losing quality and you can change the color. How? Follow Google instructions https://twitter.com/googledocs/status/730087240156643328 ✋ ❤ and many more... 72
  34. Free templates for all your presentation needs Ready to use,

    professional and customizable 100% free for personal or commercial use Blow your audience away with attractive visuals For PowerPoint and Google Slides 73