2016-04-15 15 views
0

これは私が書いた関数であり、それはそれはいくつかの速度のボトルネックを引き起こし、何百回も呼び出される可能性を秘めているという事実を除いて正常に動作します。私は実行時間に関してより効率的になるようにこのコードを最適化する方法があるかどうかを知りたい。ユーザタイムゾーンに基づいてDateTime変換を最適化するにはどうすればよいですか?

/** 
* Takes in the airing values, and then converts them to user local time, giving back the day, dayname, and a formatted timestring. 
* The Day is an ISO calendar day of the week, Hour is a 24-hour format hour, and Minutes is the minutes 
* @param int $airing_day The airing day (1-7) 
* @param int $airing_hour The airing hour (0-23) 
* @param int $airing_minutes The airing minutes (0-59) 
* @return array The Array of values with keys ['day', 'dayname', 'timestring'] 
*/ 
public static function airingTimeToUserTimezone($airing_day, $airing_hour, $airing_minutes) 
{ 

    // February 1st the 2016 is a monday, perfect for conversion, since we can correlate 1 to Monday and 7 to Sunday 
    $AirDateTime = new DateTime('2016-2-' . $airing_day . ' ' . $airing_hour . ':' . $airing_minutes . ':00'); 
    $AirDateTime->setTimezone(self::$user->DateTimeZone); 

    $toret    = array(); 
    $toret['day']  = $AirDateTime->format('N'); 
    $toret['dayname'] = $AirDateTime->format('l'); 
    $toret['hour']  = $AirDateTime->format('G'); 
    $toret['minutes'] = $AirDateTime->format('i'); 
    $toret['timestring'] = $AirDateTime->format("g:i A"); 

    return $toret; 
} 
+0

は、あなたがそれは本当にあなたが期待するほど遅い動作しますかどうかを確認するための環境をシミュレートしようとしたことがありますか? –

+0

私が残した答えに関するフィードバックは一度もありませんでした。あなたの問題を解決しましたか?もしそうであれば、それを受け入れられたものとしてマークすることは認められるでしょう。ありがとう。 – miken32

答えて

1

これが1秒に数千回実行されない限り、おそらくこの機能からのパフォーマンスのヒットはほとんど見られません。しかし、私が見る1つの最適化は一度だけDateTime::format()を呼び出すことです:

public static function airingTimeToUserTimezone($airing_day, $airing_hour, $airing_minutes) 
{ 
    $AirDateTime = new DateTime("2016-02-$airing_day $airing_hour:$airing_minutes:00"); 
    $AirDateTime->setTimezone(self::$user->DateTimeZone); 

    $toret = array(); 
    list (
     $toret['day'], 
     $toret['dayname'], 
     $toret['hour'], 
     $toret['minutes'], 
     $toret['timestring'] 
    ) = explode("/", $AirDateTime->format("N/l/G/i/g:i A")); 

    return $toret; 
} 
関連する問題