From 421c6c3c93c4f68c808aaabc6b2d8fac06674321 Mon Sep 17 00:00:00 2001 From: nasr Date: Thu, 20 Nov 2025 18:45:53 +0100 Subject: feature[benchmark]: time measurement benchmark not a good way of testing things, but it's a way and the first way --- src/main/scala/com/nsrddyn/cpu/ALU/Prime.scala | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/main/scala/com/nsrddyn/cpu') diff --git a/src/main/scala/com/nsrddyn/cpu/ALU/Prime.scala b/src/main/scala/com/nsrddyn/cpu/ALU/Prime.scala index 47c83f1..effedef 100644 --- a/src/main/scala/com/nsrddyn/cpu/ALU/Prime.scala +++ b/src/main/scala/com/nsrddyn/cpu/ALU/Prime.scala @@ -1,9 +1,9 @@ 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 @@ -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 */ + def isPrime(n: Int): Boolean = { + val start = measure() if n <= 1 then false 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 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() - + val end = System.nanoTime() + start - end } -- cgit v1.2.3-70-g09d2