Skip to content

Instantly share code, notes, and snippets.

@leodemoura
Created August 15, 2018 23:40
Show Gist options
  • Save leodemoura/1171cda9b445e7651a95dbfcb586e4ee to your computer and use it in GitHub Desktop.
Save leodemoura/1171cda9b445e7651a95dbfcb586e4ee to your computer and use it in GitHub Desktop.
static object * map1(object * l) {
if (obj_tag(l) == 0) {
return l;
} else {
object * h = cnstr_obj(l, 0);
object * t = cnstr_obj(l, 1);
object * new_h = box(unbox(h) + 1);
object * new_t = map1(t);
object * r = alloc_cnstr(1, 2, 0);
cnstr_set_obj(r, 0, new_h);
cnstr_set_obj(r, 1, new_t);
return r;
}
}
static object * map2(object * l) {
if (obj_tag(l) == 0) {
return l;
} else {
object * h = cnstr_obj(l, 0);
object * t = cnstr_obj(l, 1);
inc(t);
dec(l);
object * new_h = box(unbox(h) + 1);
object * new_t = map1(t);
object * r = alloc_cnstr(1, 2, 0);
cnstr_set_obj(r, 0, new_h);
cnstr_set_obj(r, 1, new_t);
return r;
}
}
static object * map3(object * l) {
if (obj_tag(l) == 0) {
return l;
} else {
object * h = cnstr_obj(l, 0);
object * t = cnstr_obj(l, 1);
bool s = is_shared(l);
if (s) {
inc(t);
dec(l);
}
object * new_h = box(unbox(h) + 1);
object * new_t = map1(t);
object * r;
if (s) {
r = alloc_cnstr(1, 2, 0);
} else {
r = l;
}
cnstr_set_obj(r, 0, new_h);
cnstr_set_obj(r, 1, new_t);
return r;
}
}
static object * mk_list(unsigned n) {
object * r = box(0);
while (n > 0) {
object * h = box(n);
object * new_r = alloc_cnstr(1, 2, 0);
cnstr_set_obj(new_r, 0, h);
cnstr_set_obj(new_r, 1, r);
r = new_r;
--n;
}
return r;
}
static void tst7(unsigned n) {
timeit timer(std::cout, "tst7");
object * l = mk_list(10000);
for (unsigned i = 0; i < n; i++) {
object * l2 = map3(l);
// dec(l);
l = l2;
}
dec(l);
}
static void tst8(unsigned n) {
timeit timer(std::cout, "tst7");
object * l = mk_list(10000);
inc(l);
for (unsigned i = 0; i < n; i++) {
inc(l);
object * l2 = map3(l);
dec(l2);
}
dec(l);
}
int main() {
save_stack_info();
initialize_util_module();
// tst1();
// tst2();
// tst3();
// tst4();
// tst6(1000000, 10000);
tst8(10000);
finalize_util_module();
return has_violations() ? 1 : 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment