英文:
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 (
"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);
}
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
main
和addlist1
函数内的代码是不同的。
在addlist1
函数中,没有返回任何值。编译器可以完全优化它,因为该函数什么也不做(不修改内存或返回值)。
而main()
函数中的代码不同,它打印了sum1
的值。
为了使代码相同,从你的函数中返回sum1
和count
,并在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("The time taken is ", elapsed, sum1, num);
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论