golang写出来的程序为什么那么慢

cnchanghai · · 4283 次点击
把`Command("ping",ips,"-c 1 -W 1")` 换成`Command("ping", ips, "-c", "1", "-W", "1")` 每个选项是一个独立的参数. 代码修改了一下, **1秒多**就完成了. ```Go package main import ( "fmt" "os/exec" "runtime" "strconv" "sync" "sync/atomic" ) var counter int32 = 0 func pings(ips string, w *sync.WaitGroup) { c := exec.Command("ping", ips, "-c", "1", "-W", "1") err := c.Run() if err == nil { atomic.AddInt32(&counter, 1) fmt.Printf("ip addr%s is ok\n", ips) } w.Done() } func main() { runtime.GOMAXPROCS(4) var w = &sync.WaitGroup{} for i := 1; i < 254; i += 1 { w.Add(1) ips := "202.102.201." + strconv.Itoa(i) go pings(ips, w) } w.Wait() fmt.Println(counter) } ``` Python代码没有格式化, 看不懂
#9
更多评论
你的系统是几核的?另外,并非 goroutine 开的越多,就越快
#1
python 的代码也贴出来看看。
#2