More Scala benching tests

This commit is contained in:
Luke Hubmayer-Werner 2022-12-06 20:09:07 +10:30
parent 47bfab928a
commit 90c359f7cb
1 changed files with 12 additions and 10 deletions

View File

@ -1,24 +1,26 @@
import java.nio.file.Files import java.nio.file.Files
import java.nio.file.Paths import java.nio.file.Paths
import scala.util.control.NonLocalReturns.*
def first_unique_run_for(line: String, num: Int, skip: Int = 0): Int = def first_unique_run_for(line: String, num: Int, skip: Int = 0): Int = returning {
for i <- num.max(skip) until line.length do for i <- num.max(skip) until line.length do
if line.substring(i-num, i).toCharArray.toSet.size == num then if line.substring(i-num, i).toCharArray.distinct.size == num then
return i throwReturn(i)
return -1 throwReturn(-1)
}
def first_unique_run_while(line: String, num: Int, skip: Int = 0): Int = def first_unique_run_while(line: String, num: Int, skip: Int = 0): Int =
var i = num.max(skip) var i = num.max(skip)
while i < line.length do while i < line.length do
if line.substring(i-num, i).toCharArray.toSet.size == num then if line.substring(i-num, i).toCharArray.distinct.size == num then
return i return i
i += 1 i += 1
return -1 return -1
@main def main() = @main def main() =
val line = Files.readString(Paths.get("input/6")).strip val line = Files.readString(Paths.get("input/6")).strip
do_once(line) // do_once(line)
// perf_test(line, 50000) perf_test(line, 100000)
def do_once(line: String) = def do_once(line: String) =
val four = first_unique_run_for(line, 4) val four = first_unique_run_for(line, 4)
@ -26,11 +28,11 @@ def do_once(line: String) =
println(four) println(four)
println(fourteen) println(fourteen)
def perf_test(line: String, n: int) = def perf_test(line: String, n: Int) =
var four = -1 var four = -1
var fourteen = -1 var fourteen = -1
for i <- 1 to n do for i <- 1 to n do
four = first_unique_run_while(line, 4) four = first_unique_run_for(line, 4)
fourteen = first_unique_run_while(line, 14, four) fourteen = first_unique_run_for(line, 14, four)
println(four) println(four)
println(fourteen) println(fourteen)