ref: 599f5d31aae2b1f3b09df815e5fc70ccd6cf387a
parent: eb37a4241e5ea1da1c4a7e4614f44888fdbe1511
author: qwx <qwx@sciops.net>
date: Thu Jun 2 18:56:26 EDT 2022
test/pheap: fix, make faster and more useful
--- a/test/pheap.dec.c
+++ b/test/pheap.dec.c
@@ -1,5 +1,6 @@
#include <u.h>
#include <libc.h>
+#include <bio.h>
#include "asif.h"
debuglevel = Logtrace;
enum{
@@ -6,14 +7,24 @@
Nnodes = 10000,
};
-void main(void) {
- Pairheap *queue, *a, **t, **p;
- int fd, i;
+void
+main(int argc, char **argv)
+{
double v;
+ char *suff, name[128];
+ Pairheap *queue, *a, **t, **p;
+ Biobuf *bf, *ubf;
+ ARGBEGIN{
+ }ARGEND
+ suff = *argv == nil ? "" : *argv;
t = emalloc(Nnodes * sizeof *t);
- if((fd = create("res", OWRITE, 0644)) < 0)
- sysfatal("create: %r");
+ snprint(name, sizeof name, "ph.unsorted%s", suff);
+ if((ubf = Bopen(name, OWRITE)) == nil)
+ sysfatal("Bopen: %r");
+ snprint(name, sizeof name, "ph.sorted%s", suff);
+ if((bf = Bopen(name, OWRITE)) == nil)
+ sysfatal("Bopen: %r");
queue = nil;
srand(time(nil));
for(p=t; p<t+Nnodes; p++){
@@ -20,14 +31,15 @@
v = frand() * 1e6;
*p = pushqueue(v, nil, &queue);
if(frand() < 0.5){
- a = t[nrand(p-t)];
- decreasekey(a, a->n-frand()*1e2, &queue);
+ a = p == t ? *p : t[nrand(p-t)];
+ decreasekey(a, frand() * 1e2, &queue);
}
}
for(p=t; p<t+Nnodes; p++)
- fprint(fd, "%.2f\n", (*p)->n);
- close(fd);
+ Bprint(ubf, "%.2f\n", (*p)->n);
+ Bterm(ubf);
while((a = popqueue(&queue)) != nil)
- print("%.2f\n", a->n);
+ Bprint(bf, "%.2f\n", a->n);
+ Bterm(bf);
exits(nil);
}