Difference when executing a piece of code in main versus calling it as a function in golang

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

Difference when executing a piece of code in main versus calling it as a function in golang

问题

我正在尝试在GO中执行一个CPU密集型操作。代码非常简单,我也在尝试进行测量。测量代码如下所示:

然而,当我尝试将相同的代码作为一个函数执行(从主函数调用)时,它似乎运行得更快。有什么想法为什么会这样?

package main

import (
	"fmt"
	"time"
)

func addlist1(from uint64, to uint64) {
	var i uint64
	var l uint64
	var j uint64
	var sum1 uint64
	var count uint64

	count = 0
	for i = 1; i < 1001; i++ {
		for l = from; l < (from + to); l++ {
			j = l * l
			count = count + 1
			sum1 = sum1 + (j * j * j * j * j * j * j * j * j * j)
		}
	}
}

func main() {
	var sum1 uint64
	var j uint64
	var i uint64
	var l uint64
	var num uint64
	var count uint64

	sum1 = 0
	num = 100000000
	start88 := time.Now()
	addlist1(1, num)
	elapsed88 := time.Since(start88)
	fmt.Println("The time taken is ", elapsed88, sum1, num)
	time.Sleep(1 * time.Second)

	sum1 = 0
	count = 0
	start := time.Now()
	for i = 1; i < 1001; i++ {
		for l = 1; l < num; l++ {
			j = l * l
			sum1 = sum1 + (j * j * j * j * j * j * j * j * j * j)
			count = count + 1
		}
	}
	elapsed := time.Since(start)
	fmt.Println("The total time without function is", elapsed, sum1, num, count)
	time.Sleep(1 * time.Second)
}

第一个println输出29秒,而第二个输出4分57秒。差距很大,而且是一致的。

我在一个具有4个核心的Debian发行版上运行这个代码。

有什么想法为什么会这样?

诚挚地,
Sudarsan.D

英文:

I am trying to do a CPU-intensive operation in GO. The code is pretty straight forward and I am trying to measure it as well. The measurement code looks like this

However, when I try to execute the same code as a function (invoked from main) it seems to work much faster. Any idea as to why this is happening?
package main

import (
&quot;fmt&quot;;
&quot;time&quot;
)
func addlist1 (from uint64, to uint64) {
var i uint64;   
var l uint64;   
var j uint64;   
var sum1 uint64;
var  count uint64;
count = 0;
for i=1;i&lt;1001;i++ {
for l=from;l&lt;(from+to);l++ {
j = l*l;
count = count + 1;
sum1 = sum1 + (j*j*j*j*j*j*j*j*j*j);
}
}
}
func main () {
var sum1 uint64;
var j uint64;
var i uint64;
var l uint64;
var num uint64;
var count  uint64;
sum1 = 0;
num = 100000000;
start88 := time.Now ();
addlist1(1,num);
elapsed88 := time.Since(start88);
fmt.Println(&quot;The time taken is &quot;, elapsed88,sum1,num);
time.Sleep(1*time.Second);
sum1 = 0;
count  = 0;
start := time.Now();
for i=1;i&lt;1001;i++ {
for l=1;l&lt;num;l++ {
j = l*l;
sum1 = sum1 + (j*j*j*j*j*j*j*j*j*j);
count = count+1;
}
}
elapsed := time.Since(start);
fmt.Println(&quot;The total time without function is&quot;, elapsed,sum1,num,count);
time.Sleep(1*time.Second);
}

The first println gives an output of 29 seconds whereas the second one gives an output of 4m 57seconds. The difference is huge. It is consistent as well.

I am running this in a Debian distribution that has 4 cores.

Any ideas why this is so?

Sincerely,
Sudarsan.D

答案1

得分: 2

mainaddlist1函数内的代码是不同的

addlist1函数中,没有返回任何值。编译器可以完全优化它,因为该函数什么也不做(不修改内存或返回值)。

main()函数中的代码不同,它打印了sum1的值。

为了使代码相同,从你的函数中返回sum1count,并在main函数中打印它们:

func addlist1(from uint64, to uint64) (uint64, uint64) {
    ...
    return sum1, count
}

func main() {
    ...
    start := time.Now()
    sum, count := addlist1(1, num)
    elapsed := time.Since(start)
    fmt.Println("所花费的时间是", elapsed, sum1, num)
    ...
}
英文:

The code inside main and addlist1 is not the same.

In the function addlist1, no values are returned. The compiler can completely optimize it away into nothing, because the function does nothing (it does not modify memory or return a value).

This is not true of the code in main(), which prints the value of sum1.

To make the code the same, return sum1 and count from your function and print it from main:

func addlist1(from uint64, to uint64) (uint64, uint64) {
...
return sum1, count
}
func main() {
...
start := time.Now()
sum, count := addlist1(1, num)
elapsed := time.Since(start)
fmt.Println(&quot;The time taken is &quot;, elapsed, sum1, num);
...
}

huangapple
  • 本文由 发表于 2022年2月7日 03:02:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/71010653.html
匿名

发表评论

匿名网友

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

确定