【发布时间】:2011-01-01 22:10:30
【问题描述】:
我有一个通用的数据库查询函数,每次发出 SQL 查询时都会运行以下检查:
if (preg_match('~^(?:UPDATE|DELETE)~i', $query) === 1)if (preg_match('~^(?:UPDATE|DELETE)~iS', $query) === 1)if ((stripos($query, 'UPDATE') === 0) || (stripos($query, 'DELETE') === 0))
我知道一个简单的strpos() 调用比调用preg_match() 快得多,但是由于我调用了两次strIpos(),我真的不确定哪一个应该执行得更好.
第二个选项中的S 模式修饰符也给我带来了一些困惑,来自手册:
何时使用模式 几次,值得花 更多的时间来分析它,以便 加快匹配所需的时间。 如果设置了这个修饰符,那么这个 进行额外的分析。在 目前,研究模式很有用 仅适用于非锚定模式 没有一个固定的起点 字符。
在这种情况下,速度并不重要(否则我不会使用这个通用查询函数)但是,我仍然希望让它尽可能快地运行,同时保持它的简单性。
我应该选择以上哪个选项?
编辑:我已经run a simple benchmark,但我仍然无法确定哪种方法效果更好。
这是 10,000 次尝试的结果(总时间,以秒为单位):
Array
(
[match] => Array
(
[stripos] => 0.0965
[preg_match] => 0.2445
[preg_match?] => 0.1227
[preg_match?S] => 0.0863
)
[no-match] => Array
(
[stripos] => 0.1165
[preg_match] => 0.0812
[preg_match?] => 0.0809
[preg_match?S] => 0.0829
)
)
100,000 次尝试:
Array
(
[match] => Array
(
[stripos] => 1.2049
[preg_match] => 1.5079
[preg_match?] => 1.5564
[preg_match?S] => 1.5857
)
[no-match] => Array
(
[stripos] => 1.4833
[preg_match] => 0.8853
[preg_match?] => 0.8645
[preg_match?S] => 0.8986
)
)
1,000,000 次尝试:
Array
(
[match] => Array
(
[stripos] => 9.4555
[preg_match] => 8.7634
[preg_match?] => 9.0834
[preg_match?S] => 9.1629
)
[no-match] => Array
(
[stripos] => 13.4344
[preg_match] => 9.6041
[preg_match?] => 10.5849
[preg_match?S] => 8.8814
)
)
10,000,000 次尝试:
Array
(
[match] => Array
(
[stripos] => 86.3218
[preg_match] => 93.6755
[preg_match?] => 92.0910
[preg_match?S] => 105.4128
)
[no-match] => Array
(
[stripos] => 150.9792
[preg_match] => 111.2088
[preg_match?] => 100.7903
[preg_match?S] => 88.1984
)
)
您可以看到结果差异很大,这让我想知道这是否是进行基准测试的正确方法。
【问题讨论】:
-
....查询需要多长时间才能运行?这看起来像是过早的优化。
标签: php regex string string-matching strpos