网上流行一些关于执行代码效率的内容,实际测试了一下,有一些结果并非像内容中提及的那样
比如有人建议在str_replace前先用strpos进行查找操作,理由是即使没有替换,str_replace 也会为其参数分配内存从而很慢,因此需要用 strpos 先查找,看是否需要替换,如果需要,再替换
其文中给出的效率提升是:
- 如果需要替换:效率几乎相等,差别在 0.1% 左右。
- 如果不需要替换:用 strpos 快 200%。
这跟我实测的结果并不相同.
实际测试下来,在满足替换条件的情况下,
执行Strpos时间是178ms,直接str_replace是113ms.
在不满足替换条件下,strpos加上后是72ms,不加是88ms
由此数据看出.综合而言使用strpos在大多数需要替换的情况下会加大系统开销.并不是一个值得提倡的作法 :-(
分享到:
相关推荐
有关于,PHP中字符串的处理的相关函数,strlen()函数统计字符串中字符的个数,str_word_count()函数统计字符串中单词...找不到返回false,str_replace('字符串中替换的字符','替换后的字符','字符串')字符穿的替换函数
mb_strpos — 查找字符串在另一个字符串中首次出现的位置 Description int mb_strpos ( string $haystack , string $needle [, int $offset = 0 [, string $encoding = mb_internal_encoding() ]] ) //Finds ...
$this->start_pos = strpos($this->str, $this->start_str) strlen($this->start_str); $this->end_pos = strpos($this->str, $this->end_str); $this->c_str_l = $this->end_pos - $this->start...
$code = str_replace('{goods_image_url}', site_url() . '/' . $val['default_image'], $code); $content .= $code; unset($code); } $this->js_write($content); $this->js_write($data['footer']); $this-...
str_replace () ucfirst () lcfirst () ucwords () substr () strrev () number_format () chunk_split () str_word_count () wordwrap () strlen () mb_strlen () str_repeat () strstr () strtr () str_shuffle ()...
/* author:zhiwushan date:20080411 ...eval(str_replace('eval','echo',$a)); $a = ob_get_clean(); if(strpos($a,'eval(gzinflate(str_rot13(base64_decode')===false) { return $a; } } } ?>
str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php'; include $filename; } }rss简易信息聚合(也叫聚合内容)是一种RSS基于XML标准,在互联网上被广泛采用的内容包装和投递协议。RSS(Really Simple...
分享给大家供大家参考,具体如下: <?.../*子字符串位置互换 */ $str1=Tom; $str2=Jack;...$str=This is an example,you see Tom tell Jack something;...function str_change($str,$... $str=substr_replace($str,$str2,$
$class = str_replace('\\', DIRECTORY_SEPARATOR, $class); $class = join(DIRECTORY_SEPARATOR, array('PhpPresentation', 'Tests', '_includes')) . substr($class, strlen($prefix)); $...
一、字符操作 1、访问子字符串:strpos(); if(strpos($_POST['email'],'@')==false){ echo 'the input content is not a legal email'; } 2、提取子字符串:substr();...3、替换子字符串:substr_replace();
PHP中strpos、strstr和stripos、stristr函数分析_.docx
本文实例讲述了php使用substr()和strpos()联合查找字符串中某一特定字符的方法。分享给大家供大家参考。具体分析如下: 要求:在下面的字符串中... echo substr($str,0,strpos($str,"||")); ?> 输出内容为admin。 s
① strpos() 函数返回字符串在另一个字符串中第一次出现的位置,如果没有找到该字符串,则返回 false. 语法:strpos(string,find,start),代码如下: 复制代码 代码如下:$str=”hello world”; //定义字符串1 $result=...
用Go语言实现PHP内置函数 这是一个用Go语言开发的辅助库,尤其是适用于熟悉PHP内置函数的开发者,将实现PHP内置函数。 下载安装 go get -u github....关于命名 ... 例子: package main ...strpos() ...str_replace
首先应该知道 strpos 函数可能返回布尔值 FALSE,但也可能返回一个与 FALSE 等值的非布尔值,例如 0 或者""。我们应使用 === 运算符来测试本函数的返回值。
最近的开发中在程序代码里有一个随机数是否在给定字符串里的判断,我用了如下的测试代码: $string='\u4e2d\u5956\u53f7\u7801\uff1a3'; $numtmp=mt_rand(1,10); if(strpos($string,$numtmp)!==false){ echo "恭喜...
$path=str_replace(“/”,”\\”,$path); //指定文件夹 $path=$path.”\\web\\study\\”; //得到所有文件 $s=explode(“\n”,trim(`dir/b/o:gn $path`)); //得到文件夹下的所有HTML文件名 $num=count($s); for($i=0...
自写PHP strpos() 函数 查找字符串在另一字符串中第一次出现的位置. https://blog.csdn.net/qq_40534785/article/details/87939551
strpos()函数返回字符串在另一个字符串中第一次出现的位置。如果没有找到该字符串,则返回 false,下面看示例使用方法