業(yè)務(wù)邏輯:
逐行讀取一個(gè)3.6MB的文件,
把每行的字符串切割成數(shù)組,
計(jì)算二維數(shù)組中的經(jīng)緯度值與目標(biāo)地點(diǎn)的經(jīng)緯度的距離,
符合限定距離范圍的結(jié)果存入map中,
將結(jié)果覆蓋寫入一個(gè)新的文件,
計(jì)算程序執(zhí)行的時(shí)間。

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("rn", "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; //計(jì)算差值
//echo "當(dāng)前頁(yè)面執(zhí)行時(shí)間為:{$total} 秒";
$ms = $total*1000;
echo "當(dāng)前頁(yè)面執(zhí)行時(shí)間為:{$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;
}
沒有啟動(dòng)Nginx,走php-fpm,直接從命令行執(zhí)行的。
php執(zhí)行時(shí)間:

測(cè)試N次,大約在116ms左右
go版:
package main
import ( "bufio"
"encoding/json"
"fmt"
"io"
"math"
"os"
"strconv"
"strings"
"time"
)//北京經(jīng)緯度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: %sn", 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 {
//寫數(shù)據(jù) 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("當(dāng)前頁(yè)面執(zhí)行時(shí)間為:", 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
}//轉(zhuǎn)化為弧度(rad)
func rad(d float64) (r float64) {
r = d * math.Pi / 180.0
return
}
沒有g(shù)o build或者go install, 直接使用go run執(zhí)行。
go執(zhí)行時(shí)間:

測(cè)試N次,大約在45ms以上
結(jié)論:
試驗(yàn)過(guò)程中,盡量嚴(yán)謹(jǐn)。去除了nginx,兩者沒有借助web服務(wù)器啟動(dòng),都是直接執(zhí)行。
結(jié)果是相同的for循環(huán)10w+以上的代碼邏輯,go的執(zhí)行效率要比php快一倍以上。
php的一個(gè)數(shù)組解決一切數(shù)據(jù)格式問(wèn)題,變量使用無(wú)需定義,數(shù)據(jù)類型和格式無(wú)需嚴(yán)謹(jǐn),隨用隨定義等等特點(diǎn),再加上php的web生態(tài),已有的輪子特別多,這些決定了php做網(wǎng)站開發(fā)的速度是非常快的。
缺點(diǎn)是歷史上的php4、php5遺留下的不嚴(yán)謹(jǐn),很多開源代碼框架cms有漏洞、效率、可維護(hù)等的問(wèn)題。這些問(wèn)題在未來(lái)的8,應(yīng)該能更好的解決或者彌補(bǔ)。
但是,php語(yǔ)言本身的定位決定了執(zhí)行速度的瓶頸。畢竟執(zhí)行效率比go要慢。