From 5ad185e130463a241494ad26fe1706a3601f8400 Mon Sep 17 00:00:00 2001 From: Walter Oggioni Date: Sun, 8 Oct 2023 08:38:10 +0800 Subject: [PATCH] added benchmark --- benchmark/benchmark.nim | 33 +++++++++++++++++++++++++++++++++ benchmark/config.nims | 1 + src/mmath/bigint.nim | 4 ++-- src/mmath/rational.nim | 2 +- 4 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 benchmark/benchmark.nim create mode 100644 benchmark/config.nims diff --git a/benchmark/benchmark.nim b/benchmark/benchmark.nim new file mode 100644 index 0000000..770fd62 --- /dev/null +++ b/benchmark/benchmark.nim @@ -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) diff --git a/benchmark/config.nims b/benchmark/config.nims new file mode 100644 index 0000000..3bb69f8 --- /dev/null +++ b/benchmark/config.nims @@ -0,0 +1 @@ +switch("path", "$projectDir/../src") \ No newline at end of file diff --git a/src/mmath/bigint.nim b/src/mmath/bigint.nim index 4e1e406..52b0c6c 100644 --- a/src/mmath/bigint.nim +++ b/src/mmath/bigint.nim @@ -10,7 +10,7 @@ type mpz_ptr = ptr[mpz_t] cxface: include "" - # 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) diff --git a/src/mmath/rational.nim b/src/mmath/rational.nim index bfc01da..351f8bd 100644 --- a/src/mmath/rational.nim +++ b/src/mmath/rational.nim @@ -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)