我已經得到了這段代碼,并被要求找出如何使用并發來加速這個程序。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#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 < SIZE; i )
{
a[i] = rand();
b[i] = rand();
}
gettimeofday(&time1, 0); //Original place
sum = 0.0;
for (i = 0; i < SIZE; i )
{
sum = sum a[i]*b[i];
}
gettimeofday(&time2, 0);
printf("Elapsed time (us) = %d\n", (time2.tv_sec-time1.tv_sec)*1000000 time2.tv_usec - time1.tv_usec);
return 0;
}
如果我運行代碼,我會得到輸出
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
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 (
"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)
}
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?
uj5u.com熱心網友回復:
并發加快了執行時間。
去程式:
經過的時間(我們)= 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("Elapsed time (us) = %d\n", since)
}
.
$ go build x.go && ./x
Elapsed time (us) = 130768
$
運行 C 程式的并發 Go 版本。
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("Elapsed time (us) = %d\n", since)
}
.
$ go build y.go && ./y
Elapsed time (us) = 66947
$
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/393706.html
標籤:c go concurrency
上一篇:C#中的@相當于Go中的變數嗎?
下一篇:在常量宣告中使用純函式
