尽可能使用PHP原生函数?
今天在阅读kohana源码中的Arr类的时候发现了这样一个函数
/** * Fill an array with a range of numbers. * * // Fill an array with values 5, 10, 15, 20 * $values = Arr::range(5, 20); * * @param integer $step stepping * @param integer $max ending number * @return array */ public static function range($step = 10, $max = 100) { if ($step < 1) return array(); $array = array(); for ($i = $step; $i <= $max; $i += $step) { $array[$i] = $i; } return $array; }
看到这里的时候,我发现php的原声函数也是可以实现这个功能的,忽然想到之前听到过的一个前辈关于php性能优化的说法——PHP为我们提供了那么多的原声函数,我们尽量用原生函数解决问题。于是我就做了个测试,看看php原生函数性能究竟比自己写的快多少。要测试的函数有原生函数range()和上面的函数_range(),这里加下划线开始是因为重写原声函数range()会报错“Fatal error: Cannot redeclare range() in”。
function _range($step = 10, $max = 100) { if ($step < 1) return array(); $array = array(); for ($i = $step; $i <= $max; $i += $step) { $array[$i] = $i; } return $array; } $time['begin'] = microtime(true); $tmp = range(0,1000000,3); //$tmp = _range(0,1000000,3); $time['end'] = microtime(true); echo $time['end'] - $time['begin'].'s'."\r"; echo (memory_get_peak_usage()/1024/1024)."M";
分别用原生函数和自定义函数进行测试,在产生0~1000000之间所有的3的倍数时,结果出乎我的意料:
首先是使用原生函数的结果:
下面是使用自定义函数的结果:
为了结果比较准确,我在做个图表统计
统计次数 | 原生函数range() | 自定义函数_range() | ||
(0,1000000,3) | 5.155E-3s | 27.5530M | 1.907E-5s | 0.1241M |
(0,1000000,2) | 7.479E-3s | 40.2688M | 1.811E-5s | 0.1241M |
(0,1000,1) | 8.16E-5s | 0.1620M | 2.649E-5s | 0.1241M |
从表中可以看出产生随机数时自定义函数比原生函数要节省内存和时间,而且原生函数在生成大量随机数时特别耗内存,消耗时间也特别多,而自定义函数在这方面则表现得好,产生的内存和消耗的时间基本稳定,看来前面那位前辈说的不一定完全正确哦,但是这里要注意我们这里的自定义函数只能生成数字,而原生的range还可以产生字母的,但是我想这自定义函数添加个字母应该也不会太难~
看来kohana官方对range这个函数很是了解,对php内核中该函数的复杂度也很了解,所以这个小优化才可以做这么好,太厉害了!!!
send Me~

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.