added benchmark

This commit is contained in:
2023-10-08 08:38:10 +08:00
parent 88574a8b5b
commit 5ad185e130
4 changed files with 37 additions and 3 deletions

33
benchmark/benchmark.nim Normal file
View File

@@ -0,0 +1,33 @@
import std/random
import std/os
import std/parseutils
from mmath/rational import Rational, newRational, `$`, zero, abs, `<`, `*`, `-`, `/=`, `+`, `+=`
from mmath/hmatrix import newHMatrix, clone, lup, lu_solve, `*`, `-`, `$`
from mmath/hvector import newHvector, `-`, norm
from mmath/bigint import BigInt, newBigInt, `of`, one, zero, abs, `*`, `mod`, `div`, `-`, `+`, `$`, `==`
let argv = commandLineParams()
let size = block:
var n : int = 0
if argv.len() > 0:
discard parseInt(argv[0], n)
else:
n = 3
n
proc nextInt(r : var Rand, min : int, max : int) : int = min + r.rand(max - min)
var rand = initRand(101325)
let mtx = block:
let valueGenerator = proc(i : int, j : int): Rational[BigInt] =
return newRational[BigInt](BigInt.of(rand.nextInt(-1000 * size, 1000 * size).int64), BigInt.of((1000 * size).int64))
newHMatrix[Rational[BigInt]](size, size, valueGenerator)
var lu = mtx.clone()
let pivot = lu.lup()
let b = block:
let generator = proc(i : int) : Rational[BigInt] = newRational(BigInt.of(rand.nextInt(0, size)), BigInt.of(size))
newHVector[Rational[BigInt]](size, generator)
let x = lu.lu_solve(b, pivot)
let error = mtx * x - b
echo $norm(error)

1
benchmark/config.nims Normal file
View File

@@ -0,0 +1 @@
switch("path", "$projectDir/../src")

View File

@@ -10,7 +10,7 @@ type mpz_ptr = ptr[mpz_t]
cxface:
include "<gmp.h>"
# proc argp_parse*(argp : ptr[Argp], argc : cint, argv : cstringArray, flags : cint, arg_index : cint, ctx : pointer) : cint
proc mpz_init(state : mpz_ptr) : void
proc mpz_clear(state : mpz_ptr) : void
proc mpz_set_si(self : mpz_ptr, value : clong) : void
@@ -50,7 +50,7 @@ cxface:
libs:
gmp
proc `=destroy`*(n: var mpz_t) =
proc `=destroy`*(n: mpz_t) =
let mpz : mpz_ptr = addr(n)
mpz_clear(mpz)

View File

@@ -31,7 +31,7 @@ proc simplify*[T](self : var Rational[T]) : void =
self.num = num
self.den = den
proc newRational*[T](num : T) : Rational[T] = Rational[T](num: num, den: T(1))
proc newRational*[T](num : T) : Rational[T] = Rational[T](num: num, den: T.one)
proc newRational*[T](num : T, den: T) : Rational[T] = Rational[T](num: num, den: den)
proc zero*[T](_: type[Rational[T]]): Rational[T] = newRational(T.zero, T.one)