Skip to content

Commit

Permalink
fix: Updated bench to new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Nov 28, 2024
1 parent 0cda0ae commit eacfc9f
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 227 deletions.
36 changes: 18 additions & 18 deletions tests/bench/001-btree.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import "std";
import "math";

object Node {
Node? left = null,
Node? right = null,
left: Node? = null,
right: Node? = null,
}

fun bottomUpTree(int depth) > Node {
fun bottomUpTree(depth: int) > Node {
if (depth > 0) {
return Node{
left = bottomUpTree(depth - 1),
Expand All @@ -17,7 +17,7 @@ fun bottomUpTree(int depth) > Node {
return Node{};
}

fun itemCheck(Node tree) > int {
fun itemCheck(tree: Node) > int {
if (tree.left -> left) {
return 1 + itemCheck(left) + itemCheck(tree.right!);
} else {
Expand All @@ -27,22 +27,22 @@ fun itemCheck(Node tree) > int {
return 0;
}

fun btree(int N) > void {
int mindepth = 4;
int maxdepth = mindepth + 2;
fun btree(N: int) > void {
final mindepth = 4;
var maxdepth = mindepth + 2;
if (maxdepth < N) {
maxdepth = N;
}

int stretchdepth = maxdepth + 1;
Node stretchtree = bottomUpTree(stretchdepth);
final stretchdepth = maxdepth + 1;
final stretchtree = bottomUpTree(stretchdepth);
std\print("stretch tree of depth {stretchdepth},\t check: {itemCheck(stretchtree)}");

Node longlivedtree = bottomUpTree(maxdepth);
for (int depth = mindepth; depth < maxdepth; depth = depth + 2) {
int iterations = std\toInt(math\pow(x: 2.0, y: std\toDouble(maxdepth - depth + mindepth)) catch 0.0);
int check = 0;
for (int i = 0; i < iterations; i = i + 1) {
final longlivedtree = bottomUpTree(maxdepth);
for (depth: int = mindepth; depth < maxdepth; depth = depth + 2) {
final iterations = std\toInt(math\pow(x: 2.0, y: std\toDouble(maxdepth - depth + mindepth)) catch 0.0);
var check = 0;
foreach (_ in 0..iterations) {
check = check + itemCheck(bottomUpTree(depth));
}

Expand All @@ -52,8 +52,8 @@ fun btree(int N) > void {
std\print("long lived tree of depth {maxdepth}\t check: {itemCheck(longlivedtree)}");
}

fun main([str] args) > void !> any {
int N = if (args.len() > 0) std\parseInt(args[0]) ?? 3 else 3;

btree(N);
fun main(args: [str]) > void !> any {
btree(
std\parseInt(args[?0] ?? "3") ?? 3
);
}
48 changes: 25 additions & 23 deletions tests/bench/002-merkle.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import "math";
import "errors";

object Node {
Node? left = null,
Node? right = null,
int? value = null,
int? hash = null,
left: Node? = null,
right: Node? = null,
value: int? = null,
hash: int? = null,
}

fun makeTree(int depth) > Node {
fun makeTree(depth: int) > Node {
if (depth > 0) {
return Node{
left = makeTree(depth - 1),
Expand All @@ -22,7 +22,7 @@ fun makeTree(int depth) > Node {
};
}

fun check(Node tree) > bool {
fun check(tree: Node) > bool {
if (tree.hash != null) {
if (tree.value != null) {
return true;
Expand All @@ -34,7 +34,7 @@ fun check(Node tree) > bool {
return false;
}

fun calHash(Node tree) > void {
fun calHash(tree: Node) > void {
if (tree.hash == null) {
if (tree.value -> value) {
tree.hash = value;
Expand All @@ -47,30 +47,32 @@ fun calHash(Node tree) > void {
}
}

fun main([str] args) > void !> any {
int N = 6;
if (args.len() > 0) {
N = std\parseInt(args[0]) ?? 6;
}
fun main(args: [str]) > void !> any {
final N = std\parseInt(args[?0] ?? "6") ?? 6;

int mindepth = 4;
int maxdepth = mindepth + 2;
final mindepth = 4;
var maxdepth = mindepth + 2;
if (maxdepth < N) {
maxdepth = N;
}

int stretchdepth = maxdepth + 1;
Node stretchtree = makeTree(stretchdepth);
final stretchdepth = maxdepth + 1;
final stretchtree = makeTree(stretchdepth);
calHash(stretchtree);

std\print("stretch tree of depth {stretchdepth}\t root hash: {stretchtree.hash ?? 0} check: {check(stretchtree)}");

Node longlivedtree = makeTree(maxdepth);
for (int depth = mindepth; depth < maxdepth; depth = depth + 2) {
int iterations = std\toInt(math\pow(x: 2.0, y: std\toDouble(maxdepth - depth + mindepth)));
int sum = 0;
for (int i = 0; i < iterations; i = i + 1) {
Node t = makeTree(depth);
final longlivedtree = makeTree(maxdepth);
for (depth: int = mindepth; depth < maxdepth; depth = depth + 2) {
final iterations = std\toInt(
math\pow(
x: 2.0,
y: std\toDouble(maxdepth - depth + mindepth)
)
);
var sum = 0;
foreach (_ in 0..iterations) {
final t = makeTree(depth);
calHash(t);
sum = sum + t.hash ?? 0;
}
Expand All @@ -80,4 +82,4 @@ fun main([str] args) > void !> any {

calHash(longlivedtree);
std\print("long lived tree of depth {maxdepth}\t root hash: {longlivedtree.hash ?? 0} check: {check(longlivedtree)}");
}
}
125 changes: 61 additions & 64 deletions tests/bench/003-nbody.buzz
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import "std";
import "math";

final double pi = 3.141592653589793;
final double solarMass = 4.0 * pi * pi;
final double daysPerYear = 365.24;
final pi = 3.141592653589793;
final solarMass = 4.0 * pi * pi;
final daysPerYear = 365.24;

object Body {
double x,
double y,
double z,
double vx,
double vy,
double vz,
double mass,
x: double,
y: double,
z: double,
vx: double,
vy: double,
vz: double,
mass: double,
}

fun advance([Body] bodies, int nbody, double dt) > void {
for (int i = 0; i < nbody; i = i + 1) {
Body bi = bodies[i];
double bix = bi.x;
double biy = bi.y;
double biz = bi.z;
double bimass = bi.mass;
double bivx = bi.vx;
double bivy = bi.vy;
double bivz = bi.vz;

for (int j = i + 1; j < nbody; j = j + 1) {
Body bj = bodies[j];
double dx = bix - bj.x;
double dy = biy - bj.y;
double dz = biz - bj.z;
double dist2 = dx * dx + dy * dy + dz * dz;
double mag = math\sqrt(dist2);
fun advance(bodies: [Body], nbody: int, dt: double) > void {
foreach (i in 0..nbody) {
final bi = bodies[i];
final bix = bi.x;
final biy = bi.y;
final biz = bi.z;
final bimass = bi.mass;
var bivx = bi.vx;
var bivy = bi.vy;
var bivz = bi.vz;

foreach (j in (i + 1)..nbody) {
final bj = bodies[j];
final dx = bix - bj.x;
final dy = biy - bj.y;
final dz = biz - bj.z;
final dist2 = dx * dx + dy * dy + dz * dz;
var mag = math\sqrt(dist2);
mag = dt / (mag * dist2);
double bm = bj.mass * mag;
var bm = bj.mass * mag;

bivx = bivx - (dx * bm);
bivy = bivy - (dy * bm);
Expand All @@ -54,36 +54,36 @@ fun advance([Body] bodies, int nbody, double dt) > void {
}
}

fun energy([Body] bodies, int nbody) > double {
double e = 0.0;
for (int i = 0; i < nbody; i = i + 1) {
Body bi = bodies[i];
double vx = bi.vx;
double vy = bi.vy;
double vz = bi.vz;
double bim = bi.mass;
fun energy(bodies: [Body], nbody: int) > double {
var e = 0.0;
foreach (i in 0..nbody) {
final bi = bodies[i];
final vx = bi.vx;
final vy = bi.vy;
final vz = bi.vz;
final bim = bi.mass;
e = e + (0.5 * bim * (vx * vx + vy * vy + vz * vz));
for (int j = i + 1; j < nbody; j = j + 1) {
Body bj = bodies[j];
double dx = bi.x - bj.x;
double dy = bi.y - bj.y;
double dz = bi.z - bj.z;
double distance = math\sqrt(dx * dx + dy * dy + dz * dz);
foreach (j in (i + 1)..nbody) {
final bj = bodies[j];
final dx = bi.x - bj.x;
final dy = bi.y - bj.y;
final dz = bi.z - bj.z;
final distance = math\sqrt(dx * dx + dy * dy + dz * dz);
e = e - ((bim * bj.mass) / distance);
}
}

return e;
}

fun offsetMomentum([Body] b, int nbody) > void {
double px = 0.0;
double py = 0.0;
double pz = 0.0;
fun offsetMomentum(b: [Body], nbody: int) > void {
var px = 0.0;
var py = 0.0;
var pz = 0.0;

for (int i = 0; i < nbody; i = i + 1) {
Body bi = b[i];
double bim = bi.mass;
foreach (i in 0..nbody) {
final bi = b[i];
final bim = bi.mass;

px = px + (bi.vx * bim);
py = py + (bi.vy * bim);
Expand All @@ -95,8 +95,8 @@ fun offsetMomentum([Body] b, int nbody) > void {
b[0].vz = -pz / solarMass;
}

fun main([str] args) > void {
Body sun = Body{
fun main(args: [str]) > void {
final sun = Body{
x = 0.0,
y = 0.0,
z = 0.0,
Expand All @@ -106,7 +106,7 @@ fun main([str] args) > void {
mass = solarMass,
};

Body jupiter = Body{
final jupiter = Body{
x = 4.84143144246472090,
y = -1.16032004402742839,
z = -0.103622044471123109,
Expand All @@ -116,7 +116,7 @@ fun main([str] args) > void {
mass = 0.000954791938424326609 * solarMass,
};

Body saturn = Body{
final saturn = Body{
x = 8.34336671824457987,
y = 4.12479856412430479,
z = -0.403523417114321381,
Expand All @@ -126,7 +126,7 @@ fun main([str] args) > void {
mass = 0.00028588598066613 * solarMass,
};

Body uranus = Body{
final uranus = Body{
x = 12.894369562139,
y = -15.111151401699,
z = -0.22330757889266,
Expand All @@ -136,7 +136,7 @@ fun main([str] args) > void {
mass = 0.0000436624404335156298 * solarMass,
};

Body neptune = Body{
final neptune = Body{
x = 15.379697114851,
y = -25.9193146099879641,
z = 0.179258772950371181,
Expand All @@ -146,20 +146,17 @@ fun main([str] args) > void {
mass = 0.0000515138902046611451 * solarMass,
};

[Body] bodies = [ sun, jupiter, saturn, uranus, neptune ];
final bodies = [ sun, jupiter, saturn, uranus, neptune ];

int N = 1000;
if (args.len() > 0) {
N = std\parseInt(args[0]) ?? 1000;
}
int nbody = bodies.len();
final N = std\parseInt(args[?0] ?? "1000") ?? 1000;
final nbody = bodies.len();

offsetMomentum(bodies, nbody: nbody);
std\print("{energy(bodies, nbody: nbody)}");

for (int i = 0; i < N; i = i + 1) {
foreach (_ in 0..N) {
advance(bodies, nbody: nbody, dt: 0.01);
}

std\print("{energy(bodies, nbody: nbody)}");
}
}
Loading

0 comments on commit eacfc9f

Please sign in to comment.