php,go,go协程实现斐波那契数列用时比较长度50.

php:50长度时请求超时,长度40时用时在一分钟

//实现1000斐波那契数列
$start =  microtime();
for($i=0;$i<50;$i++){
    echo getfei($i)."<br>";
}

function getfei($i){
    if ($i<=1){
        $fei = 1;
    }else{
        $fei = getfei($i-1)+getfei($i-2);
    }
    return $fei;
}
echo microtime()-$start;

go递归用时:2m23.9160774s

package main

import (
   "fmt"
   "time"
)

//递归实现斐波那契数列
func main() {
   t := time.Now()
   var i int = 0
   for i = 0; i < 50; i++ {
      fe := fei(i)
      fmt.Printf(" vale is %v\n", fe)
   }
   defer func() {
      cost := time.Since(t)
      fmt.Println("cost=", cost)
   }()

}
func fei(a int) (f int) {

   if a <= 1 {
      f = 1
   } else {
      f = fei(a-1) + fei(a-2)
   }
   return
}

go协程用时:1.9949ms

package main

import (
   "fmt"
   "time"
)

func feibonacci(ch chan<- int, quit <-chan bool) {
   x, y := 1, 1
   for {
      select {
      case ch <- x:
         x, y = y, x+y
      case flag := <-quit:
         fmt.Println("flag =", flag)
         return
      }

   }
}
func main() {
   t := time.Now()
   ch := make(chan int)
   quit := make(chan bool)
   go func() {
      for i := 0; i < 50; i++ {
         num := <-ch
         fmt.Println(num)
      }
      quit <- true
   }()
   feibonacci(ch, quit)
   defer func() {
      cost := time.Since(t)
      fmt.Println("cost=", cost)
   }()

}

综上,php用时最长,go协程用时最短毫秒级的

golang基础

在函数调用时,像切片(slice)、字典(map)、接口(interface)、通道(channel)这样的引用类型 都是默认使用引用传递(即使没有显示的指出指针)

切片提供了计算容量的函数 cap() 可以测量切片最长可以达到多少:它等于切片的长度 + 数组除切片之外的长度。如果 s 是一个切片, cap(s) 就是从 s[0] 到数组末尾的数组长度。切片的长度永远不会超过它的容量,所以对于 切片 s 来说该不等式永远成立: 0 <= len(s) <= cap(s) 。

s := make([]byte, 8)
fmt.Println(len(s))//5
fmt.Println(cap(s))//5
s = s[2:4]
fmt.Println(len(s))//2
fmt.Println(cap(s))//6

如果想知道当前的内存状态,可以使用:

func main() {
   //自身占用 单位KB
   memStat := new(runtime.MemStats)
   runtime.ReadMemStats(memStat)
   mem := uint64(10)
   mem = memStat.Alloc
   fmt.Printf("%d\n", mem/1024)
}

go问题

执行go fmt或go build命令报错:

Get “https://proxy.golang.org/github.com/gin-gonic/gin/@v/list”: dial tcp 172.217.160.81:443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

解决:执行命令:

go env -w GOPROXY=https://goproxy.cn

go安装gin框架

1,把下载好的包放到go安装目录src下
2.代码:test.go  运行>go run test.go
package main

import "github.com/gin-gonic/gin"

func main() {
   r := gin.Default()
   r.GET("/ping", func(c *gin.Context) {
      c.JSON(200, gin.H{
         "message": "pong",
      })
   })
   r.Run() // listen and serve on 0.0.0.0:8080
}
3.浏览器输入http://127.0.0.1:8080/ping

显示:

{"message":"pong"}

go环境搭建

安装GO

1.下载安装包go1.15.windows-amd64.msi点击安装到自定义目录

2.配置:新加环境系统变量

PATH  D:\desktoptool\go\bin  //可执行文件目录

GOPATH  D:\desktoptool\gocode //代码目录

GOROOT  D:\desktoptool\go  //安装目录

3.cmd窗口输入下面命令查看:

D:\desktoptool\gocode>go version
go version go1.15 windows/amd64

D:\desktoptool\gocode>go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\14065\AppData\Local\go-build
set GOENV=C:\Users\14065\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\14065\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\14065\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=D:\desktoptool\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=D:\desktoptool\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\14065\AppData\Local\Temp\go-build611408406=/tmp/go-build -gno-record-gcc-switches

安装IDE goland