Skip to content

Instantly share code, notes, and snippets.

@eb1024
Created January 6, 2010 04:41
Show Gist options
  • Save eb1024/270018 to your computer and use it in GitHub Desktop.
Save eb1024/270018 to your computer and use it in GitHub Desktop.
<?php
$marks = array();
$times = 100000;
$query = 'INSERT INTO "test";';
$start = microtime(true);
for ($i = 0; $i < $times; $i++)
{
if ((stripos($query, 'INSERT') === 0) || (stripos($query, 'REPLACE') === 0))
{
}
}
$end = microtime(true);
$marks['match']['stripos'] = round($end - $start, 4);
$start = microtime(true);
for ($i = 0; $i < $times; $i++)
{
if (preg_match('~^(INSERT|REPLACE)~i', $query) === 1)
{
}
}
$end = microtime(true);
$marks['match']['preg_match'] = round($end - $start, 4);
$start = microtime(true);
for ($i = 0; $i < $times; $i++)
{
if (preg_match('~^(?:INSERT|REPLACE)~i', $query) === 1)
{
}
}
$end = microtime(true);
$marks['match']['preg_match?'] = round($end - $start, 4);
$start = microtime(true);
for ($i = 0; $i < $times; $i++)
{
if (preg_match('~^(?:INSERT|REPLACE)~iS', $query) === 1)
{
}
}
$end = microtime(true);
$marks['match']['preg_match?S'] = round($end - $start, 4);
$query = 'SELECT * FROM "test";';
$start = microtime(true);
for ($i = 0; $i < $times; $i++)
{
if ((stripos($query, 'INSERT') === 0) || (stripos($query, 'REPLACE') === 0))
{
}
}
$end = microtime(true);
$marks['no-match']['stripos'] = round($end - $start, 4);
$start = microtime(true);
for ($i = 0; $i < $times; $i++)
{
if (preg_match('~^(INSERT|REPLACE)~i', $query) === 1)
{
}
}
$end = microtime(true);
$marks['no-match']['preg_match'] = round($end - $start, 4);
$start = microtime(true);
for ($i = 0; $i < $times; $i++)
{
if (preg_match('~^(?:INSERT|REPLACE)~i', $query) === 1)
{
}
}
$end = microtime(true);
$marks['no-match']['preg_match?'] = round($end - $start, 4);
$start = microtime(true);
for ($i = 0; $i < $times; $i++)
{
if (preg_match('~^(?:INSERT|REPLACE)~iS', $query) === 1)
{
}
}
$end = microtime(true);
$marks['no-match']['preg_match?S'] = round($end - $start, 4);
echo '<pre>';
print_r($marks);
echo '</pre>';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment