Sample text:
$text = 'Administration\Controller\UserController::Save';
Task - extract everything before ::
Option 1:
list($module) = explode('::',$text);
Option 2:
$module = substr($text, 0, strpos($text, '::');
Which option is more efficient?
Returns the position of the first occurrence of a string inside another string, or FALSE if the string is not found.
strpos in PHP is a built-in function. Its use is to find the first occurrence of a substring in a string or a string inside another string. The function returns an integer value which is the index of the first occurrence of the string.
You can use the PHP strpos() function to check whether a string contains a specific word or not. The strpos() function returns the position of the first occurrence of a substring in a string. If the substring is not found it returns false .
On my system:
~/pb$ uname -a && php -v
Linux hostname 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1+deb7u1 x86_64 GNU/Linux
PHP 5.4.19-1~dotdeb.1 (cli) (built: Aug 27 2013 00:42:43)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
with XCache v3.0.3, Copyright (c) 2005-2013, by mOo
with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
with XCache Cacher v3.0.3, Copyright (c) 2005-2013, by mOo
I have results:
~/pb$ ./test ListVsSubstr
[============================================================>] 1000 u | 8134 u/s | Est: 0.0 s | Mem: 335.74 KB | Max: 357.96 KB
[============================================================>] 1000 u | 7808 u/s | Est: 0.0 s | Mem: 336.14 KB | Max: 357.96 KB
Test name Repeats Result Performance
list+explode 1000 0.044890 sec +0.00%
substr+strpos 1000 0.052825 sec -17.68%
Test code here: link.
From time to time results slightly different, but list+explode
is always faster more than 15%.
Different systems and PHP versions may have different results. You must check it by yourself and for sure in environment configuration identical to your production.
I ran a test and seems like the first solution is faster. Here is the code for testing it:
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function solution1($text)
{
for($i = 0; $i < 10000; $i++)
list($module) = explode('::',$text);
}
function solution2($text)
{
for($i = 0; $i < 10000; $i++)
$module = substr($text, 0, strpos($text, '::'));
}
$text = 'Administration\Controller\UserController::Save';
$time_start = microtime_float();
solution1($text);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Did solution1 in $time seconds.\n";
$time_start = microtime_float();
solution2($text);
$time_end = microtime_float();
$time = $time_end - $time_start;
echo "Did solution2 in $time seconds.\n";
Test 1: Did solution1 in 0.19701099395752 seconds. Did solution2 in 0.38502216339111 seconds.
Test 2: Did solution1 in 0.1990110874176 seconds. Did solution2 in 0.37402105331421 seconds.
Test 3: Did solution1 in 0.19801092147827 seconds. Did solution2 in 0.37002205848694 seconds.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With