The pc_date_sort () function, shown in the example, shows how to sort dates.

function pc_date_sort($a, $b) { list($a_month, $a_day, $a_year) = explode('/', $a); list($b_month, $b_day, $b_year) = explode('/', $b); if ($a_year > $b_year ) return 1; if ($a_year < $b_year ) return -1; if ($a_month > $b_month) return 1; if ($a_month < $b_month) return -1; if ($a_day > $b_day ) return 1; if ($a_day < $b_day ) return -1; return 0; } $dates = array('12/14/2000', '08/07/1999', '08/10/2001'); usort($dates, 'pc_date_sort'); echo '<pre>'; print_r($dates); 

During sorting, the usort () function often — every time it needs to compare two elements — recalculates the values ​​returned by the sorting function, which slows down the process.

Please help me optimize the code. How to avoid unnecessary work?

    2 answers 2

    use the standard built-in class . It used to have their bikes fenced off with dates.

    • the built-in datetime is written in C, it will be faster
    • it is tested and will be safer
    • he offers more options than his bike

    You can add it to your class / function, inheriting (not tested)

    createFromFormat () will create objects from your date format without any additional tweaks and manual explode lines

    • he has his own format $ d = new DateTime ('2011-01-01T15: 03: 01.012345Z'). Is it necessary to reformat them with each comparison? - Vanya Avchyan
    • Translate 1 time each before comparison. Then, if you need it in this format, 1 more time back. Schwartz Conversion - Arnial pm
    • study the documentation and examples on this class carefully, most likely it will either understand your format, or you will convert all dates massively (it will still be faster) or change the format of dates in your code. Completed the answer. - strangeqargo
    • one
      Yeah, I see everything. Thank you - Vanya Avchyan
    • one
      that's the spirit! - strangeqargo

    I will add my own version, but most likely I will consider your suggestions. Despite what I have tested, it seems to be faster, and it eats less memory.

    In order to avoid unnecessary work, you can cache the compared values, as shown in the example

     function pc_array_sort($array, $map_func, $sort_func = '') { $mapped = array_map($map_func, $array); // cache $map_func() values if ('' == $sort_func) { asort($mapped); // функция asort() быстрее функции usort() } else { uasort($mapped, $sort_func); // необходимо сохранить ключи } while (list($key) = each($mapped)) { $sorted[] = $array[$key]; // используем отсортированные ключи } return $sorted; } 

    To avoid unnecessary work, the pc_array_sort () function uses a temporary $ mapped array to cache the returned values. It then sorts the $ mapped array using either the default sorting order or a user-defined sorting procedure. It is important that it uses a sort that preserves key / value relationships. By default, it uses the asort () function, because it is faster than the uasort () function. (The slowness of the uasort () function is certainly a significant argument in favor of the pc_array_sort () function.) Finally, it creates a sorted array of $ sort ed, while the sorted keys in the $ mapped array act as indices of the values ​​of the original array. For small arrays or short sorting functions, the usort () function is faster, but as soon as the number of comparisons grows, the pc_array_sort () function overtakes the usort () function.

    Well, or compare in the first example as advised as follows

     $date1 = DateTime::createFromFormat('!Y/m/d', '2012/10/17'); $date2 = DateTime::createFromFormat('!Y/m/d', '2012/10/17'); var_dump($date1 == $date2); //will be true var_dump($date1 > $date2); //will be false var_dump($date1 < $date2); //will be false