Skip to content

Commit

Permalink
Merge pull request #288 from ryanwmao/main
Browse files Browse the repository at this point in the history
Quickselect benchmark
  • Loading branch information
sampsyo authored Sep 7, 2023
2 parents 4923027 + dd62bcf commit 361229d
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
123 changes: 123 additions & 0 deletions benchmarks/mem/quickselect.bril
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Quick select algorithm
# Inputs: An unordered array of 6 elements and a number k
# Output: kth smallest number in the array
# Adopted pack and print_array code from bubblesort by Jiajie Li

@pack(size: int, n1: int, n2: int, n3: int, n4: int, n5: int, n6: int) : ptr<int> {
one: int = const 1;
i: int = const 0;
array: ptr<int> = alloc size;
# Pack data into array manually. Cannot use loop because of the different var name.
loc: ptr<int> = ptradd array i;
store loc n1;
i: int = add i one;
loc: ptr<int> = ptradd array i;
store loc n2;
i: int = add i one;
loc: ptr<int> = ptradd array i;
store loc n3;
i: int = add i one;
loc: ptr<int> = ptradd array i;
store loc n4;
i: int = add i one;
loc: ptr<int> = ptradd array i;
store loc n5;
i: int = add i one;
loc: ptr<int> = ptradd array i;
store loc n6;
ret array;
}

@print_array(array: ptr<int>, size: int) {
i: int = const 0;
one: int = const 1;
.loop:
cond: bool = lt i size;
br cond .body .done;
.body:
loc: ptr<int> = ptradd array i;
val: int = load loc;
print val;
.loop_end:
i: int = add i one;
jmp .loop;
.done:
ret;
}


@partition(array: ptr<int>, l: int, r: int): int {
one: int = const 1;
pivotloc: ptr<int> = ptradd array r;
pivot: int = load pivotloc;
i: int = id l;
j: int = id l;
.loop:
cond: bool = lt j r;
br cond .body .done;
.body:
curloc: ptr<int> = ptradd array j;
cur: int = load curloc;
swap: bool = le cur pivot;
br swap .swap_j .loop_end;
.swap_j:
iloc: ptr<int> = ptradd array i;
ival: int = load iloc;
store curloc ival;
store iloc cur;
i: int = add i one;
.loop_end:
j: int = add j one;
jmp .loop;
.done:
iloc: ptr<int> = ptradd array i;
ival: int = load iloc;
store iloc pivot;
store pivotloc ival;
ret i;
}

@quickselect(array: ptr<int>, l: int, r: int, k: int): int {
one: int = const 1;
index: int = call @partition array l r;
ipos: int = sub index l;
kpos: int = sub k one;
ieqk: bool = eq ipos kpos;
br ieqk .found .not_found;
.found:
iloc: ptr<int> = ptradd array index;
i: int = load iloc;
ret i;
.not_found:
igtk: bool = gt ipos kpos;
br igtk .greater .less;
.greater:
newr: int = sub index one;
i: int = call @quickselect array l newr k;
ret i;
.less:
newl: int = add index one;
newk: int = sub k index;
newk: int = add newk l;
newk: int = sub newk one;
i: int = call @quickselect array newl r newk;
ret i;
}

@main() {
k: int = const 4;
n1: int = const 97;
n2: int = const 108;
n3: int = const 98;
n4: int = const 101;
n5: int = const 114;
n6: int = const 116;

zero: int = const 0;
five: int = const 5;
size: int = const 6;
array: ptr<int> = call @pack size n1 n2 n3 n4 n5 n6;
output: int = call @quickselect array zero five k;
print output;
free array;
}
1 change: 1 addition & 0 deletions benchmarks/mem/quickselect.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
108
1 change: 1 addition & 0 deletions benchmarks/mem/quickselect.prof
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
total_dyn_inst: 279
1 change: 1 addition & 0 deletions docs/tools/bench.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ The current benchmarks are:
* `primitive-root`: Computes a [primitive root][primitive_root] modulo a prime number input.
* `pythagorean_triple`: Prints all Pythagorean triples with the given c, if such triples exist. An intentionally very naive implementation.
* `quadratic`: The [quadratic formula][qf], including a hand-rolled implementation of square root.
* `quickselect`: Find the kth smallest element in an array using the quickselect algorithm.
* `quicksort`: [Quicksort using the Lomuto partition scheme][qsort].
* `quicksort-hoare`: Quicksort using [Hoare partioning][qsort-hoare] and median of three pivot selection.
* `recfact`: Compute *n!* using recursive function calls.
Expand Down

0 comments on commit 361229d

Please sign in to comment.