I am trying to understand how go-routines work. Here is some code:
//parallelSum.go
func sum(a []int, c chan<- int, func_id string) {
sum := 0
for _, n := range a {
sum += n
}
log.Printf("func_id %v is DONE!", func_id)
c <- sum
}
func main() {
ELEM_COUNT := 10000000
test_arr := make([]int, ELEM_COUNT)
for i := 0; i < ELEM_COUNT; i++ {
test_arr[i] = i * 2
}
c1 := make(chan int)
c2 := make(chan int)
go sum(test_arr[:len(test_arr)/2], c1, "1")
go sum(test_arr[len(test_arr)/2:], c2, "2")
x := <-c1
y := <-c2
//x, y := <-c, <-c
log.Printf("x= %v, y = %v, sum = %v", x, y, x+y)
}
The above program runs fine and returns the output. I have an iterative version of the same program:
//iterSum.go
func sumIter(a []int, c *int, func_id string) {
sum := 0
log.Printf("entered the func %s", func_id)
for _, n := range a {
sum += n
}
log.Printf("func_id %v is DONE!", func_id)
*c = sum
}
func main() {
*/
ELEM_COUNT := 10000000
test_arr := make([]int, ELEM_COUNT)
for i := 0; i < ELEM_COUNT; i++ {
test_arr[i] = i * 2
}
var (
i1 int
i2 int
)
sumIter(test_arr[:len(test_arr)/2], &i1, "1")
sumIter(test_arr[len(test_arr)/2:], &i2, "2")
x := i1
y := i2
log.Printf("x= %v, y = %v, sum = %v", x, y, x+y)
}
I ran the program 20 times and averaged the run time for each program. I see the average almost equal? Shouldn't parallelizing make things faster? What am I doing wrong?
Here is the python program to run it 20 times:
iterCmd = 'go run iterSum.go'
parallelCmd = 'go run parallelSum.go'
runCount = 20
def analyzeCmd(cmd, runCount):
runData = []
print("running cmd (%s) for (%s) times" % (cmd, runCount))
for i in range(runCount):
┆ start_time = time.time()
┆ cmd_out = subprocess.check_call(shlex.split(cmd))
run_time = time.time() - start_time
┆ curr_data = {'iteration': i, 'run_time' : run_time}
┆ runData.append(curr_data)
return runData
iterOut = analyzeCmd(iterCmd, runCount)
parallelOut = analyzeCmd(parallelCmd, runCount)
print("iter cmd data -->")
print(iterOut)
with open('iterResults.json', 'w') as f:
json.dump(iterOut, f)
print("parallel cmd data -->")
print(parallelOut)
with open('parallelResults.json', 'w') as f:
json.dump(parallelOut, f)
avg = lambda results: sum(i['run_time'] for i in results) / len(results)
print("average time for iterSum = %3.2f" % (avg(iterOut)))
print("average time for parallelSum = %3.2f" % (avg(parallelOut)))
Here is output of 1 run:
average time for iterSum = 0.27
average time for parallelSum = 0.29