Last active
August 29, 2015 14:01
-
-
Save triplog/c20eb48d360b455ce124 to your computer and use it in GitHub Desktop.
関数を引数に渡して実行時間と結果の配列を返す
This file contains 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 characters
/* | |
checkFunc(sq,10) で [100,1000] | |
checkFunc(mul,10,20) で [200,2000] | |
が返ってくる([結果,実行時間],※実行時間はズレることもある) | |
*/ | |
//関数と引数から結果と実行時間を返す関数 | |
function checkFunc(func){ | |
//2番目以降の引数を配列として取得(?) | |
var args = Array.prototype.slice.call(arguments,1); | |
// console.log(func); | |
// console.log(args); | |
// 静的メソッドを使用 | |
var timer = Date.now(); | |
// 時間を計りたいイベントをここに置く: | |
var result = func.apply(window,args); | |
timer = Date.now() - timer; | |
//出力用配列オブジェクト | |
var array = [result,timer]; | |
// console.log(array); | |
return array; | |
} | |
//実行する関数の例,一瞬で終わるから無理やり待たせる | |
function sq(x){ | |
var timer = Date.now(); | |
//一定時間待つ | |
while(Date.now()-timer<1000) | |
continue; | |
return x*x; | |
} | |
function mul(x,y){ | |
var timer = Date.now(); | |
//一定時間待つ | |
while(Date.now()-timer<2000) | |
continue; | |
return x*y; | |
} | |
/* | |
参考 | |
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Date#Parameters | |
http://codezine.jp/article/detail/221 | |
http://www.ajaxtower.jp/js/window/index2.html | |
http://taiju.hatenablog.com/entry/20100515/1273903873 | |
https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment