php7 怎么比 Java 还快?

2019-09-23 11:22:47 +08:00
 zjsxwc

有点反认知,分别用 PHP 与 Java Spring Boot 写了个返回 1 个像素点图片的接口,结果 php 比 java 快。

代码

PHP 代码

<?php
//xpng.php

header("Content-type: image/png");

echo base64_decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWP4////fwAJ+wP9CNHoHgAAAABJRU5ErkJggg==");

Java 代码

//XController.java

package com.abtest;

import java.util.Base64;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class XController {
    @RequestMapping("/x.png")
    public ResponseEntity<byte[]> xpng() {
        String onepointpngBase64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWP4////fwAJ+wP9CNHoHgAAAABJRU5ErkJggg==";
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.set("Content-type", "image/png");
        byte[] decodedValue = Base64.getDecoder().decode(onepointpngBase64);
        return new ResponseEntity<byte[]>(decodedValue, responseHeaders, HttpStatus.OK);
    }
}

开始测试

Server 分别 SpringBoot Jar 包自带的 Apache Tomcat/9.0.24

PHP 就是 PHP 7.0.33-0+deb9u1 (cli) 自带的低性能调试用 server (使用php -S localhost:7767 -t .运行)

AB 结果反直觉,PHP 居然比 Java 快

PHP 结果耗时 0.125 秒
$ ab -n 1000 -c 1000 "http://localhost:7767/xpng.php"
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:       
Server Hostname:        localhost
Server Port:            7767

Document Path:          /xpng.php
Document Length:        70 bytes

Concurrency Level:      1000
Time taken for tests:   0.125 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      190000 bytes
HTML transferred:       70000 bytes
Requests per second:    8011.99 [#/sec] (mean)
Time per request:       124.813 [ms] (mean)
Time per request:       0.125 [ms] (mean, across all concurrent requests)
Transfer rate:          1486.60 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    6   5.5      8      14
Processing:     5   22  20.1     13      69
Waiting:        4   22  20.2     12      69
Total:          8   29  21.3     19      71

Percentage of the requests served within a certain time (ms)
  50%     19
  66%     24
  75%     37
  80%     62
  90%     68
  95%     70
  98%     70
  99%     70
 100%     71 (longest request)
Java 结果耗时 0.498 秒
$ ab -n 1000 -c 1000 "http://localhost:8080/x.png"
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 100 requests
Completed 200 requests
Completed 300 requests
Completed 400 requests
Completed 500 requests
Completed 600 requests
Completed 700 requests
Completed 800 requests
Completed 900 requests
Completed 1000 requests
Finished 1000 requests


Server Software:       
Server Hostname:        localhost
Server Port:            8080

Document Path:          /x.png
Document Length:        70 bytes

Concurrency Level:      1000
Time taken for tests:   0.498 seconds
Complete requests:      1000
Failed requests:        0
Total transferred:      188000 bytes
HTML transferred:       70000 bytes
Requests per second:    2007.85 [#/sec] (mean)
Time per request:       498.046 [ms] (mean)
Time per request:       0.498 [ms] (mean, across all concurrent requests)
Transfer rate:          368.63 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   3.2      0      11
Processing:     5   73  63.3     48     237
Waiting:        3   64  56.7     41     194
Total:          5   74  66.0     48     247

Percentage of the requests served within a certain time (ms)
  50%     48
  66%     63
  75%     76
  80%     88
  90%    216
  95%    225
  98%    229
  99%    234
 100%    247 (longest request)
16558 次点击
所在节点    程序员
134 条回复
zjsxwc
2019-09-23 17:23:09 +08:00
@qsbaq #40 原文:“楼猪,你有没有把 PHP 的 OPCACHE 打开?”
回复:没开
gimp
2019-09-23 17:30:21 +08:00
我运行楼主的 node.js 代码
2871 ms

翻译成 Rust,4701ms ... (思考脸

看有的回复提到变量类型,遂把 n, i, j 从 i32 换成 u32 和 u8 类型代替后,49ms ... (思考脸
notreami
2019-09-23 17:34:15 +08:00
2019 年了,能活下来的,性能、周边、背书、易用都不会太差。
2019 年了,考验的是某个领域的杀手锏工具,比如 java 的大数据领域、python 的 AI 领域、golang 的运维工具领域、js 的前端领域。而 php 在最近 10 年,好像没啥特别领域突破。
qsbaq
2019-09-23 17:34:58 +08:00
@zjsxwc 打开 opcache 再试,PHP 会比现在的速度快 N 倍
cloudzhou
2019-09-23 17:38:27 +08:00
https://paste.ubuntu.com/p/JCRRGCCqYx/ ,修改了一点小逻辑

在我的机器里:
@X220:/tmp$ java -cp . pn
第 300000 个素数的值是:4256233 耗时 4911 毫秒
@X220:/tmp$ go run /tmp//tt.go
第 300000 个素数的值是:4256233 耗时 5165 毫秒

非常接近
luozic
2019-09-23 17:41:54 +08:00
photon006
2019-09-23 17:42:56 +08:00
上面 node 代码改下,返回值固定死了,应该是这样:
```
'use strict';
const http = require('http');

http.createServer(function (req, res) {
let data = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWP4////fwAJ+wP9CNHoHgAAAABJRU5ErkJggg==';
// console.time('耗时:')
let buff = Buffer.from(data, 'base64');
let text = buff.toString('ascii');
// console.timeEnd('耗时:')
res.writeHead(200, {'Content-type': 'image/png'});
res.write('text');
res.end();
}).listen(8080);

```

在 raspberry pi4 上运行,node 还是 10.13.0LTS

冷启动:0.6ms
重试维持在 0.430ms
Rekkles
2019-09-23 17:44:35 +08:00
开 fpm + opcache 会快的你怀疑人生
Mac
2019-09-23 17:47:23 +08:00
opcache 不开谈什么快😂
zjsxwc
2019-09-23 17:51:09 +08:00
@Mac
@Rekkles
@qsbaq

我上面说了不开 opcache web 访问速度也都一样,
计算密集的 cli 程序怎么用 opcache
silenceeeee
2019-09-23 17:55:30 +08:00
PHP 为啥这么慢,有没有大神解释下原因
shansing
2019-09-23 18:03:20 +08:00
@auciou2 你记错了吧。7.2 相对 7.0 才叫提升大。7.4 相对 7.3 没差多少啊。
zhuzeitou
2019-09-23 18:03:48 +08:00
@cloudzhou `if j > i/j {`这个分支的改动对 java 的影响微乎其微,但对 golang 影响很大
另外,在`if i%j == 0`这个分支最后加一个`continue`,会有惊喜……

https://paste.ubuntu.com/p/3X9hB4tdzc/
xfriday
2019-09-23 18:09:22 +08:00
@zhuzeitou 你也是鬼才,你说的那个 continue 居然能提高 1s 多,go 的编译器在搞啥哦
dif
2019-09-23 18:11:19 +08:00
现在不玩性能了,都看生态已经关键领域的。
fatelight
2019-09-23 18:13:11 +08:00
再你说得天花乱坠,我还是 java
xfriday
2019-09-23 18:13:34 +08:00
上面那个 continue 加上直接比 c 还快了,2.24s 。。。golang 的 if 没优化好?
ourleven
2019-09-23 18:22:52 +08:00
总结:
Js 天下第一
auciou2
2019-09-23 18:25:46 +08:00
@shansing 没有记错,5.3、5.4、5.5、5.6、7.0 ~ 7.4 我都逐个版本试过了。7.4 到 7.3 的提升很大。
zhuzeitou
2019-09-23 18:29:26 +08:00
@xfriday 我已经想不起来为啥我要试着加这个 continue 了

然后又折腾了一下,把判断条件反了一下,好像还能提升一点点……

https://paste.ubuntu.com/p/ByXy3gYfrW/

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/603272

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX