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.