feature[benchmark]: time measurement benchmark

not a good way of testing things, but it's a way and the first way
This commit is contained in:
Abdellah El Morabit 2025-11-20 18:45:53 +01:00
parent 4bb2ff6828
commit 421c6c3c93
4 changed files with 37 additions and 24 deletions

View File

@ -4,24 +4,23 @@ object Torque {
import java.time.Instant import java.time.Instant
import com.nsrddyn.alu.* import com.nsrddyn.alu.*
import com.nsrddyn.tools.Benchmark
@main def main(args: String*): Unit = @main def main(args: String*): Unit =
// ANSI ESCAPE CODE: clear screen val pr = new Prime()
println("\u001b[2J\u001b[H") val br = new Benchmark()
println("--- TORQUE STRESS TESTING UTILITY ---")
val now: Instant = Instant.now() // ANSI ESCAPE CODE: clear screen
println(now) println("\u001b[2J\u001b[H")
println("--- TORQUE STRESS TESTING UTILITY ---")
val pr = new Prime() // val value = 2147483647
val value = 200000
/* val time = br.measure(pr.run(value))
val intMax = 2147483647 println(time)
pr.run(intMax)
*/
val intMax = 2147483647
println(pr.measure())
} }

View File

@ -1,9 +1,9 @@
package com.nsrddyn.alu package com.nsrddyn.alu
import com.nsrddyn.tools.Measurable import com.nsrddyn.tools.Benchmark
class Prime() extends Measurable: class Prime() extends Benchmark:
/* /*
* Calculate all primes up to limit * Calculate all primes up to limit
@ -24,17 +24,22 @@ class Prime() extends Measurable:
* We want the function to be less optimized so that the CPU has more work == more stress * We want the function to be less optimized so that the CPU has more work == more stress
*/ */
def isPrime(n: Int): Boolean = { def isPrime(n: Int): Boolean = {
val start = measure()
if n <= 1 then false if n <= 1 then false
else !(2 to math.sqrt(n).toInt).exists(i => n % i == 0) else !(2 to math.sqrt(n).toInt).exists(i => n % i == 0)
} }
def run(n: Int): Unit = for i <- 0 to n do isPrime(i) def run(n: Int): Unit = for i <- 0 to n do isPrime(i)
def measure(): Long = {
// TODO: implement measure methode to measure the time that it takes to find that prime number // TODO: implement measure methode to measure the time that it takes to find that prime number
def measure(): Long ={
val start = System.nanoTime()
System.nanoTime() System.nanoTime()
val end = System.nanoTime()
start - end
} }

View File

@ -0,0 +1,14 @@
package com.nsrddyn.tools
class Benchmark {
// add a reference to a function
def measure(work: => Unit): Long = {
val start = System.nanoTime()
work
val end = System.nanoTime()
end - start
}
}

View File

@ -1,5 +0,0 @@
package com.nsrddyn.tools
trait Measurable {
def measure(): Long
}