Home  >  Article  >  Backend Development  >  What is the difference between php and java

What is the difference between php and java

清浅
清浅Original
2019-05-06 16:31:555288browse

The differences between PHP and Java are: in terms of running mechanism, PHP directly interprets and compiles text codes, while Java is first compiled into bytecode and then compiled twice in the virtual machine; in terms of handling concurrency, Java is Adopting a single process and multi-thread approach, PHP is a multi-process

What is the difference between php and java

(1) Running mechanism

Java code is compiled into words After the code is saved, it will be re-compiled by JIT in the virtual machine into local code. According to rumors, its execution speed can be comparable to C. After my own testing, I implemented a simple Memcache protocol cache server in Java. In Java 1.6 When running under memcache, the access time ratio for the same amount of data is about 3:2 compared with memcache itself. Although there is a gap, it is much better than imagined. Java 1.7 has made a lot of improvements in JIT, and its performance is even better than Java 1.6.

PHP directly interprets and executes text codes. Even with opcode caching technology, there is still an insurmountable performance gap. PHP's opcode is similar to Java's class bytecode, which is still interpreted and executed.

(2) Handling concurrency

Java adopts a single-process multi-thread approach for concurrent processing. The web application will start with the startup of the web server, and from The web browser's request will be assigned to the idle thread in the thread pool for processing. That is to say, when a request arrives, the process is ready, the thread is ready, and all Java has to do is process the business logic.

PHP adopts a multi-process approach for concurrent processing. There is no physical web application concept in the web server. Each request is equivalent to an independent application, and the process is started as the request arrives. , and dies as the request ends. In the Fast CGI environment, there is a process pool technology similar to the thread pool, which is very helpful in improving performance. However, on the one hand, the communication between the web server and Fast cgi still needs to go through sockets, which causes a certain amount of IO loss. On the other hand, it is also difficult to communicate between processes in the process pool, so it is still unable to compare with Java in terms of concurrent processing.

(3) Database application

Java can use database connection pool technology to save time loss caused by the database connection process.

PHP does not have this benefit, the reason comes from the second point above.

As for the database interface, Java has JDBC and PHP has PDO. The two are very similar. However, Java has a lot of ORM technology frameworks (such as Hibernate) that make database operations extremely simple, and the way PHP runs determines that it is a restricted area for ORM (of course you can also do ORM, but to what extent you can do it depends on your ORM Determined by the tolerance of the performance loss caused).

(4) Caching technology

Java is a single process. Many caches can be done directly in the Java heap without resorting to external tools. Of course, there are also many Good caching frameworks, such as Ehcache, have very high performance because there is no network IO.

PHP multi-process and single-thread determines that it can only use external cache servers, such as Memcache.

(5) Hot deployment

Java The hot deployment capability is very weak. If you want to fix a bug without stopping the server, it is difficult to do so. PHP is naturally hot-deployable.

(6) Development cost

A good Java programmer requires more knowledge reserves, and the cycle required for development and debugging is longer. A better web Servers are also charged. PHP is free, and the web server is also free.

(7) Security

This depends on how you define security. If it is code security, it is easy to decompile java class. There are two reasons for this: Almost the same. Java has a security configuration mechanism to ensure that some "illegal operations" cannot be executed. In this regard, PHP is weaker. However, while Java can easily cause the entire application to crash due to a bug, PHP is much safer.

The above is the detailed content of What is the difference between php and java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn