diff options
author | Rocky Madden <git@rockymadden.com> | 2012-11-15 10:32:18 -0700 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2012-11-15 10:32:18 -0700 |
commit | 690846dd22982d7a4a9b998686d6d3768163475c (patch) | |
tree | 312c18c78f51431d88b4e5660dc6acc1e7aac07c /core/source/benchmark/scala/org/hashtree/stringmetric/similarity/NGramMetricBenchmark.scala | |
parent | 73f92cba9918a8aea2d784cff94c5dbade5cbbbd (diff) | |
download | stringmetric-690846dd22982d7a4a9b998686d6d3768163475c.tar.gz stringmetric-690846dd22982d7a4a9b998686d6d3768163475c.tar.bz2 stringmetric-690846dd22982d7a4a9b998686d6d3768163475c.zip |
Added Google Caliper microbenchmark suite for every metric and algorithm.
Diffstat (limited to 'core/source/benchmark/scala/org/hashtree/stringmetric/similarity/NGramMetricBenchmark.scala')
-rwxr-xr-x | core/source/benchmark/scala/org/hashtree/stringmetric/similarity/NGramMetricBenchmark.scala | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/core/source/benchmark/scala/org/hashtree/stringmetric/similarity/NGramMetricBenchmark.scala b/core/source/benchmark/scala/org/hashtree/stringmetric/similarity/NGramMetricBenchmark.scala new file mode 100755 index 0000000..c9c0717 --- /dev/null +++ b/core/source/benchmark/scala/org/hashtree/stringmetric/similarity/NGramMetricBenchmark.scala @@ -0,0 +1,55 @@ +package org.hashtree.stringmetric.similarity + +import com.google.caliper.Param +import org.hashtree.stringmetric.{ CaliperBenchmark, CaliperRunner } +import scala.annotation.tailrec +import scala.util.Random + +final class NGramMetricBenchmark extends CaliperBenchmark { + @Param(Array("0", "1", "2", "4", "8", "16")) + var length: Int = _ + + @Param(Array("2", "3")) + var n: Int = _ + + var string1: String = _ + var charArray1: Array[Char] = _ + var string2: String = _ + var charArray2: Array[Char] = _ + + override protected def setUp() { + @tailrec + def random(l: Int, ps: String = null): String = { + if (l == 0) "" + else { + val s = Random.alphanumeric.take(l).mkString + + if (ps == null || s != ps) s + else random(l, ps) + } + } + + string1 = random(length) + string2 = random(length, string1) + charArray1 = string1.toCharArray + charArray2 = string2.toCharArray + } + + def timeCompareWithDifferentCharArrays(reps: Int) = run(reps) { + NGramMetric.compare(charArray1, charArray2)(n) + } + + def timeCompareWithDifferentStrings(reps: Int) = run(reps) { + NGramMetric.compare(string1, string2)(n) + } + + def timeCompareWithIdenticalCharArrays(reps: Int) = run(reps) { + NGramMetric.compare(charArray1, charArray1)(n) + } + + def timeCompareWithIdenticalStrings(reps: Int) = run(reps) { + NGramMetric.compare(string1, string1)(n) + } +} + +object NGramMetricBenchmark extends CaliperRunner(classOf[NGramMetricBenchmark])
\ No newline at end of file |