Skip to content

Instantly share code, notes, and snippets.

@luchaninov
Last active December 14, 2018 10:07
Show Gist options
  • Save luchaninov/c8dd1f95aaeb8426f1af3a81c2dda8f6 to your computer and use it in GitHub Desktop.
Save luchaninov/c8dd1f95aaeb8426f1af3a81c2dda8f6 to your computer and use it in GitHub Desktop.
strpos: 8.149, substr: 8.816, [0]: 7.388
<?php
$varName = 'test';
$count = 0;
$time = microtime(true);
for ($i = 0; $i < 100000000; $i++) {
if (0 === strpos($varName . $i, '!')) {
$count++;
}
}
$time1 = microtime(true) - $time;
$count = 0;
$time = microtime(true);
for ($i = 0; $i < 100000000; $i++) {
if ('!' === substr($varName . $i, 0, 1)) {
$count++;
}
}
$time2 = microtime(true) - $time;
$count = 0;
$time = microtime(true);
for ($i = 0; $i < 100000000; $i++) {
if ('!' === ($varName . $i)[0]) {
$count++;
}
}
$time3 = microtime(true) - $time;
printf('strpos: %s, substr: %s, [0]: %s', number_format($time1, 3), number_format($time2, 3), number_format($time3, 3));
// strpos: 8.149, substr: 8.816, [0]: 7.388
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment