Java Collection [interview] JavaSE_2.1_Java basis ● Will you tell the difference between Java and PHP?


This paper is divided into three parts:
the main difference ①PHP and Java What? - eechen answer - know almost
https://www.zhihu.com/question/20377398/answer/141328982

② h4cd released on April 1, 2019 - Open Source China
https://www.oschina.net/news/105566/php-8-will-support-jit

③ Joe Watkins -Musings, ninja ones- think, ninja
Saturday, 30 March 2019
PHP GR8
https://blog.krakjoe.ninja/2019/10/
Here Insert Picture Description

①eechen answer - almost known

PHP is not yet support JIT compiled code that runs as hot as the Java Runtime,
(but PH8 confirmed its introduction: PHP 8 whether a support JIT (Just-In-Time) compiler to perform real-time functions, the latest news is that the official has confirmed its introduction.)
but PHP has opcache mechanism to put the script in the corresponding opcode cache memory,
PHP7 also supports configuration opcache.file_cache export opcode to file
third-party Facebook HHVM also support JIT.
in addition built on PHP official LLVM around opcache mechanism the Zend JIT branch testing is also being developed.
appear in php-src / Zend / bench.php test, PHP JIT branch 10 times faster than the PHP 5.4.

https://github.com/zendtech/php-src/tree/zend-jit/ext/opcache/jit
https://www.phpclasses.org/blog/post/493-php-performance-evolution.html
Here Insert Picture Description

PHP library functions implemented in C, while the core Java runtime library (jdk / jre / lib / rt.jar, greater than 60MB) written in Java (jdk / src.zip), so when running Java applications, user-written Code libraries and frameworks and reference should be interpreted in the Java HotSpot JVM execution mechanism until the method is performed 10,000 times. (-XX: CompileThreshold = 10000) will trigger the JIT compiler, run in interpreted mode before this to avoid the time it takes to appear JIT compiler to perform even more than a way to explain the time consumed by the situation.

PHP built-in template engine, template language itself is. The need to use JSP Java Web container such as Tomcat or third-party template engine.

PHP built-in HTTP server and SQLite databases, as well as Apache and FastCGI module implements libphp.so service PHP-FPM. And generally require the use of Tomcat Servlet container and other third parties when Java Web development.

PHP built-in single-process HTTP server (that can be used to quickly develop and test):
PHP 127.0.0.1:8080 -S -t / the WWW

PHP-FPM with Nginx, is multi-process architecture, worker processes handle requests, master process does not process the request, is only responsible for the maintenance worker processes, such as quantitative restart, crashes .PHP-FPM support for the process to restart pool features such as different process pool isolated from each other, independently of each other for example, you can configure a monitor port 9000 www process pool and a pool io 9001 monitoring process to separate the IO-intensive script:

nginx.conf: io.php are requesting access to monitor the process of pooling PHP-FPM 9001
LOCATION = {/io.php
the include fastcgi_params;
fastcgi_pass 127.0.0.1:9001;
fastcgi_param SCRIPT_FILENAME d The c in m e n t r The The t document_root fastcgi_script_name;
}
PHP-FPM: normal www pooling by a static script, the script processing by a dynamic blocking cell io
[www]
; www process called pool listening port 9000, the process is a fixed number of permanent 4
listen = 127.0 .0.1: 9000
PM = static
pm.max_children = 4
[io]
; process pool called io listening port 9001, the number of resident processes 4, maximum 8
the listen 127.0.0.1:9001 =
PM = Dynamic
pm.max_children = . 8
pm.start_servers. 4 =
pm.min_spare_servers. 4 =
pm.max_spare_servers =. 4
wherein the I / O intensive process that pool [IO] prefork the dynamic process, for example where eight is busy, idle four.
using PHP-FPM cell isolation provided separate computing intensive and I / O intensive operations, can reduce clogging of the entire PHP application.

In other words, PHP concurrency through the use of multi-core multi-process, multi-thread and universal adoption of Java concurrency, because a JVM instance is a process.

In addition, PHP can also run in multithreaded mode, such as event MPM and Facebook HHVM Apache is multi-threaded architecture. Whether multi-process or multi-threaded operating mode PHP Web, PHP developers do not need the care and control, That PHP developers do not need to write code to participate in the process and thread management, which by PHP-FPM / HHVM / Apache implementation.

PHP-FPM process management and concurrent implementation does not need to be concerned about PHP developers, and Java multi-threaded programming requires Java developers coding involved .PHP a worker process crashes, master process will automatically create a new worker process and does not lead to PHP service crashes while Java multi-threaded programming slightest mistake (for example, do not catch exceptions) will cause the JVM to crash exit.

For PHP-FPM and Apache MOD_PHP, the service process resident memory, but a request to release a resource, this memory is released very thorough. PHP-based GC reference counting had not even play a role in the program has ended. Also, unset explicitly free memory is immediate in the PHP script used, there is no delay while Java's garbage collection mechanism relies heavily on GC, Full GC under high concurrency Java services will lead to an avalanche:. JVM can not handle busy with GC reclaims memory request, the new request and the arrival of a steady stream.

PHP PHP determines the mode of operation of natural supports hot deployment, hot deployment and Java to achieve is not easy. This is why PHP dominant reason in virtual hosting environments, because PHP developers to upload files via FTP to the virtual space on to achieve a code update and deployment.

PHP share data across processes, in addition to the use of mechanisms based on the session file and birds brother developed lock-free shared cache memory expansion Yac.Linux can also use SQLite on the memory file system (tmpfs) (as /dev/shm/data.sqlite3 ). the life cycle of a Java program with the JVM resident memory, threads can access the shared data.

PHP driver is implemented in C database-driven database access speed is slower than Java .PHP problems such as mysqlnd, which do not exist, and database-driven Java JDBC driver is implemented in Java, PHP drive performance does not suffer. And also supports PHP persistent database connections, that is, multiple requests can be re-connected with a database, each request does not need to open a database connection, such as the following figure is two PHP-FPM work with MySQL process remains of two long connections:.
Here Insert Picture Description
PHP now Java were born in 1995, no PHP Java is a rising star opposite argument, but rather a beginning PHP for Web development, rather than .Java the predecessor of Oak Java language, for embedded software development and design.

C implementation of PHP later absorbed the object programming thinking of C ++, adding object-oriented programming support both used the program, you can also use objects, and more flexible. And Java must be fully object-oriented programming, even the class name and file name linked .

PHP can not develop large-scale applications, depending on whether the user according to local conditions using PHP. For example, PHP is not suitable for developing database engine (mostly C / C ++ to achieve) and other compute-intensive applications .Java on compute-intensive applications compared PHP more advantages, such as HBase database using a Java implementation, but most Web applications are I / O-intensive applications, which involves network I / O, file system I / O, database I / O.

PHP is implemented in C rapid Web development framework, the framework can not rely on third parties to achieve rapid development. The Java Web developers generally rely on third-party frameworks such as Spring.

Added:
Reply
@ groove

Persistent connection to the database is easy to use, mysqli in passing parameters p host: 127.0.0.1 will be able to open a persistent connection, pdo_mysql in the PDO :: ATTR_PERSISTENT can also be turned to true PHP persistent connections and persistent connection to the database does not depend PHP-. FPM, my shots just to illustrate, in fact, also be able to use Apache. just a few PHP-FPM process or Apache processes / threads preferably configured as a fixed quantity but also the number can not exceed the maximum number of MySQL connections (max_connections default 151).

Interprocess data sharing, in addition to Yac, I am not said a SQLite memory file system on Linux tmpfs it? SQLite locking mechanism enough Yeah, even transactions are supported, what are you worried about? And no need to worry about the memory of SQLite read and write performance limitations, SQLite engine is completely calculated in memory-intensive operations. Linux can take advantage of tmpfs mechanism, can use SQLite locking mechanism without relying PHP implementation, I think the advantage is very stable.

CLI is not achieved under such a Swoole PHP services discussed here, but the traditional PHP FastCGI mode. PHP script to run for a long time resident of the background, of course, need to GC. For some of the requirements of real-time highly concurrent applications, I think it should not be GC can be configured using the mechanism .PHP zend.enable_gc = off to disable the GC, and by their own unset manually release the memory, but running under FastCGI PHP script life cycle is very short, in fact, we should not rely on GC. again, high concurrency real-time type applications, GC will never be an advantage, but a disadvantage.

Common PHP SAPI there are so few:
PHP (cli, cli-Server)
PHP-cgi (cgi-fcgi)
PHP-FPM / hhvm (FPM-fcgi)
libphp7.so/php7apache2_4.dll(apache2handler)
FPM-fcgi and apache2handler under either multi-mode or multi-threaded process model, implement and manage processes and threads do not need PHP developers care about (which is an advantage), but by the php-fpm / hhvm / apache achieve .PHP developer if you want to participate multi-process or multi-threaded programming, can achieve in php-cli, related PECL extension includes pcntl multi-process, pthreads multi-threaded, libevent event-driven, etc., related to the project realization has WorkerMan. also peak brother Swoole also need to use php -cli run, but the process of its services and thread control and less need for PHP developers care is implemented by Swoole.

From PHP 5.4 built-in HTTP server, single-process, the purpose is for the rapid development and testing, I think it is a very handy tool, developers do not need to install and configure Apache or Nginx Web server can be like Starter. And the PHP interpreter to cross-compile Android phone or OpenWRT wireless router will be able to use PHP programming resources of this province HTTP server on the LAN and provides services, it is convenient.

JIT has the advantage on the large number of calculations, bench.php script is used to test the computing performance. Real-world applications such as WordPress, etc., JIT can bring performance improvements certainly would not be so obvious, JIT has no mechanism HHVM and the JIT PHP7, in WordPress stress tests reflect upon the same level, illustrates this problem. and I emphasized, Web applications mostly I / O-intensive applications, compiled language is not in I / O-intensive applications in the order of magnitude also has advantages. so, for the majority of PHP developers, even if there is no mechanism for JIT PHP7, performance is good enough.

Reply
@ residual wind

mem php-fpm && siege -c10 -t1M http://www.example.com/app/punbb/index.php> / dev / null && mem php-fpm
which is my definition of a mem in ~ / .bashrc Lane the function is used to quickly program name for the case where:
MEM () {
Top -n1 -b | head -n7 | Sed '1,6d' && Top -n1 -b | Sed '1,7d' | grep $ -i. 1 | grep -v grep;
PS AUX | grep. 1 $ -i | grep -v grep | awk -F "" '. 6 $ = {} + SUM {the printf the END "Resident the Set the Total Size: .1f% MB (% D KB) \ n- ", SUM / 1024, SUM} ';
}

Test program PunBB is a lightweight PHP MySQL driven forum.

Concurrent with the siege 10, the pressure measured one minute, PHP-FPM work process memory (RES) is 13.4MB, there is no question you called PHP-FPM memory usage will continue to grow with the number of requests, there are pictures and the truth :
Here Insert Picture Description
Here Insert Picture DescriptionHere Insert Picture Description
Here Insert Picture Description

②h4cd- Open Source China

All along, we are discussing whether the PHP 8 will support JIT (Just-In-Time) compiler to perform real-time functions, the latest news is that the official has confirmed its introduction.
Include JIT into PHP 8?

Real name Yes No
two (two)
ashnazg (ashnazg)
beberlei (beberlei)
brandon (brandon)
Bvoabi (Bvoabi)
carusogabriel (carusogabriel)
cmb (cmb)
cpriest (cpriest)
dams (dams)
danack (danack) ×
derick (derick)
diegopires (diegopires)
dmitry (dmitry)
duncan3dc (duncan3dc)
orders (orders)
galvao (galvao)
guilhermeblanco (guilhermeblanco)
Jdkrshr (Jdkrshr)
jmikola (jmikola)
jpauli (jpauli)
jwage (jwage)
call (call)
klaussilveira (klaussilveira)
krakjoe (krakjoe)
laruence (laruence)
lcobucci (lcobucci)
the left (left) ×
the lukenho (the lukenho)
Marian (Marian)
mbeccati (mbeccati)
mike (mike)
narf (narf)
Neel (Neel)
nikic (nikic)
ocramius (ocramius)
Pjoye (Pjoye)
slogan (slogan)
Friday (Friday)
pmmaga (pmmaga)
pretty (pretty)
remi (remi)
reywob (reywob)
rtheunissen (rtheunissen)
Olathe (Olathe)
sammyk (sammyk)
STAS (STAS)
svpernova09 (svpernova09)
tianfenghan (tianfenghan)
wjx (wjx)
yunosh (yunosh)
zeev (zeev)
cinnamon (cinnamon)
Final result: 50 2

JIT benefits

JIT is the main performance gains, as well as performance of the entire scene using the extended language support and ecological development of the language.

目前已经很难通过常规手段提升 PHP 的性能,JIT 基本上是目前性能提升的唯一手段;
JIT 带来的性能提升可以让 PHP 在更多使用场景( CPU 密集)中发挥作用;

可以使用 PHP 来开发内置函数,而不用担心性能方面的问题。这一方面可以加速语言的发展(更多人可以参与进来),同时也可以减少目前使用 C 开发容易出现的内存管理、溢出等问题

PHP 实现了一个虚拟机 Zend VM,它会将人类可读脚本编译成虚拟机理解的指令,也就是操作码,这个执行阶段就是“编译时(Compile Time)”;在“运行时(Runtime)”执行阶段,虚拟机 Zend VM 会执行这些编译好的操作码。

通常编译时与运行时两个阶段是独立分开的,脚本编译完成后,像 APC 与 OPCache 这样的字节码缓存组件会缓存这些操作码。而 JIT 去掉了编译时阶段,它将这编译时与运行时两个阶段合为一体,实现即时编译与执行。

JIT 是一种编译器策略,它将代码表述为一种中间状态,在运行时将其转换为依赖于体系结构的机器码,并即时执行。在 PHP 中,这意味着 JIT 将为 Zend VM 生成的指令视为中间表述,并以依赖于体系结构的机器码执行,也就是说托管代码的不再是 Zend VM,而是更为底层的 CPU。

虽然自 PHP 7.0 以来,通过优化核心数据结构 HashTable、强化 Zend VM 中某些操作码与持续改进 OPCache 的 Optimizer 组件等具体措施,PHP 性能得到了显著提升,但是实际上这些优化似乎已经走到极限了。现在 JIT 从底层着手,被认为是目前提升 PHP 性能的最佳出路。

关于是否引入 JIT 的官方投票结果已于近日公布,因为大部分核心开发者投了赞成票,所以 PHP 8 中将会支持 JIT。

另外值得一提的是,PHP JIT 对于使用 PHP 的网站来说提速可能并不明显,因为 JIT 在 CPU 密集型的代码上效果最好,而一般情况下,用 PHP 编写的程序都是 I/O 密集型的。

简单来说就是,PHP 程序往往受限于 I/O 而不是 CPU,使 PHP 代码运行速度变慢的因素往往是它们正在执行的 I/O 操作,包括连接、读取和写入数据库、高速缓存、文件与套接字等。

PHP 中 CPU 密集型代码的一个例子是 Zend/bench.php。

那么 PHP 中的 JIT 将会在哪里发挥作用呢?答案是数学领域。

关于 PHP JIT 的详细介绍,可以查看:https://blog.krakjoe.ninja/2019/03/php-gr8.html

③乔·沃特金斯-Musings, ninja ones-思考,忍者

Saturday, 30 March 2019
PHP GR8Here Insert Picture Description
除非您生活在一块岩石上,或者是过去的(在这种情况下,欢迎您),否则您将意识到JIT即将到PHP 8:今天的投票悄无声息地结束,绝大多数人赞成合并到PHP 8中,所以它是正式的。

为庆祝而抛出一些疯狂的形状,如图1所示,它甚至被称为“(底特律)准时制”……

现在坐下来阅读下面的神话破坏文章,我们将消除对这些东西的困惑。 JIT是什么,它将带来什么好处,并深入研究它的工作原理(但只有一点点,因为我不想让您感到无聊)。

由于我不知道我在跟谁说话,所以我将首先从简单的问题开始,然后再处理复杂的问题,如果您已经确定知道标题中的问题的答案,您可以跳过该部分…

什么是准时制?

PHP实现了一个虚拟机,一种虚拟处理器-我们称之为Zend VM。PHP将您可读的脚本编译为虚拟机可以理解的指令(我们称它们为操作码),此执行阶段称为“编译时间”。在执行的“运行时”阶段,虚拟机(Zend VM)将执行您的代码指令(操作码)。

这一切都很好,并且诸如APC(过去)和OPCache(今天)之类的工具会缓存代码的指令(操作码),以便“编译时间”仅在必须时发生。

首先,用一行来解释一般的JIT:即时是一种编译器策略,它采用中间代码表示并将其在运行时转换为与体系结构相关的机器代码-即时执行。

在PHP中,这意味着JIT将为Zend VM生成的指令视为中间表示,并发出与体系结构相关的机器代码,因此,代码的宿主不再是Zend VM,而直接是您的CPU。

为什么PHP需要JIT?

从Facebook HHVM项目的健康竞争开始,自PHP 7.0出现之前就一直是PHP内部社区的关注焦点。PHP 7.0的大多数核心更改都包含在PHPNG补丁中,该补丁大大改善了PHP在其核心中利用内存和CPU的方式,此后我们每个人都被迫密切关注性能。

从PHP 7.0开始,已经进行了一些性能改进,对HashTable(PHP的核心数据结构)进行了优化,在Zend VM中对某些操作码进行了专门化,对编译器进行了对某些序列的专门化,以及对Optimizer组件的持续改进OPCache …以及其他很多东西,太无聊了。

荒谬的事实是,这些优化只能将我们带走,而我们正在迅速接近或可能已经遇到了进一步改进它的能力。

警告:当我们说诸如“我们无法进一步改进”之类的东西时,我们真正的意思是,“我们必须做出的进一步改进的权衡不再具有吸引力”……每当我们谈论时关于性能优化,我们正在谈论取舍。通常,要在性能方面进行权衡取舍。我们都想认为最简单的代码是最快的代码,但是在现代C编程世界中情况并非如此。最快的代码通常是准备利用依赖于体系结构的内在函数或依赖于平台(编译器)的内置函数的代码。简单性并不能保证最佳性能…

目前,将PHP转换为JIT的能力似乎是从PHP压缩性能的最佳方法。

JIT可以使我的网站更快吗?

可能性不大。

也许不是您所期望的答案:通常,用PHP编写的应用程序受I / O约束,而JIT在受CPU约束的代码上效果最佳。

“ I / O和CPU绑定”到底是什么意思?

当我们要描述一段代码或应用程序的一般性能特征时,我们使用术语I / O绑定和CPU绑定。

用最简单的术语来说:
如果我们可以改进(减少,优化)它正在执行的I / O,则一段受I / O约束的代码会更快。
如果我们可以改善(减少,优化)CPU正在执行的指令,或者(神奇地)提高CPU的时钟速度,则CPU约束的代码段将更快。
一段代码或一个应用程序可能受I / O约束,CPU约束或同等地绑定到CPU和I / O。

通常,PHP应用程序倾向于受I / O约束-放慢它们的速度是它们正在执行的I / O-连接,读取和写入数据库,缓存,文件,套接字等。

CPU绑定的PHP是什么样的?

由于大多数PHP应用程序的本质,CPU绑定代码并不是很多PHP程序员都会熟悉的东西-他们的工作往往是连接到某个数据库,或者可能是缓存,进行一些轻松的工作并吐出html / json / xml响应。

您可能会四处查看代码库,发现许多与I / O无关的代码,甚至正在调用完全与I / O断开连接的函数的代码,并且很困惑我似乎暗示这并没有使即使与非I / O相比,处理非I / O的代码行可能更多,但仍受应用程序CPU的限制。

PHP实际上相当快,它是世界上最快的解释语言之一。Zend VM调用与I / O无关的函数与在机器代码中进行相同的调用之间没有显着差异。显然有区别,但是事实是机器代码具有调用约定,而Zend VM具有调用约定,机器代码具有序言,而Zend VM具有序言:您是否在Zend Opcodes或机器代码中调用some_c_level_function()不会对进行调用的应用程序的性能产生重大影响-尽管似乎可以对该调用产生重大影响。

注意:调用约定 (大致) 是在进入另一个函数之前执行的指令序列,而序言是在进入另一个函数*时执行的指令序列:在两种情况下,调用约定都将参数压入堆栈,序言将它们从堆栈中弹出。

关于循环,尾部调用和XI的问题是什么呢:PHP实际上非常聪明,启用了OPCache的Optimizer组件后,您的代码就可以像魔术一样转换为您可能编写的最高效的形式。

现在必须注意,JIT不会从VM建立的约定中更改Zend函数的调用约定-Zend必须能够随时在JIT和VM模式之间切换,因此决定保留调用约定由VM建立。结果,在JIT中,您到处都可以看到的那些呼叫并没有明显地更快。

如果您想查看CPU绑定的PHP代码是什么样子,请查看Zend / bench.php …这显然是CPU绑定代码的一个极端示例,但是应该可以理解JIT真正发挥作用的地方是数学领域。

PHP是否做出了最终的权衡以提高数学速度?

否。我们这样做是为了扩展PHP的范围,并且相当大地如此。

不希望自己大声疾呼,我们已经覆盖了整个网络-如果您是2019年的Web程序员,并且尚未考虑将PHP用于下一个项目,那么您做错了Web-在这个非常偏颇的PHP开发人员看来。

乍一看,要提高在PHP中更快地执行数学的能力,范围似乎很狭窄。

但是,这实际上为诸如机器学习,3d渲染,2d(gui)渲染和数据分析之类的事情打开了大门。

为什么在PHP 7.4中不能使用它?

我只是将JIT称为“最终的折衷”,我认为是:它可以说是有史以来发明的最复杂的编译器策略之一,也许是最复杂的。引入JIT会带来相当大的复杂性。

如果您问Dmitry(JIT的作者)是否使PHP变得复杂,他会说“不,我讨厌复杂性”(这是直接引述)。

从根本上讲,复杂是我们所不了解的一切,目前,很少有真正了解我们所拥有的JIT实现的内部开发人员(少于少数)。

PHP 7.4 soon, with PHP 7.4 will provide us with a consolidated version of PHP, very few people can debug, repair, or improvement (from any practical sense). For those who refuse to join the PHP 7.4, this is just unacceptable situation.

In the period from now to PHP 8's, many of us will work in his spare time to learn JIT: the need to rewrite PHP 8 tools, we must first understand the JIT we still have a function to be achieved as well. We need this period of time, thank you very much qualified majority of voters think it will be presented to us.

The complex is not terrible synonymous: the complex can be like a beautiful nebula, while JIT is this complex. In principle, you can fully understand the complexity of things, but only slightly reduce the complexity of the surface of things. In other words, even if there are 20 internal developers as familiar as Dmitry JIT, and also can not really change the complexity of the JIT.

PHP developers will slow it?

There is no reason to think it will. We have enough time can confidently say that, generally available to the PHP 8, we already have enough people familiar with the JIT, at least as good as we are today and fix errors in PHP and push forward.

When you try on a JIT is essentially a par with complex ideas, please consider we spend most of the time on the introduction of the new features are actually talking about the feature. For most functions, or even fix, write code may take a few minutes or a few hours, and time for discussion may take weeks or months. In rare cases, writing code for a function may take hours or days, but in those rare cases, the discussion always takes longer.

This is what I want to say ...

happy weekend.

Guess you like

Origin www.cnblogs.com/guoxinyu/p/12070243.html