`性能`标签下的文章

编程语言

不同语言下文件读取和排序测试

排序时间对比图

测试机器配置:

  • MacBook Air (13-inch, Early 2014)
  • 处理器:1.4 GHz Intel Core i5
  • 内存:4 GB 1600 MHz DDR3

PHP

1
2
3
PHP 7.1.16 (cli) (built: Mar 31 2018 02:59:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
$t1 = microtime(true);

$fileName = "Data.txt";
if(($file=fopen($fileName,'r')) === false){
echo '读取文件:'.$fileName."失败\n";
exit;
}
if(empty($file)){
echo "文件为空!\n";
exit;
}
$data = array();
while(!feof($file)){
$lineStr = fgets($file);
array_push($data, trim($lineStr));
}
$t2 = microtime(true);
echo 'Read time:' . ($t2 - $t1)*1000 . "ms\n";
BubbleSort($data);
var_dump($data);
fclose($file);

$t3 = microtime(true);

echo 'Sort time:' . ($t3 - $t2)*1000 . "ms\n";
echo 'Finished time:' . ($t3 - $t1)*1000 . "ms\n";

function BubbleSort(&$data){
$len = count($data);
for($i=0;$i<$len;$i++){
$flag = 0;
for($j=1;$j<($len-$i);$j++){
if($data[$j]<$data[$j-1]) {
$tmp = $data[$j];
$data[$j] = $data[$j-1];
$data[$j-1] = $tmp;
$flag = 1;
}
}
if($flag == 0){
return;
}
}
}
?>

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
~/Sites/languageDiff/sort » php sort.php
Read time:20.021915435791ms
Sort time:219256.12616539ms
Finished time:219276.14808083ms
-------------------------------------------------------
~/Sites/languageDiff/sort » php sort.php
Read time:19.091129302979ms
Sort time:229677.68502235ms
Finished time:229696.77615166ms
-------------------------------------------------------
~/Sites/languageDiff/sort » php sort.php
Read time:21.70205116272ms
Sort time:228220.97086906ms
Finished time:228242.67292023ms
-------------------------------------------------------
~/Sites/languageDiff/sort » php sort.php
Read time:20.930051803589ms
Sort time:206329.57100868ms
Finished time:206350.50106049ms
-------------------------------------------------------
~/Sites/languageDiff/sort » php sort.php
Read time:18.213033676147ms
Sort time:201813.07411194ms
Finished time:201831.28714561ms

Java

1
2
3
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class sort {

public static void main(String[] args) {
try {
long t1 = System.currentTimeMillis();
int[] data = new int[50000];
BufferedReader br = new BufferedReader(new FileReader("Data.txt"));

String line = null;
int index = 0;
while ((line = br.readLine()) != null) {
data[index] = Integer.parseInt(line);
index++;
}
if (br != null) {
br.close();
}
long t2 = System.currentTimeMillis();
System.out.println("Read time: " + String.valueOf(t2 - t1) + "ms");

BubbleSort(data);
long t3 = System.currentTimeMillis();
System.out.println("Sort time: " + String.valueOf(t3 - t2) + "ms");
System.out.println("Finished time: " + String.valueOf(t3 - t1) + "ms");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void BubbleSort(int[] a) {
int j, flag;
int temp;
for (int i = 0; i < a.length; i++) {
flag = 0;
for (j = 1; j < a.length - i; j++) {
if (a[j] < a[j-1]) {
temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
flag = 1;
}
}
if (flag == 0) {
return;
}
}
}
}

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
~/Sites/languageDiff/sort » java sort
Read time: 56ms
Sort time: 5286ms
Finished time: 5342ms
-------------------------------------------------------
~/Sites/languageDiff/sort » java sort
Read time: 47ms
Sort time: 4911ms
Finished time: 4958ms
-------------------------------------------------------
~/Sites/languageDiff/sort » java sort
Read time: 46ms
Sort time: 4903ms
Finished time: 4949ms
-------------------------------------------------------
~/Sites/languageDiff/sort » java sort
Read time: 47ms
Sort time: 4953ms
Finished time: 5000ms
-------------------------------------------------------
~/Sites/languageDiff/sort » java sort
Read time: 49ms
Sort time: 5335ms
Finished time: 5384ms

Go

1
go version go1.10.3 darwin/amd64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main

import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"time"
)

func main() {
fileName := "Data.txt"
data := make([]int, 0, 50000)
start := time.Now()
fi, err := os.Open(fileName)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
defer fi.Close()
br := bufio.NewReader(fi)
for {
a, _, c := br.ReadLine()
if c == io.EOF {
break
}
num, _ := strconv.Atoi(string(a))
data = append(data, num)
}
endReadTime := time.Now()
fmt.Printf("Read time: %fs\n", endReadTime.Sub(start).Seconds())
BubbleSort(data)
endTime := time.Now()
fmt.Printf("Sort time: %fs\n", endTime.Sub(endReadTime).Seconds())
fmt.Printf("finished time: %fs\n", endTime.Sub(start).Seconds())
}

func BubbleSort(arr []int) {
length := len(arr)
var flag int
var tmp int
for i := 0; i < length; i++ {
flag = 0
for j := 1; j < (length - i); j++ {
if arr[j] < arr[j-1] {
tmp = arr[j]
arr[j] = arr[j-1]
arr[j-1] = tmp
flag = 1
}
}
if flag == 0 {
return
}
}
}

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
~/Sites/languageDiff/sort » go build sort.go
~/Sites/languageDiff/sort » ./sort
Read time: 0.006603s
Sort time: 5.207361s
finished time: 5.213964s
-------------------------------------------------------
~/Sites/languageDiff/sort » ./sort
Read time: 0.005319s
Sort time: 5.167615s
finished time: 5.172934s
-------------------------------------------------------
~/Sites/languageDiff/sort » ./sort
Read time: 0.005648s
Sort time: 5.317512s
finished time: 5.323160s
-------------------------------------------------------
~/Sites/languageDiff/sort » ./sort
Read time: 0.005842s
Sort time: 5.267731s
finished time: 5.273573s
-------------------------------------------------------
~/Sites/languageDiff/sort » ./sort
Read time: 0.005585s
Sort time: 5.442154s
finished time: 5.447738s

JavaScript(NodeJS)

1
2
~/Sites » node -v
v9.11.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
var t1 = (new Date()).getTime();  
var readline = require('readline');
var fs = require('fs');
var os = require('os');

var fReadName = './data.txt';
var fRead = fs.createReadStream(fReadName);


var objReadline = readline.createInterface({
input: fRead,
});


var data = [];
objReadline.on('line', (line)=>{
data.push(Number(line));
});

objReadline.on('close', ()=>{
var t2 = (new Date()).getTime();
console.log("Read time:" + (t2 - t1) + "ms");
var res = BubbleSort(data);
// console.log(res);
var t3 = (new Date()).getTime();
console.log("Sort time:" + (t3 - t2) + "ms");
console.log("finished time:" + (t3 - t1) + "ms");
});

function BubbleSort(arr){
for (var i = 0; i < arr.length; i++) {
var flag = 0;
for(var j = 1;j< arr.length-i;j++){
if(arr[j]<arr[j-1]){
var tmp = arr[j];
arr[j] = arr[j-1]
arr[j-1] = tmp
flag = 1;
}
}
if(flag == 0){
return arr;
}
}
return arr;
}

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
~/Sites/languageDiff/sort » node sort.js
Read time:55ms
Sort time:6067ms
finished time:6122ms
-------------------------------------------------------
~/Sites/languageDiff/sort » node sort.js
Read time:38ms
Sort time:6064ms
finished time:6102ms
-------------------------------------------------------
~/Sites/languageDiff/sort » node sort.js
Read time:37ms
Sort time:6339ms
finished time:6376ms
-------------------------------------------------------
~/Sites/languageDiff/sort » node sort.js
Read time:42ms
Sort time:6367ms
finished time:6409ms
-------------------------------------------------------
~/Sites/languageDiff/sort » node sort.js
Read time:40ms
Sort time:7110ms
finished time:7150ms

Python

1
2
~/Sites » python -V
Python 2.7.10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import time

def BubbleSort(a):
length = len(a)
for i in range(0,length):
flag = 0
for j in range(1,length-i):
if a[j] < a[j-1]:
tmp = a[j]
a[j] = a[j-1]
a[j-1] = tmp
flag = 1
if flag == 0:
return a


t1 = time.time()
fileName = "Data.txt"
data = []
for line in open(fileName):
data.append(int(line.strip()))
t2 = time.time()
print 'Read time: %.02fs' % (t2 - t1)
res = BubbleSort(data)
t3 = time.time()
print 'Sort time: %.02fs' % (t3 - t2)
print 'finished time: %.02fs' % (t3 - t1)

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-------------------------------------------------------
~/Sites/languageDiff/sort » python sort.py
Read time: 0.05s
Sort time: 282.76s
finished time: 282.82s
-------------------------------------------------------
~/Sites/languageDiff/sort » python sort.py
Read time: 0.07s
Sort time: 263.54s
finished time: 263.61s
-------------------------------------------------------
~/Sites/languageDiff/sort » python sort.py
Read time: 0.06s
Sort time: 346.26s
finished time: 346.32s
-------------------------------------------------------
~/Sites/languageDiff/sort » python sort.py
Read time: 0.06s
Sort time: 298.12s
finished time: 298.18s
-------------------------------------------------------
~/Sites/languageDiff/sort » python sort.py
Read time: 0.06s
Sort time: 251.43s
finished time: 251.49s

目前这几种编程语言看来,排序如下:IO时长,排序时长,总时长

  1. Java 平均 0.0490s 5.078s 5.127s
  2. Go 平均 0.0058s 5.281s 5.286s
  3. NodeJs 平均 0.0424s 6.389s 6.432s
  4. PHP 平均 0.0200s 217.059s 217.079s
  5. Python 平均 0.0600s 288.422s 288.484s

Ps: 如果你希望看到自己喜欢的语言也排进来,欢迎提供代码!

阅读剩下更多

不同语言下文件读取和排序测试
编程语言

不同语言对网络IO的效率对比

网络IO时间对比图

测试机器配置:

  • MacBook Air (13-inch, Early 2014)
  • 处理器:1.4 GHz Intel Core i5
  • 内存:4 GB 1600 MHz DDR3

PHP

1
2
3
PHP 7.1.16 (cli) (built: Mar 31 2018 02:59:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$t1 = microtime(true);

for($i=0; $i<20; $i++){
$url = "http://2018.ip138.com/ic.asp?count=".$i;
$data = file_get_contents($url);
// echo 'get:'.$i." ".$data."\r\n";
}

$t2 = microtime(true);

echo 'php time:' . ($t2 - $t1)*1000 . "ms\n";

?>

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
~/Sites/languageDiff/io » php main.php
php time:2112.9629611969ms
-------------------------------------------------------
~/Sites/languageDiff/io » php main.php
php time:1960.746049881ms
-------------------------------------------------------
~/Sites/languageDiff/io » php main.php
php time:2055.7940006256ms
-------------------------------------------------------
~/Sites/languageDiff/io » php main.php
php time:2052.6430606842ms
-------------------------------------------------------
~/Sites/languageDiff/io » php main.php
php time:1990.2958869934ms

Java

1
2
3
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Main {

public static void main(String[] args) {
long t1 = System.currentTimeMillis();

for(int i=0; i<20; i++){
String s=HttpRequest.sendGet("http://2018.ip138.com/ic.asp", "count="+i);
// System.out.println(""+i+" : "+s);
}

long t2 = System.currentTimeMillis();

System.out.println("java time: " + String.valueOf(t2 - t1) + "ms");
}
}

HttpRequst.java CodeOrigin:https://www.cnblogs.com/guoyinli/p/7192839.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class HttpRequest {
/**
* 向指定URL发送GET方法的请求
*
* @param url
* 发送请求的URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return URL 所代表远程资源的响应结果
*/
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
/*for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}*/
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}

/**
* 向指定 URL 发送POST方法的请求
*
* @param url
* 发送请求的 URL
* @param param
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
* @return 所代表远程资源的响应结果
*/
public static String sendPost(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
}

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
~/Sites/languageDiff/io » java Main
java time: 1416ms
-------------------------------------------------------
~/Sites/languageDiff/io » java Main
java time: 1472ms
-------------------------------------------------------
~/Sites/languageDiff/io » java Main
java time: 1328ms
-------------------------------------------------------
~/Sites/languageDiff/io » java Main
java time: 1348ms
-------------------------------------------------------
~/Sites/languageDiff/io » java Main
java time: 1336ms

Go

1
go version go1.10.3 darwin/amd64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main

import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"sync"
"time"
)

func main() {
wg := sync.WaitGroup{}
start := time.Now()
for k := 0; k < 2; k++ {
wg.Add(1)
go func(k int) {
for i := 0; i < 10; i++ {
HttpGet("http://2018.ip138.com/ic.asp?count=" + strconv.Itoa(i*2+k))
}
wg.Done()
}(k)
}
wg.Wait()
fmt.Printf("cost time: %f\n", time.Now().Sub(start).Seconds())
}

func HttpGet(url string) string {
resp, err := http.Get(url)
if err != nil {
return err.Error()
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err.Error()
}
return string(body)
}

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
~/Workspaces/go » go run main.go
cost time: 0.952737
-------------------------------------------------------
~/Workspaces/go » go run main.go
cost time: 0.983901
-------------------------------------------------------
~/Workspaces/go » go run main.go
cost time: 0.967799
-------------------------------------------------------
~/Workspaces/go » go run main.go
cost time: 0.972302
-------------------------------------------------------
~/Workspaces/go » go run main.go
cost time: 0.980509

JavaScript(NodeJS)

1
2
~/Sites » node -v
v9.11.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var t1 = (new Date()).getTime();  
var job = 0;
for(var i=0; i<20; i++){
get(i);
}

var t2 = (new Date()).getTime();

console.log("nodejs time:" + (t2 - t1) + "ms");


function get(i){

var http=require('http');
//get 请求外网
http.get('http://2018.ip138.com/ic.asp?count='+i,function(req,res){
var html='';
req.on('data',function(data){
html+=data;
});
req.on('end',function(){
job++
// console.info(html);
if(job == 20){
var t2 = (new Date()).getTime();
console.log("finished time:" + (t2 - t1) + "ms");
}
});
});
}

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
~/Sites/languageDiff/io » node main.js
nodejs time:25ms
finished time:236ms
-------------------------------------------------------
~/Sites/languageDiff/io » node main.js
nodejs time:21ms
finished time:220ms
-------------------------------------------------------
~/Sites/languageDiff/io » node main.js
nodejs time:22ms
finished time:230ms
-------------------------------------------------------
~/Sites/languageDiff/io » node main.js
nodejs time:21ms
finished time:227ms
-------------------------------------------------------
~/Sites/languageDiff/io » node main.js
nodejs time:20ms
finished time:221ms

Python

1
2
~/Sites » python -V
Python 2.7.10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import time,urllib2

def get(url):
req = urllib2.Request(url)

res_data = urllib2.urlopen(req)
res = res_data.read()
return res


t = time.time()

for i in xrange(0, 20):
# print "Get("+str(i)+"):"
data = get("http://2018.ip138.com/ic.asp?count="+str(i))
# print data

print 'Python time: %.02fs' % (time.time() - t)

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
~/Sites/languageDiff/io » python main.py
Python time: 1.98s
-------------------------------------------------------
~/Sites/languageDiff/io » python main.py
Python time: 1.98s
-------------------------------------------------------
~/Sites/languageDiff/io » python main.py
Python time: 2.01s
-------------------------------------------------------
~/Sites/languageDiff/io » python main.py
Python time: 2.07s
-------------------------------------------------------
~/Sites/languageDiff/io » python main.py
Python time: 2.20s
-------------------------------------------------------
~/Sites/languageDiff/io » python main.py
Python time: 2.08s

目前这几种编程语言看来,排序如下:

  1. NodeJs 平均 0.23s
  2. Go 平均 0.97s
  3. Java 平均 1.38s
  4. PHP 平均 2.03s
  5. Python 平均 2.05s

Ps: 如果你希望看到自己喜欢的语言也排进来,欢迎提供代码!

阅读剩下更多

不同语言对网络IO的效率对比
编程语言

不同语言的基本性能测试

计算时间对比图
测试机器配置:

  • MacBook Air (13-inch, Early 2014)
  • 处理器:1.4 GHz Intel Core i5
  • 内存:4 GB 1600 MHz DDR3

PHP

1
2
3
PHP 7.1.16 (cli) (built: Mar 31 2018 02:59:59) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
$t1 = microtime(true);

for($i=0; $i<10000000; $i++){
aaa($i);
}

$t2 = microtime(true);

echo 'php time:' . ($t2 - $t1)*1000 . "ms\n";

function aaa($i){
$a = $i + 1;
$b = 2.3;
$s = "abcdefkkbghisdfdfdsfds";

if($a > $b){
++$a;
}else{
$b = $b + 1;
}

if($a == $b){
$b = $b + 1;
}

$c = $a * $b + $a / $b - pow($a, 2);
$d = substr($s, 0, strpos($s, 'kkb')) . strval($c);
}
?>

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
~/Sites » php tes.php
php time:8196.1958408356ms
-------------------------------------------------------
~/Sites » php tes.php
php time:7649.4419574738ms
-------------------------------------------------------
~/Sites » php tes.php
php time:7908.7510108948ms
-------------------------------------------------------
~/Sites » php tes.php
php time:7708.6410522461ms
-------------------------------------------------------
~/Sites » php tes.php
php time:7515.1500701904ms
-------------------------------------------------------
~/Sites » php tes.php
php time:7468.3780670166ms

Java

1
2
3
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Main {

public static void main(String[] args) {
long t1 = System.currentTimeMillis();

for(int i=0; i<10000000; i++){
aaa((float)i);
}

long t2 = System.currentTimeMillis();

System.out.println("java time: " + String.valueOf(t2 - t1) + "ms");
}

static void aaa(float i) {
float a = i + 1;
float b = 2.3f;
String s = "abcdefkkbghisdfdfdsfds";

if(a > b){
++a;
}else{
b = b + 1;
}

if(a == b){
b = b + 1;
}

float c = (float)(a * b + a / b - Math.pow(a, 2));
String d = s.substring(0, s.indexOf("kkb")) + String.valueOf(c);
}
}

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
~/Sites » java Main
java time: 1537ms
-------------------------------------------------------
~/Sites » java Main
java time: 1497ms
-------------------------------------------------------
~/Sites » java Main
java time: 1466ms
-------------------------------------------------------
~/Sites » java Main
java time: 1610ms
-------------------------------------------------------
~/Sites » java Main
java time: 1488ms
-------------------------------------------------------
~/Sites » java Main
java time: 1503ms
-------------------------------------------------------
~/Sites » java Main
java time: 1475ms

Go

1
go version go1.10.3 darwin/amd64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package main

import (
"fmt"
"math"
"strconv"
"strings"
"time"
)

func main() {
t1 := time.Now()
for i := 0; i < 10000000; i++ {
Aaa(float64(i))
}
t2 := time.Now()
fmt.Printf("Go time: %f s\n", t2.Sub(t1).Seconds())
}

func Aaa(i float64) {
var a float64 = i + 1
var b float64 = 2.3
s := "abcdefkkbghisdfdfdsfds"

if a > b {
a++
} else {
b = b + 1
}

if a == b {
b = b + 1
}

c := a*b + a/b - math.Pow(a, 2)
d := s[0:strings.Index(s, "kkb")] + strconv.FormatFloat(c, 'E', -1, 64)
_ = d
}

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
~/Workspaces/GoLand/src/Aaa » go build main.go
-------------------------------------------------------
~/Workspaces/GoLand/src/Aaa » ./main
Go time: 4.083166 s
-------------------------------------------------------
~/Workspaces/GoLand/src/Aaa » ./main
Go time: 4.095651 s
-------------------------------------------------------
~/Workspaces/GoLand/src/Aaa » ./main
Go time: 4.154200 s
-------------------------------------------------------
~/Workspaces/GoLand/src/Aaa » ./main
Go time: 4.175203 s
-------------------------------------------------------
~/Workspaces/GoLand/src/Aaa » ./main
Go time: 4.111375 s

Go并发版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main

import (
"fmt"
"math"
"strconv"
"strings"
"sync"
"time"
)

func main() {
wg := sync.WaitGroup{}
t1 := time.Now()
for k := 0; k < 2; k++ {
wg.Add(1)
go func() {
for i := 0; i < 5000000; i++ {
Aaa(float64(i))
}
wg.Done()
}()
}
wg.Wait()
t2 := time.Now()
fmt.Printf("Go time: %f s\n", t2.Sub(t1).Seconds())
}

func Aaa(i float64) {
var a float64 = i + 1
var b float64 = 2.3
s := "abcdefkkbghisdfdfdsfds"

if a > b {
a++
} else {
b = b + 1
}

if a == b {
b = b + 1
}

c := a*b + a/b - math.Pow(a, 2)
_ = s[0:strings.Index(s, "kkb")] + strconv.FormatFloat(c, 'E', -1, 64)
}

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
~/Workspaces/GoLand/src/Aaa » go build main.go
-------------------------------------------------------
~/Workspaces/GoLand/src/Aaa » ./main
Go time: 2.326077 s
-------------------------------------------------------
~/Workspaces/GoLand/src/Aaa » ./main
Go time: 2.270769 s
-------------------------------------------------------
~/Workspaces/GoLand/src/Aaa » ./main
Go time: 2.277345 s
-------------------------------------------------------
~/Workspaces/GoLand/src/Aaa » ./main
Go time: 2.252540 s
-------------------------------------------------------
~/Workspaces/GoLand/src/Aaa » ./main
Go time: 2.255374 s

JavaScript(NodeJS)

1
2
~/Sites » node -v
v9.11.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var t1 = (new Date()).getTime();  

for(var i=0; i<10000000; i++){
aaa(i);
}

var t2 = (new Date()).getTime();

console.log("nodejs time:" + (t2 - t1) + "ms");


function aaa(i){
var a = i + 1;
var b = 2.3;
var s = "abcdefkkbghisdfdfdsfds";

if(a > b){
++a;
}else{
b = b + 1;
}

if(a == b){
b = b + 1;
}

var c = a * b + a / b - Math.pow(a, 2);

var d = s.substring(0, s.indexOf("kkb")) + c.toString();
}

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
~/Sites » node main.js
nodejs time:4898ms
-------------------------------------------------------
~/Sites » node main.js
nodejs time:5567ms
-------------------------------------------------------
~/Sites » node main.js
nodejs time:4846ms
-------------------------------------------------------
~/Sites » node main.js
nodejs time:4853ms
-------------------------------------------------------
~/Sites » node main.js
nodejs time:4955ms
-------------------------------------------------------
~/Sites » node main.js
nodejs time:5113ms

Python

1
2
~/Sites » python -V
Python 2.7.10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import sys, time, math

def aaa(i):
a = i + 1
b = 2.3
s = "abcdefkkbghisdfdfdsfds"

if a > b:
a = a + 1
else:
b = b + 1

if a == b:
b = b + 1

c = a * b +a / b - math.pow(a, 2)

d = s[0: s.find("kkb")] + str(c)


t = time.time()

for i in xrange(0, 10000000):
aaa(i)

print 'Python time: %.02f s' % (time.time() - t)

测试结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
~/Sites » python main.py
Python time: 20.34
-------------------------------------------------------
~/Sites » python main.py
Python time: 19.90
-------------------------------------------------------
~/Sites » python main.py
Python time: 20.07
-------------------------------------------------------
~/Sites » python main.py
Python time: 19.88
-------------------------------------------------------
~/Sites » python main.py
Python time: 22.89

目前这几种编程语言看来,排序如下:

  1. Java 平均 1.51s (稳居第一)
  2. Go 平均 3.94s (并发2.28s)
  3. NodeJs 平均 5.03s
  4. PHP 平均 7.65s
  5. Python 平均 20.62s (差距大应该是我对这语言不熟,密集运算应该有优化方案)

Ps: 如果你希望看到自己喜欢的语言也排进来,欢迎提供代码!

阅读剩下更多

不同语言的基本性能测试
返回顶部