测试机器配置:
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 ); } $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); } 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 { public static String sendGet (String url, String param) { String result = "" ; BufferedReader in = null ; try { String urlNameString = url + "?" + param; URL realUrl = new URL (urlNameString); 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(); 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 { try { if (in != null ) { in.close(); } } catch (Exception e2) { e2.printStackTrace(); } } return result; } public static String sendPost (String url, String param) { PrintWriter out = null ; BufferedReader in = null ; String result = "" ; try { URL realUrl = new 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)" ); conn.setDoOutput(true ); conn.setDoInput(true ); out = new PrintWriter (conn.getOutputStream()); out.print(param); out.flush(); 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 { 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 mainimport ( "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' ); 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++ 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 ): data = get("http://2018.ip138.com/ic.asp?count=" +str (i)) 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
目前这几种编程语言看来,排序如下:
NodeJs 平均 0.23s
Go 平均 0.97s
Java 平均 1.38s
PHP 平均 2.03s
Python 平均 2.05s
Ps: 如果你希望看到自己喜欢的语言也排进来,欢迎提供代码!