diff options
author | Rocky Madden <git@rockymadden.com> | 2013-03-09 15:01:48 -0700 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2013-03-09 15:01:48 -0700 |
commit | 532568e38856f277eb947c46239ed39b98216947 (patch) | |
tree | dbed1f847de666babb0a40f94b7c00ccab5d93b2 /core/source/benchmark | |
parent | 2a775ee2a8279b458b84ef35c458c58b19aa98c9 (diff) | |
download | stringmetric-532568e38856f277eb947c46239ed39b98216947.tar.gz stringmetric-532568e38856f277eb947c46239ed39b98216947.tar.bz2 stringmetric-532568e38856f277eb947c46239ed39b98216947.zip |
Created Jaccard metric, spec, benchmark, and CLI.
Diffstat (limited to 'core/source/benchmark')
-rwxr-xr-x | core/source/benchmark/scala/com/rockymadden/stringmetric/similarity/JaccardMetricBenchmark.scala | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/core/source/benchmark/scala/com/rockymadden/stringmetric/similarity/JaccardMetricBenchmark.scala b/core/source/benchmark/scala/com/rockymadden/stringmetric/similarity/JaccardMetricBenchmark.scala new file mode 100755 index 0000000..62be6bc --- /dev/null +++ b/core/source/benchmark/scala/com/rockymadden/stringmetric/similarity/JaccardMetricBenchmark.scala @@ -0,0 +1,55 @@ +package com.rockymadden.stringmetric.similarity + +import com.google.caliper.Param +import com.rockymadden.stringmetric.{ CaliperBenchmark, CaliperRunner } +import scala.annotation.tailrec +import scala.util.Random + +final class JaccardMetricBenchmark extends CaliperBenchmark { + import JaccardMetricBenchmark.Metric + + @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) { + Metric.compare(charArray1, charArray2)(2) + } + + def timeCompareWithDifferentStrings(reps: Int) = run(reps) { + Metric.compare(string1, string2)(2) + } + + def timeCompareWithIdenticalCharArrays(reps: Int) = run(reps) { + Metric.compare(charArray1, charArray1)(2) + } + + def timeCompareWithIdenticalStrings(reps: Int) = run(reps) { + Metric.compare(string1, string1)(2) + } +} + +object JaccardMetricBenchmark extends CaliperRunner(classOf[JaccardMetricBenchmark]) { + private final val Metric = JaccardMetric() +} |