Golang中的嵌套循环字符串操作花费异常的时间。

huangapple go评论68阅读模式
英文:

Golang for loop inside for loop string operation taking abnormal time

问题

Golang版本:go version go1.13.5 windows/amd64

请查看以下代码:

https://go.dev/play/p/pfqXLnuQfYA

当我们在play上运行它时,会超时。

在我的本地Windows机器上,16GB内存需要50秒的时间。
另一台Unix机器,96GB内存需要1分08秒的时间。

同样的代码变体需要超过5分钟的时间https://go.dev/play/p/WMPvGYPSDjJ
而在Java中,这个程序只需要一分钟。

请指出这里出了什么问题?
为什么会花费这么长时间?

英文:

Golang version : go version go1.13.5 windows/amd64

Please take a look at this code :

https://go.dev/play/p/pfqXLnuQfYA

When we run it on play then it is timing out.

In my local windows machine 16GB ram taking 50 seconds time.
Another unix machine 96GB ram taking 1minute 8 seconds time.

Another variation of same code taking more than 5 minutes https://go.dev/play/p/WMPvGYPSDjJ
While in java this program takes a minute only.

Please give pointers what is going wrong in this?
How come it is taking so much time ?

答案1

得分: 1

如@icza所指出的,您创建了未使用并被垃圾回收的字符串,并且还使用了旧版本的Go。

这个程序在我的机器上使用go version go1.17.5 linux/amd64运行时间为6.3秒。

func main() {
    startTime := time.Now()
    log.Println("for loop started")
    a := make([]string, 0)
    a = append(a, "N")
    x := 0
    for i := 0; i < 47831; i++ {
        for j := 0; j < 47831; j++ {
            a[0] = strings.ToLower(a[0])
            x++
        }
    }
    endTime := time.Now()
    log.Println("for loop end, time taken=", endTime.Sub(startTime), "iters=", x)
}

2021/12/20 10:26:22 for loop started
2021/12/20 10:26:29 for loop end, time taken= 6.323187151s iters= 2287804561
英文:

As @icza pointed out, you're creating strings that are not used and get garbage collected, additionally using an old version of Go.

This program runs in 6.3s in my machine with go version go1.17.5 linux/amd64

func main() {
	startTime := time.Now()
	log.Println(&quot;for loop started&quot;)
	a := make([]string, 0)
	a = append(a, &quot;N&quot;)
	x := 0
	for i := 0; i &lt; 47831; i++ {
		for j := 0; j &lt; 47831; j++ {
			a[0] = strings.ToLower(a[0])
			x++
		}
	}
	endTime := time.Now()
	log.Println(&quot;for loop end, time taken=&quot;, endTime.Sub(startTime), &quot;iters=&quot;, x)
}

2021/12/20 10:26:22 for loop started
2021/12/20 10:26:29 for loop end, time taken= 6.323187151s iters= 2287804561

答案2

得分: 0

根据@icza的评论和@Wolfgang的回答,你可以使用最新版本的Golang。

或者

如果你想要加快速度,可以将任务分成不同的goroutine。这样肯定会提高速度。

这些是导致你得到异常时间的三个因素。

在程序开始时:

  1. 等待队列中的进程数量,与操作系统无关。
  2. CPU调度
  3. 处理器的时钟速度
英文:

As commented by @icza and answered by @Wolfgang, You can use latest version of Golang.

OR

If you want to make it fast then divide the task in different goroutines. It will definitely enhance the time.

These are the three factor due to which you are getting abnormal time.

At that time when the program started :

  1. Number of process in the waiting queue irrespective of the OS.
  2. CPU scheduling
  3. Clock speed of processor

huangapple
  • 本文由 发表于 2021年12月20日 17:16:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/70419635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定