Last active
May 31, 2023 23:35
-
-
Save bor0/3fa539263335fa415faa67606a469f2e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
$start = microtime( true ); | |
$book_sections = [ '1.0', '1.1', '1.2', '2.0', '2.1', '3.0', '3.1' ]; | |
$groups = array_group_pair( $book_sections, function( $p1, $p2 ) { | |
return $p1[0] === $p2[0]; | |
} ); | |
$end = microtime( true ); | |
printf( "%.10f\n", $end - $start ); |
This file contains hidden or 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
<?php | |
$start = microtime( true ); | |
$book_sections = [ '1.0', '1.1', '1.2', '2.0', '2.1', '3.0', '3.1' ]; | |
function custom_array_group(array $arr1, callable $compare): array { | |
$groups = []; | |
$group = []; | |
$prev = NULL; | |
foreach ($arr1 as $value) { | |
if ($group && !$compare($prev, $value)) { | |
$groups[] = $group; | |
$group = []; | |
} | |
$group[] = $value; | |
$prev = $value; | |
} | |
if ($group) { | |
$groups[] = $group; | |
} | |
return $groups; | |
} | |
$groups = custom_array_group( $book_sections, function( $p1, $p2 ) { | |
return $p1[0] === $p2[0]; | |
} ); | |
$end = microtime( true ); | |
printf( "%.10f\n", $end - $start ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An improvement of
(3.22533 - 2.44975)/3.22533 = 24%