golang和php哪个性能更强,相同逻辑的php与golang代码效率
对⽐,最好语⾔落谁。。。
业务逻辑:
逐⾏读取⼀个3.6MB的⽂件,
把每⾏的字符串切割成数组,
计算⼆维数组中的经纬度值与⽬标地点的经纬度的距离,
符合限定距离范围的结果存⼊map中,
将结果覆盖写⼊⼀个新的⽂件,
计算程序执⾏的时间。
$stime=microtime(true);
const BJ_LAT = 39.9041999;const BJ_LNG = 116.4073963;const DISTANCE = 100;$handle =
fopen('air_','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_',$jsonString);
//$handle2 = fopen('bjs_',"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);
好用的cms$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_")
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 {
/
/写数据temp := new(Location)temp.Lat = lattemp.Lng = lngresult[airRoute[2]] = temp
//result[airRoute[2]] = Location{lat,lng}
}}//写⽂件jsonString, _ := json.Marshal(result)f, _ := os.OpenFile("bjs_", 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_RADIUSreturn
}//转化为弧度(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要慢。