Skip to content

Instantly share code, notes, and snippets.

@sndyuk
Created March 20, 2013 08:29
Show Gist options
  • Save sndyuk/5203158 to your computer and use it in GitHub Desktop.
Save sndyuk/5203158 to your computer and use it in GitHub Desktop.
ListとListBufferの要素を後ろに追加する処理の速度を測ってみた Scala 2.10
import java.io.PrintWriter
import scala.collection.mutable.ListBuffer
import scala.util.{ Try, Success, Failure }
object List_add_functioins extends App {
//val out = new PrintWriter("score.csv", "utf-8")
//out.println("""symbol,size,elapsed""") -> For D3js
def time[R](symbol: String, size: Int, block: => R): R = {
val limit = 1000 // timeout ms
val t0 = System.nanoTime()
val results = (0 until 5).map { n =>
block
}
val t1 = System.nanoTime()
val elapsed = (t1 - t0) / 1000000
println(f"${size}%06d > Elapsed time: ${elapsed}%dms")
//out.println(s"${symbol},${size},${elapsed}")
if (elapsed > limit) {
throw new RuntimeException("Timeup.")
}
results.head
}
def drive[R](id: String, block: Int => R) {
println(s"------ ${id}")
Runtime.getRuntime.gc
Thread.sleep(1000)
Try((1 to 200001 by 5000).foreach(n => time(id, n, block(n)))) match {
case Success(v) =>
println("All done.")
case Failure(v) =>
println(v.getMessage)
}
}
drive("List :::", (size: Int) => {
val actual = (List(0) /: (1 until size)) { (xs, n) =>
xs ::: List(n)
}
actual.sum
assume(actual == (0 to size - 1).toList)
})
drive("List ::", (size: Int) => {
val actual = (List(0) /: (1 until size)) { (xs, n) =>
n :: xs
}.reverse
actual.sum
assume(actual == (0 to size - 1).toList)
})
drive("List ++", (size: Int) => {
val actual = (List(0) /: (1 until size)) { (xs, n) =>
xs ++ List(n)
}
actual.sum
assume(actual == (0 to size - 1).toList)
})
drive("List +:", (size: Int) => {
val actual = (List(0) /: (1 until size)) { (xs, n) =>
n +: xs
}.reverse
actual.sum
assume(actual == (0 to size - 1).toList)
})
drive("List :+", (size: Int) => {
val actual = (List(0) /: (1 until size)) { (xs, n) =>
xs :+ n
}
actual.sum
assume(actual == (0 to size - 1).toList)
})
drive("Buffer +=", (size: Int) => {
val actual = (ListBuffer(0) /: (1 until size)) { (xs, n) =>
xs += n
}
actual.sum
assume(actual == (0 to size - 1).toList)
})
drive("Buffer +=:", (size: Int) => {
val actual = (ListBuffer(0) /: (1 until size)) { (xs, n) =>
n +=: xs
}.reverse
actual.sum
assume(actual == (0 to size - 1).toList)
})
drive("Buffer ++=", (size: Int) => {
val actual = (ListBuffer(0) /: (1 until size)) { (xs, n) =>
xs ++= List(n)
}
actual.sum
assume(actual == (0 to size - 1).toList)
})
drive("ArrayList add", (size: Int) => {
import scala.collection.JavaConversions._
import java.util.ArrayList
val list = new ArrayList[Int]()
list.add(0)
val actual = ((list /: (1 until size)) { (xs, n) =>
xs.add(n)
xs
}).toList
actual.sum
assume(actual == (0 to size - 1).toList)
})
//out.close
}
@sndyuk
Copy link
Author

sndyuk commented Mar 20, 2013

------ List :::
000001 > Elapsed time: 6ms
005001 > Elapsed time: 446ms
010001 > Elapsed time: 1225ms
Timeup.
------ List ::
000001 > Elapsed time: 2ms
005001 > Elapsed time: 11ms
010001 > Elapsed time: 13ms
015001 > Elapsed time: 5ms
020001 > Elapsed time: 3ms
025001 > Elapsed time: 4ms
030001 > Elapsed time: 5ms
035001 > Elapsed time: 6ms
040001 > Elapsed time: 7ms
045001 > Elapsed time: 8ms
050001 > Elapsed time: 8ms
055001 > Elapsed time: 9ms
060001 > Elapsed time: 10ms
065001 > Elapsed time: 13ms
070001 > Elapsed time: 15ms
075001 > Elapsed time: 34ms
080001 > Elapsed time: 15ms
085001 > Elapsed time: 16ms
090001 > Elapsed time: 17ms
095001 > Elapsed time: 19ms
100001 > Elapsed time: 20ms
105001 > Elapsed time: 97ms
110001 > Elapsed time: 27ms
115001 > Elapsed time: 24ms
120001 > Elapsed time: 25ms
125001 > Elapsed time: 25ms
130001 > Elapsed time: 105ms
135001 > Elapsed time: 27ms
140001 > Elapsed time: 28ms
145001 > Elapsed time: 32ms
150001 > Elapsed time: 33ms
155001 > Elapsed time: 38ms
160001 > Elapsed time: 35ms
165001 > Elapsed time: 73ms
170001 > Elapsed time: 41ms
175001 > Elapsed time: 38ms
180001 > Elapsed time: 42ms
185001 > Elapsed time: 55ms
190001 > Elapsed time: 44ms
195001 > Elapsed time: 95ms
200001 > Elapsed time: 51ms
All done.
------ List ++
000001 > Elapsed time: 3ms
005001 > Elapsed time: 308ms
010001 > Elapsed time: 1173ms
Timeup.
------ List +:
000001 > Elapsed time: 2ms
005001 > Elapsed time: 2ms
010001 > Elapsed time: 2ms
015001 > Elapsed time: 2ms
020001 > Elapsed time: 3ms
025001 > Elapsed time: 4ms
030001 > Elapsed time: 5ms
035001 > Elapsed time: 6ms
040001 > Elapsed time: 7ms
045001 > Elapsed time: 8ms
050001 > Elapsed time: 9ms
055001 > Elapsed time: 9ms
060001 > Elapsed time: 10ms
065001 > Elapsed time: 12ms
070001 > Elapsed time: 13ms
075001 > Elapsed time: 29ms
080001 > Elapsed time: 16ms
085001 > Elapsed time: 17ms
090001 > Elapsed time: 23ms
095001 > Elapsed time: 22ms
100001 > Elapsed time: 21ms
105001 > Elapsed time: 51ms
110001 > Elapsed time: 24ms
115001 > Elapsed time: 26ms
120001 > Elapsed time: 31ms
125001 > Elapsed time: 29ms
130001 > Elapsed time: 57ms
135001 > Elapsed time: 32ms
140001 > Elapsed time: 36ms
145001 > Elapsed time: 35ms
150001 > Elapsed time: 62ms
155001 > Elapsed time: 35ms
160001 > Elapsed time: 44ms
165001 > Elapsed time: 83ms
170001 > Elapsed time: 42ms
175001 > Elapsed time: 42ms
180001 > Elapsed time: 80ms
185001 > Elapsed time: 49ms
190001 > Elapsed time: 43ms
195001 > Elapsed time: 91ms
200001 > Elapsed time: 50ms
All done.
------ List :+
000001 > Elapsed time: 2ms
005001 > Elapsed time: 343ms
010001 > Elapsed time: 1399ms
Timeup.
------ Buffer +=
000001 > Elapsed time: 3ms
005001 > Elapsed time: 3ms
010001 > Elapsed time: 2ms
015001 > Elapsed time: 2ms
020001 > Elapsed time: 3ms
025001 > Elapsed time: 4ms
030001 > Elapsed time: 5ms
035001 > Elapsed time: 6ms
040001 > Elapsed time: 7ms
045001 > Elapsed time: 8ms
050001 > Elapsed time: 9ms
055001 > Elapsed time: 10ms
060001 > Elapsed time: 10ms
065001 > Elapsed time: 11ms
070001 > Elapsed time: 12ms
075001 > Elapsed time: 13ms
080001 > Elapsed time: 35ms
085001 > Elapsed time: 18ms
090001 > Elapsed time: 21ms
095001 > Elapsed time: 19ms
100001 > Elapsed time: 20ms
105001 > Elapsed time: 21ms
110001 > Elapsed time: 25ms
115001 > Elapsed time: 48ms
120001 > Elapsed time: 28ms
125001 > Elapsed time: 26ms
130001 > Elapsed time: 25ms
135001 > Elapsed time: 30ms
140001 > Elapsed time: 67ms
145001 > Elapsed time: 32ms
150001 > Elapsed time: 31ms
155001 > Elapsed time: 31ms
160001 > Elapsed time: 31ms
165001 > Elapsed time: 69ms
170001 > Elapsed time: 62ms
175001 > Elapsed time: 60ms
180001 > Elapsed time: 71ms
185001 > Elapsed time: 81ms
190001 > Elapsed time: 41ms
195001 > Elapsed time: 40ms
200001 > Elapsed time: 76ms
All done.
------ Buffer +=:
000001 > Elapsed time: 1ms
005001 > Elapsed time: 10ms
010001 > Elapsed time: 12ms
015001 > Elapsed time: 6ms
020001 > Elapsed time: 6ms
025001 > Elapsed time: 5ms
030001 > Elapsed time: 6ms
035001 > Elapsed time: 7ms
040001 > Elapsed time: 9ms
045001 > Elapsed time: 10ms
050001 > Elapsed time: 11ms
055001 > Elapsed time: 13ms
060001 > Elapsed time: 14ms
065001 > Elapsed time: 21ms
070001 > Elapsed time: 21ms
075001 > Elapsed time: 19ms
080001 > Elapsed time: 20ms
085001 > Elapsed time: 28ms
090001 > Elapsed time: 24ms
095001 > Elapsed time: 45ms
100001 > Elapsed time: 33ms
105001 > Elapsed time: 29ms
110001 > Elapsed time: 29ms
115001 > Elapsed time: 59ms
120001 > Elapsed time: 38ms
125001 > Elapsed time: 35ms
130001 > Elapsed time: 40ms
135001 > Elapsed time: 68ms
140001 > Elapsed time: 39ms
145001 > Elapsed time: 39ms
150001 > Elapsed time: 58ms
155001 > Elapsed time: 43ms
160001 > Elapsed time: 48ms
165001 > Elapsed time: 76ms
170001 > Elapsed time: 52ms
175001 > Elapsed time: 47ms
180001 > Elapsed time: 80ms
185001 > Elapsed time: 57ms
190001 > Elapsed time: 103ms
195001 > Elapsed time: 58ms
200001 > Elapsed time: 80ms
All done.
------ Buffer ++=
000001 > Elapsed time: 1ms
005001 > Elapsed time: 4ms
010001 > Elapsed time: 5ms
015001 > Elapsed time: 6ms
020001 > Elapsed time: 8ms
025001 > Elapsed time: 10ms
030001 > Elapsed time: 13ms
035001 > Elapsed time: 16ms
040001 > Elapsed time: 18ms
045001 > Elapsed time: 22ms
050001 > Elapsed time: 26ms
055001 > Elapsed time: 40ms
060001 > Elapsed time: 33ms
065001 > Elapsed time: 35ms
070001 > Elapsed time: 33ms
075001 > Elapsed time: 42ms
080001 > Elapsed time: 42ms
085001 > Elapsed time: 46ms
090001 > Elapsed time: 48ms
095001 > Elapsed time: 69ms
100001 > Elapsed time: 50ms
105001 > Elapsed time: 58ms
110001 > Elapsed time: 87ms
115001 > Elapsed time: 56ms
120001 > Elapsed time: 81ms
125001 > Elapsed time: 78ms
130001 > Elapsed time: 73ms
135001 > Elapsed time: 87ms
140001 > Elapsed time: 69ms
145001 > Elapsed time: 84ms
150001 > Elapsed time: 80ms
155001 > Elapsed time: 89ms
160001 > Elapsed time: 83ms
165001 > Elapsed time: 117ms
170001 > Elapsed time: 118ms
175001 > Elapsed time: 88ms
180001 > Elapsed time: 94ms
185001 > Elapsed time: 92ms
190001 > Elapsed time: 118ms
195001 > Elapsed time: 162ms
200001 > Elapsed time: 104ms
All done.
------ ArrayList add
000001 > Elapsed time: 4ms
005001 > Elapsed time: 5ms
010001 > Elapsed time: 6ms
015001 > Elapsed time: 4ms
020001 > Elapsed time: 5ms
025001 > Elapsed time: 6ms
030001 > Elapsed time: 6ms
035001 > Elapsed time: 7ms
040001 > Elapsed time: 8ms
045001 > Elapsed time: 9ms
050001 > Elapsed time: 10ms
055001 > Elapsed time: 11ms
060001 > Elapsed time: 13ms
065001 > Elapsed time: 14ms
070001 > Elapsed time: 18ms
075001 > Elapsed time: 25ms
080001 > Elapsed time: 18ms
085001 > Elapsed time: 18ms
090001 > Elapsed time: 27ms
095001 > Elapsed time: 23ms
100001 > Elapsed time: 22ms
105001 > Elapsed time: 39ms
110001 > Elapsed time: 30ms
115001 > Elapsed time: 26ms
120001 > Elapsed time: 28ms
125001 > Elapsed time: 35ms
130001 > Elapsed time: 40ms
135001 > Elapsed time: 36ms
140001 > Elapsed time: 34ms
145001 > Elapsed time: 34ms
150001 > Elapsed time: 55ms
155001 > Elapsed time: 37ms
160001 > Elapsed time: 41ms
165001 > Elapsed time: 42ms
170001 > Elapsed time: 58ms
175001 > Elapsed time: 41ms
180001 > Elapsed time: 57ms
185001 > Elapsed time: 65ms
190001 > Elapsed time: 48ms
195001 > Elapsed time: 47ms
200001 > Elapsed time: 49ms
All done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment