相同逻辑的php与golang代码效率对比,最好语言落谁家…
业务逻辑:
逐行读取一个3.6MB的文件,
把每行的字符串切割成数组,
计算二维数组中的经纬度值与目标地点的经纬度的距离,
符合限定距离范围的结果存入map中,
将结果覆盖写入一个新的文件,
计算程序执行的时间。

php版:
<?php
$stime=microtime(true);
const BJ_LAT = 39.9041999;
const BJ_LNG = 116.4073963;
const DISTANCE = 100;
$handle = fopen('air_route_data.txt','rb+');
$result = [];
while(!feof($handle)){
$lines = fgets($handle);
$airRoute = explode(' ',$lines);
$lat = (float)$airRoute[3];
$lng = (float)str_replace(array("\r\n", "\r", "\n"), "", $airRoute[4]);
$distance = getDistance($lat,$lng);
if ($distance <= DISTANCE){
$result[$airRoute[2]] = ['lat'=>$lat,'lng'=>$lng ];
}
}
fclose($handle);
$jsonString = json_encode($result,JSON_UNESCAPED_UNICODE);
file_put_contents('bjs_route_data.txt',$jsonString);
//$handle2 = fopen('bjs_route_data.txt',"w+");
//fwrite($handle2,$jsonString);
//fclose($handle2);
$etime=microtime(true);
$total=$etime-$stime; //计算差值
//echo "当前页面执行时间为:{$total} 秒";
$ms = $total*1000;
echo "当前页面执行时间为:{$ms} 毫秒";
function getDistance($lat1, $lng1, $lat2=39.9041999, $lng2=116.4073963)
{
$EARTH_RADIUS = 6378.137;
$radLat1 = rad($lat1);
$radLat2 = rad($lat2);
$a = $radLat1 - $radLat2;
$b = rad($lng1) - rad($lng2);
$s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
$s = $s * $EARTH_RADIUS;
$s = round($s * 100) / 100;
return $s;
}
function rad($d)
{
return $d * M_PI / 180.0;
}
没有启动nginx,走php-fpm,直接从命令行执行的。
php执行时间:

测试N次,大约在116ms左右
go版:
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"math"
"os"
"strconv"
"strings"
"time"
)
//北京经纬度
const BJ_LAT = 39.9041999
const BJ_LNG = 116.4073963
const DISTANCE = 100
type Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
}
func main() {
t := time.Now()
fi, err := os.Open("air_route_data.txt")
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
defer fi.Close()
result := make(map[string]*Location)
//result := make(map[string]Location)
br := bufio.NewReader(fi)
for {
data, _, c := br.ReadLine()
if c == io.EOF {
break
}
airRoute := strings.Fields(string(data))
lat,_ := strconv.ParseFloat(airRoute[3],64)
lng , _ := strconv.ParseFloat(airRoute[4],64)
if lng < 0 || lat < 0 {
continue
}
distance := GetDistance(lat,lng , BJ_LAT,BJ_LNG)
if distance <= DISTANCE {
//写数据
temp := new(Location)
temp.Lat = lat
temp.Lng = lng
result[airRoute[2]] = temp
//result[airRoute[2]] = Location{lat,lng}
}
}
//写文件
jsonString, _ := json.Marshal(result)
f, _ := os.OpenFile("bjs_route_data.txt", os.O_TRUNC | os.O_WRONLY , 0666) //打开文件
io.WriteString(f,string(jsonString))
defer f.Close()
elapsed := time.Since(t)
fmt.Println("当前页面执行时间为:", elapsed)
}
// 返回单位,千米
func GetDistance(lon1, lat1, lon2, lat2 float64) (distance float64) {
//赤道半径(单位m)
const EARTH_RADIUS = 6378.137
rad_lat1 := rad(lat1)
rad_lon1 := rad(lon1)
rad_lat2 := rad(lat2)
rad_lon2 := rad(lon2)
if rad_lat1 < 0 {
rad_lat1 = math.Pi/2 + math.Abs(rad_lat1)
}
if rad_lat1 > 0 {
rad_lat1 = math.Pi/2 - math.Abs(rad_lat1)
}
if rad_lon1 < 0 {
rad_lon1 = math.Pi*2 - math.Abs(rad_lon1)
}
if rad_lat2 < 0 {
rad_lat2 = math.Pi/2 + math.Abs(rad_lat2)
}
if rad_lat2 > 0 {
rad_lat2 = math.Pi/2 - math.Abs(rad_lat2)
}
if rad_lon2 < 0 {
rad_lon2 = math.Pi*2 - math.Abs(rad_lon2)
}
x1 := EARTH_RADIUS * math.Cos(rad_lon1) * math.Sin(rad_lat1)
y1 := EARTH_RADIUS * math.Sin(rad_lon1) * math.Sin(rad_lat1)
z1 := EARTH_RADIUS * math.Cos(rad_lat1)
x2 := EARTH_RADIUS * math.Cos(rad_lon2) * math.Sin(rad_lat2)
y2 := EARTH_RADIUS * math.Sin(rad_lon2) * math.Sin(rad_lat2)
z2 := EARTH_RADIUS * math.Cos(rad_lat2)
d := math.Sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) + (z1-z2)*(z1-z2))
theta := math.Acos((EARTH_RADIUS*EARTH_RADIUS + EARTH_RADIUS*EARTH_RADIUS - d*d) / (2 * EARTH_RADIUS * EARTH_RADIUS))
distance = theta * EARTH_RADIUS
return
}
//转化为弧度(rad)
func rad(d float64) (r float64) {
r = d * math.Pi / 180.0
return
}
没有go build或者go install, 直接使用go run执行。
go执行时间:

测试N次,大约在45ms以上
结论:
试验过程中,尽量严谨。去除了nginx,两者没有借助web服务器启动,都是直接执行。
结果是相同的for循环10w+以上的代码逻辑,go的执行效率要比php快一倍以上。
php的一个数组解决一切数据格式问题,变量使用无需定义,数据类型和格式无需严谨,随用随定义等等特点,再加上php的web生态,已有的轮子特别多,这些决定了php做网站开发的速度是非常快的。
缺点是历史上的php4、php5遗留下的不严谨,很多开源代码框架cms有漏洞、效率、可维护等的问题。这些问题在未来的8,应该能更好的解决或者弥补。
但是,php语言本身的定位决定了执行速度的瓶颈。毕竟执行效率比go要慢。