并发对象的创建会显著降低执行时间。

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

Creation of concurrency objects dramatically slows down execution time

问题

我已经得到这段代码,并被要求找出如何使用并发来加速处理过程。

如果我运行这段代码,我会得到以下输出:

Elapsed time (us) = 26546

然后我用Go语言编写了一个类似的程序:

package main

import (
	"fmt"
	"math/rand"
	"time"
)

const size int64 = 10000000

var (
	a = [size]float32{}
	b = [size]float32{}
)

func main() {
	var (
		i     int64
		sum   float32
		time1 time.Time
		time2 time.Time
	)

	rand.Seed(time.Now().UnixNano())

	for i = 0; i < size; i++ {
		a[i] = rand.Float32()
		b[i] = rand.Float32()
	}

	time1 = time.Now() //Original place

	sum = 0.0

	for i = 0; i < size; i++ {
		sum = sum + a[i] + b[i]
	}

	time2 = time.Now()

	fmt.Printf("Elapsed time (us) = %d\n", time2.Sub(time1).Microseconds())
}

我得到了这个输出(非常令人惊讶地比C版本更快):

Elapsed time (us) = 2462

我的任务是尝试使用并发使其更快,我想到可以通过并行运行数组的创建来加速它们,但是计时器只在创建之后启动。所以我不知道如何加速它,因为值需要合并,这将是一个顺序过程。

所以我将启动计时器移到创建时间之前,得到C程序的结果如下:

Elapsed time (us) = 172496

Go程序的结果如下:

Elapsed time (us) = 247603

所以现在Go比C慢,这是预期的。

然后我尝试将Go程序更改为在每个goroutine中创建每个数组:

package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

const size int = 10000000

var (
	a = [size]float64{}
	b = [size]float64{}
)

func main() {
	var (
		wg    sync.WaitGroup
		sum   float64
		time1 time.Time
		time2 time.Time
	)

	rand.Seed(time.Now().UnixNano())

	wg.Add(2)

	time1 = time.Now()

	go func() {
		for i := 0; i < size; i++ {
			a[i] = rand.Float64()
		}
		wg.Done()
	}()

	go func() {
		for i := 0; i < size; i++ {
			b[i] = rand.Float64()
		}
		wg.Done()
	}()

	wg.Wait()

	sum = 0.0

	for i := 0; i < size; i++ {
		sum = sum + a[i] + b[i]
	}

	time2 = time.Now()

	fmt.Printf("Elapsed time (us) = %d\n", time2.Sub(time1).Microseconds())
}

我得到了以下输出:

Elapsed time (us) = 395808

这相当慢,我认为这与函数的调用和等待组逻辑有关。

然后我尝试使用通道。

这只会使程序运行很长时间,并且代码非常冗长。

然后我尝试让每个goroutine自己添加字段:

package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

const size int = 10000000

func main() {
	var (
		wg    sync.WaitGroup
		sum   float64
		asum  float64
		bsum  float64
		time1 time.Time
		time2 time.Time
	)

	rand.Seed(time.Now().UnixNano())

	wg.Add(2)

	time1 = time.Now()

	go func() {
		asum = 0
		for i := 0; i < size; i++ {
			asum = asum + rand.Float64()
		}

		wg.Done()
	}()

	go func() {
		bsum = 0
		for i := 0; i < size; i++ {
			bsum = bsum + rand.Float64()
		}
		wg.Done()
	}()

	wg.Wait()

	sum = asum + bsum

	time2 = time.Now()

	fmt.Printf("Elapsed time (us) = %d\n", time2.Sub(time1).Microseconds())
	fmt.Println(sum)
}

它返回了以下结果:

Elapsed time (us) = 395182
1.000137482475232e+07

我必须使用sum变量才能运行程序-这就是为什么我打印它。

所以我似乎无法通过并发使这个程序运行得更快。

有人对此有什么提示吗?或者我应该在并发产生效果之前运行更多的任务?这只是因为我在这种情况下只处理了2个任务,并且数组处理速度非常快吗?

英文:

I have gotten this code and been asked to find out how I can use concurrency to speed up the process.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;
#include &lt;sys/time.h&gt;

#define SIZE 10000000

volatile float a[SIZE];
volatile float b[SIZE];

int main(int argc, char **argv)
{
  long int       i;
  double         sum;
  struct timeval time1, time2;

  srand(time(0));

  for (i = 0; i &lt; SIZE; i++)
  {
    a[i] = rand();
    b[i] = rand();
  }

  gettimeofday(&amp;time1, 0); //Original place

  sum = 0.0;
  
  for (i = 0; i &lt; SIZE; i++)
  {
    sum = sum + a[i]*b[i];
  }

  gettimeofday(&amp;time2, 0);
  
  printf(&quot;Elapsed time (us) = %d\n&quot;, (time2.tv_sec-time1.tv_sec)*1000000 + time2.tv_usec - time1.tv_usec);

  return 0;
}                

if I run the code I get the output

Elapsed time (us) = 26546

Then I wrote a similar program in Go

package main

import (
	&quot;fmt&quot;
	&quot;math/rand&quot;
	&quot;time&quot;
)

const size int64 = 10000000

var (
	a = [size]float32{}
	b = [size]float32{}
)

func main() {
	var (
		i     int64
		sum   float32
		time1 time.Time
		time2 time.Time
	)

	rand.Seed(time.Now().UnixNano())

	for i = 0; i &lt; size; i++ {
		a[i] = rand.Float32()
		b[i] = rand.Float32()
	}

	time1 = time.Now() //Original place

	sum = 0.0

	for i = 0; i &lt; size; i++ {
		sum = sum + a[i] + b[i]
	}

	time2 = time.Now()

	fmt.Printf(&quot;Elapsed time (us) = %d\n&quot;, time2.Sub(time1).Microseconds())
}

An I get this output (which was very surprisingly faster than the C version)

Elapsed time (us) = 2462

My job was to try to make it faster with concurrency, and I was thinking that the creation of the arrays could be speed up if they would be run in parallel, However the timer is only started after the creation. So then I don't really know how I can speed it up since the values need to be merges which would be a sequential process.

So I move the start timer over the creation time and get for the c program:

Elapsed time (us) = 172496

and for the go program:

Elapsed time (us) = 247603

So now go is slower than C as expected.

Then I tried to change my go program to create each array in its own goroutine:

package main

import (
	&quot;fmt&quot;
	&quot;math/rand&quot;
	&quot;sync&quot;
	&quot;time&quot;
)

const size int = 10000000

var (
	a = [size]float64{}
	b = [size]float64{}
)

func main() {
	var (
		wg    sync.WaitGroup
		sum   float64
		time1 time.Time
		time2 time.Time
	)

	rand.Seed(time.Now().UnixNano())

	wg.Add(2)

	time1 = time.Now()

	go func() {
		for i := 0; i &lt; size; i++ {
			a[i] = rand.Float64()
		}
		wg.Done()
	}()

	go func() {
		for i := 0; i &lt; size; i++ {
			b[i] = rand.Float64()
		}
		wg.Done()
	}()

	wg.Wait()

	sum = 0.0

	for i := 0; i &lt; size; i++ {
		sum = sum + a[i] + b[i]
	}

	time2 = time.Now()

	fmt.Printf(&quot;Elapsed time (us) = %d\n&quot;, time2.Sub(time1).Microseconds())
}

and I get the output:

Elapsed time (us) = 395808

Which is quite slow. and I expect that this has something to do with the invokation of the functions and the waitgroup logic.

Then I tried with channels.

Which just made the program take forever, and the code waay to long.

Then I tried with each coroutine adding the fields itself

package main

import (
	&quot;fmt&quot;
	&quot;math/rand&quot;
	&quot;sync&quot;
	&quot;time&quot;
)

const size int = 10000000

func main() {
	var (
		wg    sync.WaitGroup
		sum   float64
		asum  float64
		bsum  float64
		time1 time.Time
		time2 time.Time
	)

	rand.Seed(time.Now().UnixNano())

	wg.Add(2)

	time1 = time.Now()

	go func() {
		asum = 0
		for i := 0; i &lt; size; i++ {
			asum = asum + rand.Float64()
		}

		wg.Done()
	}()

	go func() {
		bsum = 0
		for i := 0; i &lt; size; i++ {
			bsum = bsum + rand.Float64()
		}
		wg.Done()
	}()

	wg.Wait()

	sum = asum + bsum

	time2 = time.Now()

	fmt.Printf(&quot;Elapsed time (us) = %d\n&quot;, time2.Sub(time1).Microseconds())
	fmt.Println(sum)
}

which returned

Elapsed time (us) = 395182
1.000137482475232e+07

I had to use the sum as well to be able to run the program - thats why I print it.


So I just cant seem to get this program to run any faster with concurrency.

Does anyone have a hint for me? or should I just run more jobs before concurrency will have any effect? Is it just because I only deal with 2 jobs in this case, and because arrays are so fast to process?

答案1

得分: 2

并发可以加快执行时间。

Go程序:

经过的时间(微秒)= 130768

带有并发的Go程序:

经过的时间(微秒)= 66947

为了使每个goroutine都拥有自己的rand.Rand实例,请使用rand.New(src Source)。


运行C程序的Go版本。

x.go

package main
import (
"fmt"
"math/rand"
"time"
)
const size = 10000000
var (
a = [size]float32{}
b = [size]float32{}
)
func main() {
start := time.Now()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < size; i++ {
a[i] = r.Float32()
b[i] = r.Float32()
}
sum := 0.0
for i := 0; i < size; i++ {
sum += float64(a[i]) * float64(b[i])
}
since := time.Since(start).Microseconds()
fmt.Printf("经过的时间(微秒)= %d\n", since)
}

.

$ go build x.go && ./x
经过的时间(微秒)= 130768
$ 

运行并发的Go版本的C程序。

y.go

package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
const size = 10000000
var (
a = [size]float32{}
b = [size]float32{}
)
func main() {
start := time.Now()
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < size; i++ {
a[i] = r.Float32()
}
}()
go func() {
defer wg.Done()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < size; i++ {
b[i] = r.Float32()
}
}()
wg.Wait()
sum := 0.0
for i := 0; i < size; i++ {
sum += float64(a[i]) * float64(b[i])
}
since := time.Since(start).Microseconds()
fmt.Printf("经过的时间(微秒)= %d\n", since)
}

.

$ go build y.go && ./y
经过的时间(微秒)= 66947
$
英文:

Concurrency speeds up execution time.

Go program:

Elapsed time (us) = 130768

Go program with concurrency:

Elapsed time (us) = 66947

For each goroutine to have its own rand.Rand instance, use rand.New(src Source).


Run a Go version of the C program.

x.go:

package main
import (
&quot;fmt&quot;
&quot;math/rand&quot;
&quot;time&quot;
)
const size = 10000000
var (
a = [size]float32{}
b = [size]float32{}
)
func main() {
start := time.Now()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i &lt; size; i++ {
a[i] = r.Float32()
b[i] = r.Float32()
}
sum := 0.0
for i := 0; i &lt; size; i++ {
sum += float64(a[i]) * float64(b[i])
}
since := time.Since(start).Microseconds()
fmt.Printf(&quot;Elapsed time (us) = %d\n&quot;, since)
}

.

$ go build x.go &amp;&amp; ./x
Elapsed time (us) = 130768
$ 

Run a concurrent Go version of the C program.

y.go:

package main
import (
&quot;fmt&quot;
&quot;math/rand&quot;
&quot;sync&quot;
&quot;time&quot;
)
const size = 10000000
var (
a = [size]float32{}
b = [size]float32{}
)
func main() {
start := time.Now()
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i &lt; size; i++ {
a[i] = r.Float32()
}
}()
go func() {
defer wg.Done()
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i &lt; size; i++ {
b[i] = r.Float32()
}
}()
wg.Wait()
sum := 0.0
for i := 0; i &lt; size; i++ {
sum += float64(a[i]) * float64(b[i])
}
since := time.Since(start).Microseconds()
fmt.Printf(&quot;Elapsed time (us) = %d\n&quot;, since)
}

.

$ go build y.go &amp;&amp; ./y
Elapsed time (us) = 66947
$ 

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

发表评论

匿名网友

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

确定