continuing to optimize

This commit is contained in:
Danny Yoo 2012-02-10 14:24:42 -05:00
parent 6d035504af
commit d70db16956
4 changed files with 20 additions and 13 deletions

View File

@ -1,11 +1,13 @@
#lang planet dyoo/whalesong #lang planet dyoo/whalesong
(define (mylen x) (define (mylen x acc)
(cond (cond
[(empty? x) [(empty? x)
0] acc]
[else [else
(add1 (mylen (rest x)))])) (mylen (rest x) (add1 acc))]))
"computing length" "computing length"
(mylen (build-list 100000 (lambda (i) i))) (define v (build-list 1000000 (lambda (i) i)))
(printf "Built list\n")
(mylen v 0)
"done computing length" "done computing length"

View File

@ -264,18 +264,26 @@
var tortoise, hare; var tortoise, hare;
tortoise = hare = x; tortoise = hare = x;
if (hare === EMPTY) { if (hare === EMPTY) {
tortoise._isList = true;
return true; return true;
} }
while (true) { while (true) {
if (!(hare instanceof Cons)) { return false; } if (!(hare instanceof Cons)) { return false; }
if (tortoise instanceof Cons) { if (tortoise instanceof Cons) {
if (tortoise._isList === true) { return true; }
tortoise = tortoise.rest; tortoise = tortoise.rest;
} }
hare = hare.rest; hare = hare.rest;
if (hare instanceof Cons) { hare = hare.rest; } if (hare instanceof Cons) {
if (hare === EMPTY) { return true; } // optimization to get amortized linear time isList:
if (hare._isList) { tortoise._isList = true; return true; }
hare = hare.rest;
// optimization to get amortized linear time isList:
if (hare instanceof Cons && hare._isList) { tortoise._isList = true; return true; }
}
if (hare === EMPTY) {
// optimization to get amortized linear time isList:
tortoise._isList = true;
return true;
}
if (tortoise === hare) { return false; } if (tortoise === hare) { return false; }
} }
}; };

View File

@ -15,10 +15,6 @@
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
this.elts[i] = initialElements[i]; this.elts[i] = initialElements[i];
} }
} else {
for (i = 0; i < n; i++) {
this.elts[i] = undefined;
}
} }
this.mutable = true; this.mutable = true;
}; };

View File

@ -395,7 +395,8 @@
var frame = this.c[this.c.length - 1]; var frame = this.c[this.c.length - 1];
var marks = frame.marks; var marks = frame.marks;
var i; var i;
for (i = 0; i < marks.length; i++) { var l = marks.length;
for (i = 0; i < l; i++) {
if (key === marks[i][0]) { if (key === marks[i][0]) {
marks[i][1] = value; marks[i][1] = value;
return; return;