PHP7函数类型限定是什么意思,有什么影响吗
Admin 2022-07-30 群英技术资讯 552 次浏览
本文主要通过简单的压测来探讨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进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
swoole做mysql连接池的方法:1、可基于swoole的AsyncTask模块实现的连接池,编程简单,没有数据同步和锁的问题。2、使用channel实现连接池。
Swoole在1.8.6版本提供了全新的异步MySQL客户端,底层自行实现了MySQL的通信协议,无需依赖其他第三方库,直接使用swoole_mysql->__construct命令,创建异步mysql客户端。
在PHP的日常操作中,数组是最常出现的结构,而我们几乎每天都在处理数组相关的内容,这篇文章主要给大家介绍了关于PHP数组迭代器的使用方法,需要的朋友可以参考下
在本篇文章里小编给大家分享了关于如何通过PHP创建数据库的知识点内容,有需要的朋友们学习下。
用PHP怎样实现堆排序?要解决这个问题,首先我们需要了解关于堆,完成二叉树以及堆排序的原理,本文后面有堆排序的PHP实现代码,有需要的朋友可以参考一下。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008