From 7f0ab1fc566f0b3ec97156f4b3e5abc764d9703a Mon Sep 17 00:00:00 2001 From: Jarek Sacha Date: Fri, 30 Apr 2021 13:38:10 -0400 Subject: [PATCH] Add simple benchmarks for testing debayer performance --- .../experimental/BenchmarkDDFAPD.scala | 54 ++++++++++++++ .../experimental/BenchmarkDebayer.scala | 71 +++++++++++++++++++ .../experimental/BenchmarkHelper.scala | 55 ++++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 ijp-debayer2sx-experimental/src/main/scala/ij_plugins/debayer2sx/experimental/BenchmarkDDFAPD.scala create mode 100644 ijp-debayer2sx-experimental/src/main/scala/ij_plugins/debayer2sx/experimental/BenchmarkDebayer.scala create mode 100644 ijp-debayer2sx-experimental/src/main/scala/ij_plugins/debayer2sx/experimental/BenchmarkHelper.scala diff --git a/ijp-debayer2sx-experimental/src/main/scala/ij_plugins/debayer2sx/experimental/BenchmarkDDFAPD.scala b/ijp-debayer2sx-experimental/src/main/scala/ij_plugins/debayer2sx/experimental/BenchmarkDDFAPD.scala new file mode 100644 index 0000000..d04aa96 --- /dev/null +++ b/ijp-debayer2sx-experimental/src/main/scala/ij_plugins/debayer2sx/experimental/BenchmarkDDFAPD.scala @@ -0,0 +1,54 @@ +/* + * Image/J Plugins + * Copyright (C) 2002-2021 Jarek Sacha + * Author's email: jpsacha at gmail [dot] com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Latest release available at https://github.com/ij-plugins/ijp-DeBayer2SX + */ + +package ij_plugins.debayer2sx.experimental + +import ij.ImageStack +import ij.process.FloatProcessor +import ij_plugins.debayer2sx.DDFAPD + +object BenchmarkDDFAPD extends App { + + run() + + private def run(): Unit = { + + val helper = new BenchmarkHelper(testIter = 5) + + val fp = new FloatProcessor(2048, 1536) + + val nbIter = 5 + + for (i <- 0 until nbIter) { + println() + println(s"Run ${i + 1}") + + helper.measure[FloatProcessor, ImageStack]( + tag = "DDFAPD.debayerGR ", fp, + DDFAPD.debayerGR(_, 16, doRefine = true)) + } + + println() + println("Min time:") + helper.printResults() + } +} diff --git a/ijp-debayer2sx-experimental/src/main/scala/ij_plugins/debayer2sx/experimental/BenchmarkDebayer.scala b/ijp-debayer2sx-experimental/src/main/scala/ij_plugins/debayer2sx/experimental/BenchmarkDebayer.scala new file mode 100644 index 0000000..0fbf5db --- /dev/null +++ b/ijp-debayer2sx-experimental/src/main/scala/ij_plugins/debayer2sx/experimental/BenchmarkDebayer.scala @@ -0,0 +1,71 @@ +/* + * Image/J Plugins + * Copyright (C) 2002-2021 Jarek Sacha + * Author's email: jpsacha at gmail [dot] com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Latest release available at https://github.com/ij-plugins/ijp-DeBayer2SX + */ + +package ij_plugins.debayer2sx.experimental + +import ij.process.{FloatProcessor, ShortProcessor} +import ij.{ImagePlus, ImageStack} +import ij_plugins.debayer2sx.DeBayer2Config.{Demosaicing, MosaicOrder} +import ij_plugins.debayer2sx.debayer1.Debayer_Image +import ij_plugins.debayer2sx.{DDFAPD, DeBayer2, DeBayer2Config} + +object BenchmarkDebayer extends App { + + run() + + private def run(): Unit = { + + val helper = new BenchmarkHelper(testIter = 10) + + val sp = new ShortProcessor(2048, 1536) + val fp: FloatProcessor = sp.convertToFloatProcessor() + val imp = new ImagePlus("", sp) + + val nbIter = 10 + + for (i <- 0 until nbIter) { + println() + println(s"Run ${i + 1}") + + helper.measure("replicate_decode", imp, Debayer_Image.replicate_decode(0, _)) + helper.measure("average_decode ", imp, Debayer_Image.average_decode(0, _)) + helper.measure("smooth_decode ", imp, Debayer_Image.smooth_decode(0, _)) + helper.measure("adaptive_decode ", imp, Debayer_Image.adaptive_decode(0, _)) + + helper.measure( + tag = "DeBayer2/AdaptiveSmoothHue", sp, + DeBayer2.process(_, DeBayer2Config(MosaicOrder.B_G, Demosaicing.AdaptiveSmoothHue))) + + helper.measure( + tag = "DeBayer2/DDFAPDRefined ", sp, + DeBayer2.process(_, DeBayer2Config(MosaicOrder.G_R, Demosaicing.DDFAPDRefined))) + + helper.measure[FloatProcessor, ImageStack]( + tag = "DDFAPD.debayerGR ", fp, + DDFAPD.debayerGR(_, 16, doRefine = true)) + } + + println() + println("Min time:") + helper.printResults() + } +} diff --git a/ijp-debayer2sx-experimental/src/main/scala/ij_plugins/debayer2sx/experimental/BenchmarkHelper.scala b/ijp-debayer2sx-experimental/src/main/scala/ij_plugins/debayer2sx/experimental/BenchmarkHelper.scala new file mode 100644 index 0000000..db3d238 --- /dev/null +++ b/ijp-debayer2sx-experimental/src/main/scala/ij_plugins/debayer2sx/experimental/BenchmarkHelper.scala @@ -0,0 +1,55 @@ +/* + * Image/J Plugins + * Copyright (C) 2002-2021 Jarek Sacha + * Author's email: jpsacha at gmail [dot] com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Latest release available at https://github.com/ij-plugins/ijp-DeBayer2SX + */ + +package ij_plugins.debayer2sx.experimental + +import scala.collection.mutable.ListBuffer +import scala.collection.{immutable, mutable} + +class BenchmarkHelper(val testIter: Int) { + private val _results = mutable.ListMap.empty[String, ListBuffer[Double]] + + def results: immutable.Map[String, List[Double]] = { + _results.map { case (k, v) => (k, v.toList) }.toMap + } + + def measure[A, R](tag: String, arg: A, op: (A) => R): Unit = { + val t0 = System.currentTimeMillis() + for (_ <- 0 until testIter) { + op(arg) + } + val delta = (System.currentTimeMillis() - t0) / testIter.toDouble + _results.getOrElseUpdate(tag, ListBuffer.empty[Double]) += delta + println(f"$tag: $delta%7.2f ms.") + } + + def printResults(): Unit = { + val r = results + val maxTagLength = r.keys.map(_.length).max + r.foreach { case (tag, values) => + val p = maxTagLength - tag.length + val t = tag + " " * p + val minVal = values.min + println(f" $t: $minVal%7.2f ms.") + } + } +}