I have gotten this code and been asked to find out how I can use concurrency to speed up the process.
#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;
}
if I run the code I get the output
Elapsed time (us) = 26546
Then I wrote a similar program in 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())
}
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 (
"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())
}
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 (
"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?
CodePudding user response:
Concurrency speeds up execution time of a Go program.
To summarize your examples (optimized):
C program:
Elapsed time (us) = 181474
Go program:
Elapsed time (us) = 132481
Go program with concurrency:
Elapsed time (us) = 74139
As a control, run the C program with O2 optimization.
#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;
gettimeofday(&time1, 0);
srand(time(0));
for (i = 0; i < SIZE; i )
{
a[i] = rand();
b[i] = rand();
}
sum = 0.0;
for (i = 0; i < SIZE; i )
{
sum = sum a[i]*b[i];
}
gettimeofday(&time2, 0);
printf("Elapsed time (us) = %ld\n", (time2.tv_sec-time1.tv_sec)*1000000 time2.tv_usec - time1.tv_usec);
return 0;
}
.
$ gcc xc.c -o xc -O2 && ./xc
Elapsed time (us) = 181474
$
Run the Go version of the C program.
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
)
time1 = 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 = sum a[i]*b[i]
}
time2 = time.Now()
fmt.Printf("Elapsed time (us) = %d\n", time2.Sub(time1).Microseconds())
}
.
$ go build xgo.go && ./xgo
Elapsed time (us) = 132481
$
Run a concurrent version of the Go program.
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
const size int = 10000000
var (
a = [size]float32{}
b = [size]float32{}
)
func main() {
var (
wg sync.WaitGroup
sum float64
time1 time.Time
time2 time.Time
)
time1 = time.Now()
r1 := rand.New(rand.NewSource(time.Now().UnixNano()))
r2 := rand.New(rand.NewSource(time.Now().UnixNano()))
wg.Add(2)
go func() {
for i := 0; i < size; i {
a[i] = r1.Float32()
}
wg.Done()
}()
go func() {
for i := 0; i < size; i {
b[i] = r2.Float32()
}
wg.Done()
}()
wg.Wait()
sum = 0.0
for i := 0; i < size; i {
sum = sum float64(a[i]*b[i])
}
time2 = time.Now()
fmt.Printf("Elapsed time (us) = %d\n", time2.Sub(time1).Microseconds())
}
.
$ go build ygo.go && ./ygo
Elapsed time (us) = 74139
$