PHP7函数类型限定是什么意思,有什么影响吗
Admin 2022-07-30 群英技术资讯 654 次浏览
本文主要通过简单的压测来探讨PHP7函数数据类型限定设定与否对性能的影响,另外,分享下自己两个工作中遇到的小问题及其应对,如有错误,恳请指正。
作用
推荐学习:《PHP视频教程》
function testInt(int $intNum){ var_dump($intNum); } testInt("123"); // int(123)
运行环境
单机配置
AB
主要参数
/***** 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; }
/*****第一次*****/ // 类型限定接口 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
类型限定对性能的提升没有预期的大,很微小,不过还是推荐这种写法
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了PHP中用Trait封装单例模式的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
我们知道PHP中很多数组排序函数,之前我们了解升序排序sort函数,这篇文章就带大家了解一下降序排序rsort函数,降序排序就是从高到低排序,可见rsort函数和sort函数是相反,那么语法和返回值会有所不同吗?下面我们来具体看看rsort函数以及实例
在本篇文章中我们给大家整理了关于PHP中filesize函数的用法和相关知识点,有需要的朋友们学习下。
会有Notice:Undefinedvariableecho$myvariable+3;//使用这个变量出现:Notice:Undefinedvariable:myvariablein如果将其改写成。
异常指的是程序在运行中出现不符合预期的情况,云溪发生,但属于一种不正常的情况,是本不该出现但仍出现的错误,属于逻辑和业务流程的错误,而不是编译或者语法上的错误。异常用于在指定的错误发生时改变脚本的正常流程。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008