diff options
Diffstat (limited to 'core/source/benchmark/scala/org/hashtree/stringmetric/similarity/LevenshteinMetricBenchmark.scala')
-rwxr-xr-x | core/source/benchmark/scala/org/hashtree/stringmetric/similarity/LevenshteinMetricBenchmark.scala | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/core/source/benchmark/scala/org/hashtree/stringmetric/similarity/LevenshteinMetricBenchmark.scala b/core/source/benchmark/scala/org/hashtree/stringmetric/similarity/LevenshteinMetricBenchmark.scala new file mode 100755 index 0000000..598a44f --- /dev/null +++ b/core/source/benchmark/scala/org/hashtree/stringmetric/similarity/LevenshteinMetricBenchmark.scala @@ -0,0 +1,52 @@ +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 LevenshteinMetricBenchmark extends CaliperBenchmark { + @Param(Array("0", "1", "2", "4", "8", "16")) + var length: 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) { + LevenshteinMetric.compare(charArray1, charArray2) + } + + def timeCompareWithDifferentStrings(reps: Int) = run(reps) { + LevenshteinMetric.compare(string1, string2) + } + + def timeCompareWithIdenticalCharArrays(reps: Int) = run(reps) { + LevenshteinMetric.compare(charArray1, charArray1) + } + + def timeCompareWithIdenticalStrings(reps: Int) = run(reps) { + LevenshteinMetric.compare(string1, string1) + } +} + +object LevenshteinMetricBenchmark extends CaliperRunner(classOf[LevenshteinMetricBenchmark])
\ No newline at end of file |