新聞中心
php7函數(shù)類型限定是否對(duì)性能有影響?下面本篇文章就來聊聊PHP7函數(shù)數(shù)據(jù)類型限定設(shè)定與否對(duì)性能的影響,希望對(duì)大家有所幫助!

成都創(chuàng)新互聯(lián)技術(shù)團(tuán)隊(duì)10余年來致力于為客戶提供網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、成都品牌網(wǎng)站建設(shè)、營(yíng)銷型網(wǎng)站、搜索引擎SEO優(yōu)化等服務(wù)。經(jīng)過多年發(fā)展,公司擁有經(jīng)驗(yàn)豐富的技術(shù)團(tuán)隊(duì),先后服務(wù)、推廣了上千多家網(wǎng)站,包括各類中小企業(yè)、企事單位、高校等機(jī)構(gòu)單位。
本文主要通過簡(jiǎn)單的壓測(cè)來探討PHP7函數(shù)數(shù)據(jù)類型限定設(shè)定與否對(duì)性能的影響,另外,分享下自己兩個(gè)工作中遇到的小問題及其應(yīng)對(duì),如有錯(cuò)誤,懇請(qǐng)指正。
(1) 介紹
- 函數(shù)參數(shù)類型限定(包括返回值、成員屬性)從PHP5開始支持的,但是支持的類型不多,PHP7做了擴(kuò)展:int/string/bool/object 等
-
作用
- 避免錯(cuò)誤調(diào)用,標(biāo)明類型,只能傳遞同類型的參數(shù),尤其多人協(xié)同開發(fā)時(shí)。
推薦學(xué)習(xí):《PHP視頻教程》
- 如果不是可以自動(dòng)轉(zhuǎn)換數(shù)據(jù)類型,如下,當(dāng)然前提是待轉(zhuǎn)化類型可以正常轉(zhuǎn)化
- 而本文正是測(cè)試類型限定對(duì)性能的影響程度
function testInt(int $intNum){ var_dump($intNum); } testInt("123"); // int(123) - 避免錯(cuò)誤調(diào)用,標(biāo)明類型,只能傳遞同類型的參數(shù),尤其多人協(xié)同開發(fā)時(shí)。
- 注意 參數(shù)、返回值如果跟設(shè)定的類型不一致時(shí)會(huì)報(bào)錯(cuò),不是百分百確認(rèn)的需要手動(dòng)轉(zhuǎn)化下
(2) 壓測(cè)
-
運(yùn)行環(huán)境
- PHP 7.2.34
- Laravel 5.8
- AB 2.3
-
單機(jī)配置
- 型號(hào)名稱 MacBook Pro
- 處理器名稱 Quad-Core Intel Core i7
- 內(nèi)存 8 GB
- 核總數(shù) 4
-
AB
- 使用AB (Apache Benchmark) 進(jìn)行壓測(cè),由于不是正式的壓測(cè),所以此處只關(guān)心綜合指標(biāo):Requests per second (平均每秒請(qǐng)求數(shù))
-
主要參數(shù)
- -n 壓測(cè)請(qǐng)求數(shù)
- -c 并發(fā)數(shù)
- -p POST請(qǐng)求時(shí)指定所需攜帶參數(shù)的文件
- -r 遇到錯(cuò)誤響應(yīng)不退出,操作系統(tǒng)有防高并發(fā)攻擊保護(hù)措施 (apr_socket_recv: Connection reset by peer)
- 設(shè)置項(xiàng)目 設(shè)置兩個(gè) POST 接口,沒有業(yè)務(wù)邏輯、中間件操作等,如下
/***** 1 普通接口 *****/
// CommonUserController
public function createUser(Request $request)
{
$this->validate($request, [
'name' => 'required|string',
'age' => 'required|integer',
'sex' => ['required', Rule::in([1, 2])],
]);
(new CommonUserModel())->createUser($request['age'], $request['name'], $request['sex'], $request['address'] ?? '');
return response()->json(['status' => 200, 'msg' => 'ok']);
}
// CommonUserModel
public function createUser($sex, $age, $name, $address)
{
if(empty($sex) || empty($age) || empty($name)) return false;
// 省略DB操作
return true;
}
/***** 2 類型限定接口 *****/
// TypeUserController
public function createUser(Request $request): JsonResponse
{
$this->validate($request, [
'name' => 'required|string',
'age' => 'required|integer',
'sex' => ['required', Rule::in([1, 2])],
]);
(new TypeUserModel())->createUser($request['age'], $request['name'], $request['sex'], $request['address'] ?? '');
return response()->json(['status' => 200, 'msg' => 'ok']);
}
// TypeUserModel
public function createUser(int $age, string $name, int $sex, string $address): bool
{
if(empty($sex) || empty($age) || empty($name)){
return false;
}
// 省略DB操作
return true;
}
(3) 實(shí)施
- 共進(jìn)行五次壓測(cè),配置及結(jié)果展示如下 (統(tǒng)一刪除:| grep 'Requests per second')
/*****第一次*****/ // 類型限定接口 rps=456.16 ab -n 100 -c 10 -p '/tmp/ab_post_data.json' -T 'application:json' http://www.laravel_type_test.com/api/type/create_user // 普通接口 rps=450.12 ab -n 100 -c 10 -p '/tmp/ab_post_data.json' -T 'application:json' http://www.laravel_type_test.com/api/common/create_user /*****第二次*****/ // 類型限定接口 rps=506.74 ab -n 1000 -c 100 -p '/tmp/ab_post_data.json' -T 'application:json' http://www.laravel_type_test.com/api/type/create_user // 普通接口 rps=491.24 ab -n 1000 -c 100 -p '/tmp/ab_post_data.json' -T 'application:json' http://www.laravel_type_test.com/api/common/create_user /*****第三次*****/ // 類型限定接口 rps=238.43 ab -n 5000 -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/type/create_user // 普通接口 rps=237.16 ab -n 5000 -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/common/create_user /*****第四次*****/ // 類型限定接口 rps=209.21 ab -n 10000 -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/type/create_user // 普通接口 rps=198.01 ab -n 10000 -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/common/create_user /*****第五次*****/ // 類型限定接口 rps=191.17 ab -n 100000 -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/type/create_user // 普通接口 rps=190.55 ab -n 100000 -c 150 -p '/tmp/ab_post_data.json' -T 'application:json' -r http://www.laravel_type_test.com/api/common/create_user
(4) 結(jié)果
- 壓測(cè)不算太嚴(yán)謹(jǐn),結(jié)果僅供參考
-
類型限定對(duì)性能的提升沒有預(yù)期的大,很微小,不過還是推薦這種寫法
網(wǎng)站標(biāo)題:聊聊PHP7函數(shù)類型限定是否對(duì)性能有影響?(測(cè)試探討)
網(wǎng)頁鏈接:http://www.dlmjj.cn/article/ccegdpj.html


咨詢
建站咨詢
