wp-settings.php (7) wordpress処理時間
「wp-settings.php」の続きをみていきます。
※wordress2.7で解析しています。
/** * PHP 4 standard microtime start capture. * @access private * @since 0.71 * @global int $timestart Seconds and Microseconds added together from when function is called. * @return bool Always returns true. */ function timer_start() { global $timestart; $mtime = explode(' ', microtime() ); $mtime = $mtime[1] + $mtime[0]; $timestart = $mtime; return true; }
関数「microtime」の説明をphpマニュアルから引用します。
microtime()は、現在時刻をマイクロ秒まで測定したUnixタイムスタンプを返します。
現在の時刻をマイクロ秒まで取得します。
浮動小数点型で取得するか、半角スペース区切りの文字列として取得するかは引数によって決定され、引数にTRUEを設定した場合には浮動小数点型の数値が返されます。
文字列として取得した場合には、マイクロ秒とUnixタイムスタンプを半角スペースで区切った文字列形式になります。左側がマイクロ秒・右側がUnixタイムスタンプです。
例)
microtime() 0.89402700 1246794973
$timestart 1246794973.89
/** * Return and/or display the time from the page start to when function is called. * You can get the results and print them by doing:* $nTimePageTookToExecute = timer_stop(); * echo $nTimePageTookToExecute;* Or instead, you can do:* timer_stop(1);* which will do what the above does. If you need the result, you can assign it to a variable, but * most cases, you only need to echo it. * @since 0.71 * @global int $timestart Seconds and Microseconds added together from when timer_start() is called * @global int $timeend Seconds and Microseconds added together from when function is called * @param int $display Use '0' or null to not echo anything and 1 to echo the total time * @param int $precision The amount of digits from the right of the decimal to display. Default is 3. * @return float The "second.microsecond" finished time calculation */ function timer_stop($display = 0, $precision = 3) { //if called like timer_stop(1), will echo $timetotal global $timestart, $timeend; $mtime = microtime(); $mtime = explode(' ',$mtime); $mtime = $mtime[1] + $mtime[0]; $timeend = $mtime; $timetotal = $timeend-$timestart; $r = ( function_exists('number_format_i18n') ) ? number_format_i18n($timetotal, $precision) : number_format($timetotal, $precision); if ( $display ) echo $r; return $r; } timer_start();
「timer_stop」関数は、「timer_start」関数を実行されたときから、「timer_stop」関数が実行されるまでの時間間隔を求めます。
「timer_stop」関数が実行された時の時間は、グローバル変数$timeendに格納されますが、求め方は⑪で求めるグローバル変数$timestartと同じです。
第一引数:$display が「0」のときはそのまま値として返ってきます。それ以外は、計算結果を出力します。
第二引数:$precision は少数第何桁まで求めるかを指定します。
「number_format_i18n」関数があれば、値をこの関数を通して整形します。この関数は「wp-includes/functions.php 160行目前後」で呼ばれています。しかし、functions.phpは、今みている「wp-settings.php」の、この処理より後で読み込まれています。なぜ先に読み込まないのかよくわかりません。
