Skip to content

Instantly share code, notes, and snippets.

@GreyCat
Created October 26, 2016 08:29
Show Gist options
  • Save GreyCat/6436a0d981b4d6e6034ca613909775c2 to your computer and use it in GitHub Desktop.
Save GreyCat/6436a0d981b4d6e6034ca613909775c2 to your computer and use it in GitHub Desktop.
Benchmark FastParse + MidiParse
import java.nio.file.{Files, Paths}
import fastparse.byte.Midi.MidiData.Controller
import fastparse.byte.Midi.MidiEvent
import fastparse.byte.MidiParse
import scodec.bits.ByteVector
import java.text.DecimalFormat
import java.text.DecimalFormatSymbols
import java.util.Locale
class Series {
private var count: Long = 0
private var sum: Long = 0
private var sum2: Long = 0
def add(x: Long) {
count += 1
sum += x
sum2 += x * x
}
def avg: Double = sum.toDouble / count
def stDev: Double = {
if (count > 1)
Math.sqrt((count * sum2 - sum * sum) / (count * (count - 1)))
else
Double.PositiveInfinity
}
def report: String = {
val df: DecimalFormat = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH))
df.setMaximumFractionDigits(340)
df.format(avg / 1e9) + " ± " + df.format(stDev / 1e9)
}
def perSec: Double = count / (sum.toDouble / 1e9)
}
object BenchmarkMidi extends App {
var count: Array[Int] = new Array[Int](16)
def oneIteration() {
val bytes = Files.readAllBytes(Paths.get("../../git/kaitai_struct/benchmarks/data/standard_midi_file.dat"))
MidiParse.midiParser.parse(ByteVector(bytes)) match {
case fastparse.byte.all.Parsed.Success(midi, index) =>
count = new Array[Int](16)
midi.tracks.foreach((track) =>
track.foreach { case (t, ev) =>
ev match {
case MidiEvent(channel, data) =>
data match {
case _: Controller =>
count(channel) += 1
case _ =>
// ignore all other event data
}
case _ =>
// ignore all other event types
}
}
)
case fastparse.byte.all.Parsed.Failure(lastParser, index, extra) =>
Console.println("Failed to parse midi\n" + extra.traced.trace)
}
}
def timedIteration(): Long = {
val t1 = System.nanoTime
oneIteration()
val t2 = System.nanoTime
Console.println(count.mkString(", "))
t2 - t1
}
val timings = new Series
Console.println("=== WARMUP")
(0 until 20).foreach { _ =>
val t = timedIteration()
Console.println(t)
}
Console.println("=== GO!")
(0 until 500).foreach { _ =>
val t = timedIteration()
timings.add(t)
Console.println(timings.report)
}
Console.println(timings.perSec)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment