Skip to content

Instantly share code, notes, and snippets.

@fero23
Last active October 29, 2015 20:24
Show Gist options
  • Save fero23/d1a406b901dcfe8968fa to your computer and use it in GitHub Desktop.
Save fero23/d1a406b901dcfe8968fa to your computer and use it in GitHub Desktop.
Gets and processes benchmark data from http://benchmarksgame.alioth.debian.org/u64q
object Benchmarks {
import io.Source
import util.{Try, Success}
case class Benchmark(name: String, lang: String, id: Int, n: Int, size: Int,
cpu: Double, mem: Int, status: Int, load: String, elapsed: Double)
object Benchmark { def fromSeq(s: Seq[String]) = Benchmark(s(0), s(1), s(2).toInt, s(3).toInt,
s(4).toInt, s(5).toDouble, s(6).toInt, s(7).toInt, s(8), s(9).toDouble) }
val BenchmarkRegex = """[\s\S]*<pre>([\s\S]+)<\/pre>[\s\S]*""".r
lazy val benchmarks = Source.fromURL("http://benchmarksgame.alioth.debian.org/u64q/summarydata.php").mkString match {
case BenchmarkRegex(pre) => pre.trim.lines.drop(1).map(line => Benchmark.fromSeq(line.split(","))).toSeq
case _ => Nil
}
def getFasterBenchmarks(lang: String) = benchmarks.filter(b => b.lang == lang).groupBy(_.name).values.map(_.sortWith(_.elapsed > _.elapsed).head).toSeq
def compare(lang1: String, lang2: String): Option[String] = {
(getFasterBenchmarks(lang1), getFasterBenchmarks(lang2)) match {
case (_, Nil) => None
case (Nil, _) => None
case (sb1, sb2) =>
def results = sb1.map { b1 =>
Try(sb2.filter(_.name == b1.name).head) match {
case Success(b2) =>
Some((b1.name, b1.elapsed.compare(b2.elapsed), s"""${b1.lang} => Elapsed(s): ${b1.elapsed} CPU(s): ${b1.cpu} Mem: ${b1.mem}
|${b2.lang} => Elapsed(s): ${b2.elapsed} CPU(s): ${b2.cpu} Mem: ${b2.mem}""".stripMargin))
case _ => None
}
}
var won: Seq[(String, String)] = Nil
var lost: Seq[(String, String)] = Nil
var tie: Seq[(String, String)] = Nil
for (Some((name, comparation, res)) <- results) {
if(comparation < 0)
won = (name, res) +: won
else if(comparation == 0)
tie = (name, res) +: tie
else
lost = (name, res) +: lost
}
val res = s"""Comparision between ${sb1.head.lang} and ${sb2.head.lang}
|${sb1.head.lang} won ${won.length}
|${sb2.head.lang} won ${lost.length}
|Tied on ${tie.length}""".stripMargin
def join(list: Seq[(String, String)]): String = {
val res = list.map { case (name, r) => s"\n$name\n$r" }.mkString("\n")
"\n" + "-" * 30 + (if(res.isEmpty) "\nNone" else res)
}
Some(res + "\n\nPrograms won:" + join(won) +
"\n\nPrograms lost:" + join(lost) +
"\n\nPrograms tied:" + join(tie))
}
}
def getFasterLanguages = benchmarks.groupBy(_.lang).keys.map(lang => getFasterBenchmarks(lang)).map { b =>
(b.head.lang, b.map(_.elapsed).sum / b.length)
}.toSeq
def getFasterLanguagesStr: String = {
"Faster languages (on average):\n" + "-" * 30 + "\n" + getFasterLanguages.sortWith(_._2 < _._2).map {
case (name, average) => f"$name: $average%.2f"
}.mkString("\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment