Use a `LinkedList` instead of a `.toMutableList()`...
# advent-of-code
k
Use a
LinkedList
instead of a
.toMutableList()
ArrayList
.
s
I don't understand it, but when I change it to
LinkedList
, performance is worser. In all cases.
I mean that I tried to replace
ArrayList
with
LinkedList
in all places (both in main function and in part2 implementation), and always it causes problems with performance.
k
You literally changed line 23 to create a linkedlist?
s
I changed
polymer.toMutableList()
to
LinkedList(polymer)
in line 23.
k
Can you quickly share your buid.gradle for the coroutine stuff?
s
I've added also measuring like this:
Copy code
fun main(args: Array<String>) {
    val time = measureTimeMillis {
        val input = File("input5.txt").readText()
        val currentPolymer = input.toCharArray().toMutableList()
        println(Day5.part1(currentPolymer))
        println(Day5.part2(currentPolymer))
    }
    println(time)
}
And version with LinkedList is about 2 times slower
@karelpeeters It's default build.gradle file generated for gradle project by Intellij. The only thing I added was:
Copy code
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
Also part1 is much slower when I use LinkedList in 10 line.
k
Yeah it's the same for me when I run your code.
Which is really strange since I have about the same code here: https://kotlinlang.slack.com/archives/C87V9MQFK/p1544022734125600
I'm going to bed now, I'll experiment a bit with it tomorrow simple smile
👍 1
h
ArrayList generally outperforms LinkedList
p
If you want to go faster, keep ArrayList, but start in the end of the list instead of the beginning
this will take constant time with arraylist, but linear time with linkedlist
k
Nice, thanks for finding it!