summaryrefslogtreecommitdiff
path: root/core/src/benchmark/scala/com/rockymadden/stringmetric/phonetic/RefinedSoundexMetricBenchmark.scala
blob: 6c52e8d3e5e80103c7b96c369ba4cb914abe0ca7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.rockymadden.stringmetric.phonetic

import com.google.caliper.Param
import com.rockymadden.stringmetric.{CaliperBenchmark, CaliperRunner}
import scala.annotation.tailrec
import scala.util.Random

final class RefinedSoundexMetricBenchmark 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.filter(_ > '9').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) {
		RefinedSoundexMetric.compare(charArray1, charArray2)
	}

	def timeCompareWithDifferentStrings(reps: Int) = run(reps) {
		RefinedSoundexMetric.compare(string1, string2)
	}

	def timeCompareWithIdenticalCharArrays(reps: Int) = run(reps) {
		RefinedSoundexMetric.compare(charArray1, charArray1)
	}

	def timeCompareWithIdenticalStrings(reps: Int) = run(reps) {
		RefinedSoundexMetric.compare(string1, string1)
	}
}