Discussion on the processing string's validity rate in php

Source: Internet
Author: User
There are a bunch of strings similar to the following variable $ abc, and there is a comma at the end. I want to drop the comma at the end. in order to verify the fulfillment time of different processing methods, the following is a control program I wrote for an experiment: Program 1, using subs

There are a bunch of strings similar to the following variable $ abc, and there is a comma at the end. I want to drop the comma at the end. in order to verify the fulfillment time of different processing methods, the following is a control program I wrote for an experiment:

Program 1: Use substr

Function getmicrotime (){
List ($ usec, $ sec) = explode ('', microtime ());
Return (float) $ usec (float) $ sec );
}

$ Time_start = getmicrotime ();

$ Abc = 'Chongqing, Wanzhou, Fuling, Yuzhong, Dadukou, Jiangbei, Shapingba, Jiulongpo, Southbank, Beibei, Wansheng, Shuangbei, Yubei, Bannan, Qijiang, Changshou, Zhujiang, tongnan, Tongliang, Dazu, Rongchang, Bishan, Liangping, Chengkou, Fengdu, Dianjiang, Wulong, Zhongxian, Kaixian, Yunyang, Fengjie, Wushan, Wuxi, Shizhu ,';

$ L = strlen ($ abc );
$ Abc = substr ($ abc, 0, $ L-1 );
Echo $ abc .'
';
$ Time_end = getmicrotime ();
Echo ($ time_end-$ time_start) * 1000;
?>

Procedure 2: use a regular expression

Function getmicrotime (){
List ($ usec, $ sec) = explode ('', microtime ());
Return (float) $ usec (float) $ sec );
}

$ Time_start = getmicrotime ();

$ Abc = 'Chongqing, Wanzhou, Fuling, Yuzhong, Dadukou, Jiangbei, Shapingba, Jiulongpo, Southbank, Beibei, Wansheng, Shuangbei, Yubei, Bannan, Qijiang, Changshou, Zhujiang, tongnan, Tongliang, Dazu, Rongchang, Bishan, Liangping, Chengkou, Fengdu, Dianjiang, Wulong, Zhongxian, Kaixian, Yunyang, Fengjie, Wushan, Wuxi, Shizhu ,';

$ Abc = str_replace (', $', '', $ abc );
Echo $ abc .'
';
$ Time_end = getmicrotime ();
Echo ($ time_end-$ time_start) * 1000;
?>

The average performance time of result 1 is 0.045, and that of procedure 2 is 0.085. The average performance time for no processing is 0.041.

It can be seen that the processing efficiency of the first method is 50% higher.

As a result, we should compare different methods in the process of program creation, especially in the process of efficient request, to find the optimal processing method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.