业务逻辑:
【相同逻辑的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("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;//计算差值//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 mainimport ( "bufio" "encoding/json" "fmt" "io" "math" "os" "strconv" "strings" "time")//北京经纬度const BJ_LAT = 39.9041999const BJ_LNG = 116.4073963const DISTANCE = 100type 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 {//写数据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_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服务器启动,都是直接执行 。
推荐阅读
- 金银花露不能连续服用,金银花茶的保留先容
- 一款免费的数据库工具,比Navicat还要好用,功能还很强大
- 太厉害了!这应该是目前Redis可视化工具最全的横向评测
- Java15的新特性
- 客厅茶几的摆放风水禁忌
- 客厅的颜色搭配有什么宜忌?
- 客厅采光的方向与颜色的搭配
- 不同朝向住宅的客厅用什么颜色好?
- 客厅的沙发应当如何摆放?
- 客厅格局的影响不可小视