Skip to content

Instantly share code, notes, and snippets.

@vitormattos
Last active June 20, 2022 14:27
Show Gist options
  • Save vitormattos/e242d41f92dc1ae4548c7311644fb287 to your computer and use it in GitHub Desktop.
Save vitormattos/e242d41f92dc1ae4548c7311644fb287 to your computer and use it in GitHub Desktop.
benchmark between string casting and srtcal
<?php
/**
* How to use?
*
* Run:
* php test.php <number of loops>
* Example:
* php test.php 100000
*/
$count = $argv[1];
echo "string cast\n";
for ($a=0; $a < 3; $a++) {
$time_start = microtime(true);
for ($i=0; $i < $count; $i++) {
$test = (string) $count;
}
echo number_format(microtime(true) - $time_start, 3) . PHP_EOL;
}
echo "strval\n";
for ($a=0; $a < 3; $a++) {
$time_start = microtime(true);
for ($i=0; $i < $count; $i++) {
$test = strval($count);
}
echo number_format(microtime(true) - $time_start, 3) . PHP_EOL;
}
@vitormattos
Copy link
Author

My results:

php test.php 100000000
string cast
4.960
4.929
4.951
strval
4.943
4.964
4.952

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment