Skip to content

Instantly share code, notes, and snippets.

@MattLoyeD
Last active June 29, 2020 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattLoyeD/12cf730a8cdce8af460aa122f25876ab to your computer and use it in GitHub Desktop.
Save MattLoyeD/12cf730a8cdce8af460aa122f25876ab to your computer and use it in GitHub Desktop.
<?php
// Made this on production grade server (PHP FPM 7.2 & 7.4) and local PHP CLI 7.4
// Solution 2 > Solution 1
// Local cli 7.4 : Did solution1 in 7.2129311561584 seconds. Did solution2 in 4.0765311717987 seconds.
// Server 7.2 FPM : Did solution1 in 9.0072309970856 seconds. Did solution2 in 4.500275850296 seconds.
// Server 7.4 FPM : Did solution1 in 8.1211979389191 seconds. Did solution2 in 4.0727269649506 seconds.
// example code
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function solution1($text)
{
for($i = 0; $i < 10000; $i++)
list($username, $domain) = explode('@', $text);
}
function solution2($text)
{
for($i = 0; $i < 10000; $i++) {
$domain = substr($text, strpos($text, '@') +1 );
}
}
$time_start = microtime_float();
for($i = 0; $i < 10000; $i++) {
$text = "jeanmich".$i."@gmail".$i.".com";
solution1($text);
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Did solution1 in $time seconds.\n";
$time_start = microtime_float();
for($i = 0; $i < 10000; $i++) {
$text = "johndoe".$i."@domain".$i.".com";
solution2($text);
} $time_end = microtime_float();
$time = $time_end - $time_start;
echo "Did solution2 in $time seconds.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment