Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use a better recursive algorithm, release the loop less often and sto…

…p cheating with memoisation (issue #7)
  • Loading branch information...
commit d52caf69a0fc83162ace531a30cd0f042ca365a1 1 parent 334cfd4
@sky-glenjamin sky-glenjamin authored
Showing with 32 additions and 31 deletions.
  1. +17 −17 README.md
  2. +14 −13 app.js
  3. +1 −1  package.json
View
34 README.md
@@ -69,31 +69,31 @@ Yes.
Document Length: 21 bytes
Concurrency Level: 50
- Time taken for tests: 1.855 seconds
+ Time taken for tests: 2.114 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 1420000 bytes
HTML transferred: 210000 bytes
- Requests per second: 5390.22 [#/sec] (mean)
- Time per request: 9.276 [ms] (mean)
- Time per request: 0.186 [ms] (mean, across all concurrent requests)
- Transfer rate: 747.47 [Kbytes/sec] received
+ Requests per second: 4731.49 [#/sec] (mean)
+ Time per request: 10.568 [ms] (mean)
+ Time per request: 0.211 [ms] (mean, across all concurrent requests)
+ Transfer rate: 656.12 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
- Connect: 0 0 0.2 0 4
- Processing: 1 9 4.7 9 26
- Waiting: 1 9 4.7 9 26
- Total: 1 9 4.7 9 26
+ Connect: 0 0 0.3 0 5
+ Processing: 2 10 3.0 10 28
+ Waiting: 2 10 3.0 10 28
+ Total: 3 10 3.0 10 28
Percentage of the requests served within a certain time (ms)
- 50% 9
+ 50% 10
66% 11
- 75% 13
- 80% 14
- 90% 15
- 95% 16
- 98% 19
- 99% 22
- 100% 26 (longest request)
+ 75% 12
+ 80% 13
+ 90% 14
+ 95% 15
+ 98% 18
+ 99% 20
+ 100% 28 (longest request)
View
27 app.js
@@ -2,22 +2,23 @@ var async = require('async');
var app = require('express').createServer();
var fibonacci = function(n, callback) {
- if (n <= 2) {
+ var inner = function(n1, n2, i) {
+ if (i > n) {
+ callback(null, n2);
+ return;
+ }
+ var func = (i % 100) ? inner : inner_tick;
+ func(n2, n1 + n2, i + 1);
+ }
+ var inner_tick = function(n1, n2, i) {
+ process.nextTick(function() { inner(n1, n2, i); });
+ }
+ if (n == 1 || n == 2) {
callback(null, 1);
- return;
+ } else {
+ inner(1, 1, 3);
}
- async.series({
- n2: function(next) {
- process.nextTick(function() { fibonacci(n - 2, next); });
- },
- n1: function(next) {
- process.nextTick(function() { fibonacci(n - 1, next); });
- },
- }, function(err, results) {
- callback(null, results.n1 + results.n2);
- });
}
-fibonacci = async.memoize(fibonacci);
app.get('/:n', function(req, res) {
fibonacci(req.params.n, function(err, number) {
View
2  package.json
@@ -10,6 +10,6 @@
"engines": {
"node": "~v0.4.12"
},
- "dependencies": {"express":"2.4.x", "async":"0.1.x"},
+ "dependencies": {"express":"2.4.x"},
"devDependencies": {}
}
Please sign in to comment.
Something went wrong with that request. Please try again.