-
-
Save skaarlcooper/f965bf64d9c741446f78bf9bb47c8025 to your computer and use it in GitHub Desktop.
Revisions
-
craigbeck revised this gist
Sep 16, 2011 . 1 changed file with 45 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,29 @@ var $w = function(string) { return string.split(' '); }; var asInts = function(arr) { var results=[]; for(var i=0, m=arr.length; i<m; i++) results.push(parseInt(arr[i], 10)); return results; }; Array.prototype.map = function(func) { var results=[]; for(i=0, m=this.length; i<m; i++) results.push(func(this[i])); return results; }; var time = function(subject, label) { this.seq = 1; var name = label || this.seq++; console.time(name); subject(); console.timeEnd(name); } var primes = asInts($w('524287 9369319 2147483647')); var notprimes = asInts($w('4 6 8 15 21 524284 9369312')); function isPrime(num) { if (!num) return false; var prime = num != 1; @@ -10,11 +36,22 @@ function isPrime(num) { return prime; } Function.prototype.cached = function() { var self = this; if (!self.cache) self.cache = {}; return function(o) { if (self.cache[o] == null) { console.log('cache miss'); self.cache[o] = self(o); } else { console.log('cache hit'); } return self.cache[o]; } }; isPrimeCached = isPrime.cached(); time(function(){ primes.map(isPrime); }, 'isprime-no-cache'); time(function(){ primes.map(isPrimeCached); }, 'isprime-with-cache first'); time(function(){ primes.map(isPrimeCached); }, 'isprime-with-cache repeated'); -
craigbeck renamed this gist
Sep 16, 2011 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
craigbeck revised this gist
Sep 16, 2011 . 1 changed file with 19 additions and 40 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,41 +1,20 @@ function isPrime(num) { if (!num) return false; var prime = num != 1; for ( var i = 2; i < num; i++ ) { if ( num % i == 0 ) { prime = false; break; } } return prime; } Function.prototype.cached = function(o) { if (!this.cache) this.cache = {}; if (this.cache[o] == null) { console.log('cache miss'); this.cache[o] = this(o); } else { console.log('cache hit'); } return this.cache[o]; }; -
craigbeck created this gist
Sep 15, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ <!DOCTYPE html> <html> <head> <script type="text/javascript"> var $w = function(string) { return string.split(' '); }; var asInts = function(arr) { var results=[]; for(var i=0, m=arr.length; i<m; i++) results.push(parseInt(arr[i], 10)); return results; }; var someprimes = asInts($w('524287 9369319 2147483647')); function isPrime(num) { if (!num) return false; var prime = num != 1; for ( var i = 2; i < num; i++ ) { if ( num % i == 0 ) { prime = false; break; } } return prime; } Function.prototype.cached = function(o) { if (!this.cache) this.cache = {}; if (this.cache[o] == null) { console.log('cache miss'); this.cache[o] = this(o); } else { console.log('cache hit'); } return this.cache[o]; }; </script> </head> <body> </body> </html>