開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- JavaScript (プログラミング言語)
- SpiderMonkey, Node.js(V8) (JavaScript engine)
Data Structures and Algorithms With Javascript (Michael McMillan(著)、O'Reilly Media)のChapter 13(Searching Algorithms)、Exercises 1.(No. 6070)を解いてみる。
Exercises 1.(No. 6070)
JavaScript(Emacs)
/*jslint browser : true, continue : true,
devel : true, indent : 4, maxerr : 50,
newcap : true, nomen : true, plusplus : false,
regexp : true, sloppy : true, vars : false,
white : true
*/
/*global */
var nums = [],
i,
num = 23,
len = 1000000,
start,
stop;
Array.prototype.dispArr = function () {
var i,
max,
str = '';
for (i = 0, max = this.length; i < max; i += 1) {
str += this[i] + ' ';
if (i % 10 === 9) {
str += '\n';
}
}
if (i % 10 !== 0) {
str += '\n';
}
console.log(str);
};
Array.prototype.seqSearch = function (data) {
var i,
len = this.length;
for (i = len - 1; i >= 0; i -= 1) {
if (this[i] === data) {
return true;
}
}
return false;
};
for (i = 0; i < len; i += 1) {
nums[i] = 0;
}
nums.push(num);
start = new Date().getTime();
nums.seqSearch(num);
stop = new Date().getTime();
console.log(stop - start);
nums.pop();
nums.shift(num);
start = new Date().getTime();
nums.seqSearch(num);
stop = new Date().getTime();
console.log(stop - start);
出力結果(Terminal, shell, SpiderMonkey)
$ jslint sample1.js
sample1.js is OK.
$ node sample1.js
1
16
$
0 コメント:
コメントを投稿