• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:素朴な疑問です)

関数実行速度計測の自動追加

このQ&Aのポイント
  • 関数やオブジェクトに一律して同じ処理を自動的に追加する方法として、関数実行速度の計測を考えています。
  • 具体的な実装方法として、関数内で実行開始時と終了時のタイムスタンプを取得し、差分を計算することで実行時間を計測します。
  • この方法を採用すれば、関数を定義するだけで自動的に実行時間の計測が行われるようになります。

質問者が選んだベストアンサー

  • ベストアンサー
  • dyna_1550
  • ベストアンサー率34% (122/353)
回答No.1

JavaScriptはオブジェクト指向で、クラス定義ができます。 継承やオーバーライドもできるので、それを利用すれば可能と思います。 以下のソースは、テストしていないので、多分にバグがあるかもしれませんが・・ オーバーライドでは、以下のような感じになると思います。 myFunction()という仮の関数を作成し、後でオーバーライドします。 // 共通クラスを作る function baseModule() {   this.action() {    startTime = (new Date()).getMilliseconds();    // あとから実装される処理     this.myFunction();    endTime = (new Date()).getMilliseconds();    exeTime = endTime - startTime;    }    this.myFunction() {      // Nothing    } } 自動的に追加する箇所は以下のような感じ。 var foo = new baseModule() ; // 関数をオーバーライド foo.myFunction() {   for(i=0;i<10000;i++){}; } // 実行 foo.action();

t_netbug
質問者

お礼

やっぱり提示していただいたような書き方になってしまいますよね…。 どうもありがとうございます!

t_netbug
質問者

補足

書き込み数的にも、ご提示頂いた書き方が最高系のようですね! 質問締め切ります! ありがとうございました!

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • Ruby の記述方法を教えてください。

    {"utf8"=>"✓", "authenticity_token"=>"PJzalm4QwBv3vj51Hgs/wJTorA1X7fsCntivSAo4d6g=", "studylogheader"=>{"studydate(1i)"=>"2011", "studydate(2i)"=>"6", "studydate(3i)"=>"7", "member_id"=>"902806575", "starttime"=>"1000", "endtime"=>"1600", "comment_member"=>"comment_member1", "comment_staff"=>"comment_staff", "comment_parent"=>"comment_parent", "staff_id"=>"679619944", "delete_flag"=>"0"}, "studylogdetails"=>[{"starttime"=>"1300", "endtime"=>"1400", "place"=>"totto", "studycontent"=>"aaaaa"}, {"starttime"=>"1200", "endtime"=>"1300", "place"=>"totto", "studycontent"=>"aaaaa"}, {"starttime"=>"1100", "endtime"=>"1200", "place"=>"totto", "studycontent"=>"aaaaa"}, {"starttime"=>"1500", "endtime"=>"1600", "place"=>"totto", "studycontent"=>"aaaaa"}, {"starttime"=>"1000", "endtime"=>"1100", "place"=>"totto", "studycontent"=>"aaaaa"}, {"starttime"=>"1400", "endtime"=>"1500", "place"=>"totto", "studycontent"=>"aaaaa"}], "commit"=>"Save changes"} このような params から、 01: @studylogheader = Studylogheader.new(params[:studylogheader]) 02: @studylogheader.studylogdetails = Array.new(Studylogdetail.new(params[:studylogdetails])) このように構造体を取得したいのです。 この 02 のところでエラーになってしまいます。 エラーは、 can't convert Studylogdetail into Integer です。わかる方書き込みお願いします。

    • ベストアンサー
    • Ruby
  • VBAの処理時間表示で小数点以下を四捨五入したい。

    VBAで処理の時間を終了後にメッセージボックスに表示するのですが、以下のようにすると小数点以下沢山の数があります。処理の時間が短くこれが時間なのかシリアル値であるのか判別つきません。時間であれば小数点以下をなくしたいです。シリアル値であればそれを時間に直したいです。教えてください。 Dim Time, StartTime, EndTime As Date starttime = Now ... ... ... endtime =Now Time =endtime - starttime msgbox ・・・

  • JS初心者です。このコードの式を教えてください

    下記はストップウォッチのコードとなります 「startTime = startTime + (new Date()).getTime() - stopTime;」 ここの式の意味がわかりません。 startTimeは 「if (!startTime) { startTime = (new Date()).getTime(); }」 の部分で、0秒にセットされている?と思います。 ですのでストップウォッチをスタートした1秒後にストップを作動させ、再度スタートさせるときの時間は 「startTime = startTime + (new Date()).getTime() - stopTime;」つまり 「startTime = 0秒 + (new Date()).getTime() - 1秒;」 と言い換えられると思います。単純に考えて1秒でストップさせてスタートする時刻も1秒目からですので、自ずと(new Date()).getTime()の中身は2秒になっちゃうと思っているのですが、どこからそのような数字がくるんだよということで絶対に違うと思います。 色々とわかりません。どなたかこの式を教えてください! <!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>ストップウォッチ</title> </head> <body> <h1>ストップウォッチ</h1> <div id="sec" style="font-size:128px">0.00</div> <input type="button" value="Start!" id="run"> <input type="button" value="Stop!" id="stop"> <input type="button" value="Reset!" id="reset"> <script> (function(){ var startTime, stopTime, running = false, timerId; document.getElementById('run').onclick = function(){ run(); } document.getElementById('stop').onclick = function(){ stop(); } document.getElementById('reset').onclick = function(){ reset(); } function run() { if(running) return; running = true; if (stopTime) { startTime = startTime + (new Date()).getTime() - stopTime; } if (!startTime) { startTime = (new Date()).getTime(); } timer(); } function timer() { document.getElementById('sec').innerHTML = (((new Date()).getTime() - startTime) / 1000).toFixed(2); timerId = setTimeout(function() { timer(); }, 100); } function stop() { if(!running) return false; running = false; clearTimeout(timerId); stopTime = (new Date()).getTime(); } function reset() { if(running) return; startTime = undefined; document.getElementById('sec').innerHTML = '0.00'; } }()); </script> </body> </html>

  • JavaScriptのsetTimeoutについて

    setTimeoutのタイマー処理の仕様を調べています。 var func = function () { alert(1); }; setTimeout(func, 1000); alert(2); このコードを実行した場合、alert(2)→alert(1)の順で処理されるのは当然ですが、 alert(2)を開いたまま1秒以上待ち、そこでOKボタンを押すとどうなるか、という問題です。 Firefoxの実装については下記ページで分かりました。 http://takoyakim.tumblr.com/post/10875885/firefox-settimeout 先ほどのコードの1000ミリ秒の経過処理は、タイマースレッドという専用スレッドで扱うため、 alert(2)で1000ミリ秒以上止めた後にOKボタンを押すと、即座にalert(1)が開きます。 しかしIEの場合はそうではなく、alert(2)を閉じてから約1000ミリ秒後にalert(1)が開きました。 alert(2)を開いたままどれだけ待機しても変わりません。 ChromeやSafariもIEと同じようです。 ですがこの結果は、次のコードの実行結果と矛盾してしまいました。 var heavyFunc = function () { for(var i = 0; i < 100000; i++) new Date(); } var startTime = +new Date(); var callCount = 0; var testFunc = function (x) { heavyFunc(); // (1) if (++callCount < 5) setTimeout(testFunc, 100); else alert(+new Date() - startTime); //heavyFunc(); // (2) }; testFunc(); heavyFuncという重い処理を、setTimeoutの前後のどちらに置くかという実験コードです。 setTimeoutのタイマー処理がFirefoxのように別スレッドで実行されているのであれば、 先にsetTimeoutを呼び出してからheavyFuncを実行した方が速く処理が完了するだろう、という想定です。 そして結果は、FirefoxだけでなくIEとChromeでも先にsetTimeoutを呼んだ方が速い、となりました。 結局どのブラウザも専用のタイマースレッドを実装しており、 alert関数が特殊(タイマースレッドもブロックする)なだけ、ということなのでしょうか? それとも私が何か見落としているのでしょうか。

  • Rails3 の記述方法を教えてください。

    {"utf8"=>"✓", "authenticity_token"=>"bqoR9tW+GGskvv4MRTtd1OikjwPKLeACkl7S/Pv55jE=", "studylogheader"=>{"studydate"=>"2011-06-07", "member_id"=>"902806575", "comment_member"=>"頑張ったけど、だめだった。もうしんどい。", "comment_staff"=>"comment_staff01", "comment_parent"=>"comment_parent01"}, "studylogdetails"=>[{"starttime"=>{"(1i)"=>"2011", "(2i)"=>"6", "(3i)"=>"17", "(4i)"=>"11", "(5i)"=>"00"}, "endtime"=>{"(1i)"=>"2011", "(2i)"=>"6", "(3i)"=>"17", "(4i)"=>"12", "(5i)"=>"00"}, "place"=>"totto", "studycontent"=>"aaaaa"}, {"place"=>"totto", "studycontent"=>"aaaaa", "starttime"=>{"(1i)"=>"2011", "(2i)"=>"6", "(3i)"=>"17", "(4i)"=>"12", "(5i)"=>"00"}, "endtime"=>{"(1i)"=>"2011", "(2i)"=>"6", "(3i)"=>"17", "(4i)"=>"13", "(5i)"=>"00"}}, {"place"=>"totto", "studycontent"=>"aaaaa", "starttime"=>{"(1i)"=>"2011", "(2i)"=>"6", "(3i)"=>"17", "(4i)"=>"07", "(5i)"=>"47"}, "endtime"=>{"(1i)"=>"2011", "(2i)"=>"6", "(3i)"=>"17", "(4i)"=>"07", "(5i)"=>"47"}}, {"place"=>"totto", "studycontent"=>"aaaaa", "starttime"=>{"(1i)"=>"2011", "(2i)"=>"6", "(3i)"=>"17", "(4i)"=>"07", "(5i)"=>"47"}, "endtime"=>{"(1i)"=>"2011", "(2i)"=>"6", "(3i)"=>"17", "(4i)"=>"07", "(5i)"=>"47"}}, {"place"=>"totto", "studycontent"=>"aaaaa", "starttime"=>{"(1i)"=>"2011", "(2i)"=>"6", "(3i)"=>"17", "(4i)"=>"07", "(5i)"=>"47"}, "endtime"=>{"(1i)"=>"2011", "(2i)"=>"6", "(3i)"=>"17", "(4i)"=>"07", "(5i)"=>"47"}}, {"place"=>"totto", "studycontent"=>"aaaaa"}], "commit"=>"Save changes"} 上記のような params です。 01: index = 0 02: params[:studylogdetails].map { |hash| 03: studylogdetails[index].starttime = 04: Time.new( 05: hash[:starttime]["(1i)"].to_i, 06: hash[:starttime]["(2i)"].to_i, 07: hash[:starttime]["(3i)"].to_i, 08: hash[:starttime]["(4i)"].to_i, 09: hash[:starttime]["(5i)"].to_i, 0) 10: ......<中略> 11: studylogdetails[index].save 12: index = index + 1 13: } このようなコードで、行05 で以下のようなエラーになります。 You have a nil object when you didn't expect it! You might have expected an instance of ActiveRecord::Base. The error occurred while evaluating nil.[] どなたかわかる方、お願いします。

    • ベストアンサー
    • Ruby
  • javascriptのストップウオッチのコード

    javascript初級勉強中です。 以下のコードを実行すると(1)小数点第2の動きがおかしい。  (2)一度リセットボタンを押さないとタイマーが動かない。 という2点の不具合が出ます。   どうすれば解決するでしょうか? <!DOCTYPE HTML> <head> <meta charset="utf-8"> <title>ストップウォッチ</title> </head> <body> <h1>ストップウオッチ</h1> <div id="sec" style="font-size:128px">0.00</div> <input type="button" value="Start!" onclick="run();"> <input type="button" value="Stop!" onclick="stop();"> <input type="button" value="Reset!" onclick="reset();"> <script> var statTime, stopTime, running = false, timerId; function run() { if (running)return; running = true; if (stopTime) { startTime = startTime + (new Date()).getTime() - stopTime; } if (! startTime) { startTime = (new Date()).getTime(); } timer(); //タイマー処理回していく } function timer() { document.getElementById('sec').innerHTML = (((new Date()).getTime()- startTime)/1000). toFixed(2); timerId =setTimeout(function() { timer(); //このタイマー自身を回していく }, 100); } function stop() { if (!running) return false; running = false; clearTimeout(timerId); //timerIdを渡して止める stopTime = (new Date()).getTime(); } function reset() { if (running) return;   startTime = undefined; document.getElementById('sec').innerHTML = '0.00'; } </script> </body> </html>

  • スクリプトをまとめて書くには

    Actionscript 2.0で作成しています。 例えばムービークリップaaa1~aaa10までと、bbb1~bbb10があったとして、 aaa1をクリックするとaaa1とbbb1が動く、というようなスクリプトを書きたい場合、 for( i = 1 ; i <= 10 ; i++ ){ eval("aaa"+i).onRelease = function(){ this.gotoAndPlay(2); _root.eval("bbb"+i).gotoAndPlay(2); } } としてもbbbの方が動きません。 iの値がeval("bbb"+i)の時点で11になってしまってます。 なので現状forを使わずに書いているのですが、 このような方法を簡単な記述で実現するにはどうすればいいのでしょう? それと、同じ動作を別のムービークリップにさせたい場合、 まとめて書くことはできますでしょうか? (例えばこれをまとめて書くとか) aaa.onRelease = function(){ _root.stop(); } bbb.onRelease = function(){ _root.stop(); }

    • ベストアンサー
    • Flash
  • jQueryのforループ時のanimate

    jQueryにて下記のように 配列にて登録した要素に対して forループにてanimateを実行し、 completeで、animate完了後の動作を実行したいのですが、 forループにて複数に対して処理を行った際の挙動がおかしく、 動作を実行する前にcompleteの処理が走ってしまいます。 どのようにすれば、forループで複数のanimate処理をおこなっても 動くことができるようになるのか、 教えて頂けますよう宜しくお願いいたします。 -------------------------------------------------------------------- $(document).ready(function () {  for(i=0; i<10; i++){   pArray($('p').eq(i));  };  for(i=0; i<10; i++){   pArray[i].stop().animate({left: -100 * i}, {duration:1500, easing:'swing',complete:aaa()});  }  function aaa(){   alert('完了')  } }

  • TempleMonkey 自動更新

    自動更新 TempleMonkeyにてそのまま使える記述でお願いします。 (function myloop(i) { setTimeout(function() { myfunc(); if (--i) myloop(i); },150000) })(1); function myfunc() { window.location.reload(true);} var myp = document.createElement("p"); myp.textContent = "Last update: " + (new Date()).toString(); $("#top").append(myp); この記述で指定のページを秒数指定で自動更新をしているのですが、そのページを操作中の場合はリロードをしなくする方法、もしくは秒数をリセットする記述をお願いします。 TempleMonkeyにてそのまま使える記述をお願いします。 #ユーザースクリプト #Javascript #Greasemonkeyスクリプト

  • 多次元配列とfor文について

    javascriptの配列について質問です。 例えば: var arrXXX = new Array(); function samplefunc{ //3次元配列の種類の作成 for (m = 0; m < aaa.length ; m++) { //連想配列作成 arrXXX .push(aaa[m]); } for (j = 0; j < bbb.length; j++) { for (i = 0; i < ccc.length; i++) { arrXXX[aaa[j]] = new Array(ccc.length); arrXXX[aaa[j]][i] = new Array(ccc.length); for (k = 0; k < ddd.length; k++) { arrXXX[aaa[j]][i][k] = eee;     ここでは配列を適切に使える・・・ } } } ここでarrXXXを使いたいが、3次元配列でなくなっている?!  arrXXX[~][0][0]はnullまたはオブジェクトではありません・・・がでます。 } 結局、for文を完全にでてしまうと、せっかくつくった配列がダメになってしまいます。どうすればfor文外で配列を使用できるのか教えてください!