when you compiled this on plan 9, you must have
either replaced stdlib.h with something else or have used
ape. i substituted libbio and get almost the same performance
you did on linux. i used my home cpu server, a amd64 revF 3800+.
cpu% 8c -FVw t.c
warning: t.c:64 set and not used: t
cpu% 8l -o t t.8
cpu% time t >/tmp/xyzw
2.08u 0.05s 2.36r t
i think the reason for the difference in performance is that somehow
your translation of print was calling write(2) for each integer printed.
assuming i understand the point of this program -- sorting a bunch of
integers, i rewrote this program using an array and qsort, which should
be more memory efficient and faster. here's what i get
cpu% time ./u >/tmp/xyz
0.08u 0.01s 0.22r ./u
- erik#include <u.h>
#include <libc.h>
#include <bio.h>
enum{
MAX = 200000,
};
typedef struct Bst Bst;
struct Bst{
int v;
Bst *menor;
Bst *maior;
};
Biobuf b;
Bst*
pbst(Bst *t)
{
if(t->menor != 0)
t->menor = pbst(t->menor);
Bprint(&b, "%d - ", t->v);
if(t->maior != 0)
t->maior = pbst(t->maior);
return t;
}
Bst*
bstini(int v)
{
Bst *t;
t = malloc(sizeof *t);
t->v = v;
t->maior = 0;
t->menor = 0;
return t;
}
Bst *
bstadd(int v, Bst *t)
{
if(t == 0)
t = bstini(v);
else if(v >= t->v)
t->maior = bstadd(v, t->maior);
else
t->menor = bstadd(v, t->menor);
return t;
}
void
main(void)
{
Bst *t;
int i;
Binit(&b, 1, OWRITE);
t = 0;
srand(time(0));
for(i = 0; i < MAX; i++)
t = bstadd(rand()%1000, t);
t = pbst(t);
exits("");
}#include <u.h>
#include <libc.h>
#include <bio.h>
enum{
MAX = 200000,
};
int
intcmp(void *a, void *b)
{
return *((int*)a)-*((int*)b);
}
void
main(void)
{
Biobuf b;
int *t, i;
Binit(&b, 1, OWRITE);
t = malloc(MAX*sizeof *t);
srand(time(0));
for(i = 0; i < MAX; i++)
t[i] = rand()%1000;
qsort(t, MAX, sizeof *t, intcmp);
for(i = 0; i < MAX; i++)
Bprint(&b, " - %d", t[i]);
exits("");
}