In this lab, we will speed up our program by splitting up the work into multiple goroutines so that we can take advantage of multiple processor cores.
-
go build
your current program and then rename the resulting binary toipsum-single-threaded
-
In the
generateIpsum
function, after initializing theWordBank
, make a channel of strings. -
Inside the for loop, create an anonymous function with no input that calls
generateSentence
and pushes the result into the channel.
Note: functional buffs will recognize this as a closure -
Run this function in a goroutine by putting the
go
keyword directly before the function definition and()
right after the function's closing curly brace.// for example go func(){ doStuff() }()
Note: Javascript buffs will recognize this (minus the go keyword) as an Immediately Invoked Function Expression or IIFE
-
After this for loop, create another for loop that reads each sentence from the channel and builds them into the paragraph to be returned.
-
go build
this version of the executable and compare the execution times.time ./ipsum-single-threaded time ./ipsum
*Note: see this stack overflow for equivalent non-unix commands.