计算字符串中数字数量的最快方法,以及如何测试 PHP 性能?
我需要找到字符串中有多少个数字的最佳方法,我是否必须首先删除除数字之外的所有内容,然后删除 strlen?
另外,我将如何测试我编写的任何 PHP 脚本的性能,例如特定条件下的速度和性能?
更新
说我必须包含 ½ 个半数,那么它肯定是 preg 不是吗?
I need the best way of finding how many numbers in a string, would I have to first remove everything but numbers and then strlen?
Also how would I go about testing the performance of a any PHP script I have written, say for speed and performance under certain conditions?
UPDATE
say I had to inlcude ½ half numbers, its definetly preg then is it not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 preg_split() 拆分字符串并计算各个部分:
为了提高性能,您可以使用 microtime()。
例如:
对于执行时间:
对于内存使用情况:
http:// us.php.net/manual/en/function.memory-get-usage.php
对于峰值内存:
http://us.php.net/manual/en/function.memory-get-peak-usage.php
还有具体的 PHP profiling像 XDebug 这样的工具。
有一个关于在 Eclipse 中使用它的很好的教程:
http://devzone.zend.com/article/2930
PEAR 中也有基准测试:
http://pear.php.net/package/Benchmark/ docs/latest/Benchmark/Benchmark_Profiler.html
以及其他列表:
http://onwebdevelopment.blogspot.com/2008/ 06/php-code-performance-profiling-on.html
You can just split the string with preg_split() and count the parts:
For performance you could use microtime().
eg:
For execution time:
For memory usage:
http://us.php.net/manual/en/function.memory-get-usage.php
For peak memory:
http://us.php.net/manual/en/function.memory-get-peak-usage.php
There are also specific PHP profiling tools like XDebug.
There is a good tutorial on using it with Eclipse:
http://devzone.zend.com/article/2930
There is also benchmark in PEAR:
http://pear.php.net/package/Benchmark/docs/latest/Benchmark/Benchmark_Profiler.html
And a list of others:
http://onwebdevelopment.blogspot.com/2008/06/php-code-performance-profiling-on.html