Alright, folks, buckle up! We're going to see how concurrency in Go can make your programs way faster. We'll start with some basic stuff and then level up with some concurrency magic. Let's roll!
Step 1: Single Non-Concurrent Program
First up, we have a plain and simple program that prints "Hello" 50 times. This one runs in a single thread, so it's pretty slow and boring.
Code Snippet (Single Non-Concurrent)
package main
import (
"fmt"
"time"
)
func says(s string) {
for i := 0; i < 50; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
start := time.Now()
says("Hello")
duration := time.Since(start)
fmt.Println("Duration:", duration)
}
Result
- Duration: Approximately 5.5 seconds.
Step 2: 50 Non-Concurrent Programs
Now, imagine running that boring program 50 times in a row. Yeah, it's going to take forever!
Code Snippet (50 Non-Concurrent)
package main
import (
"fmt"
"time"
)
func says(s string) {
for i := 0; i < 50; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
start := time.Now()
for i := 0; i < 50; i++ {
says("Hello")
}
duration := time.Since(start)
fmt.Println("Duration:", duration)
}
Result
- Duration: Approximately 5.5 seconds * 50 = 275 seconds. Ouch!
Step 3: Single Concurrent Program
Alright, now let's add some flair! We'll run a single instance of our program using concurrency. Check it out!
Code Snippet (Single Concurrent)
package main
import (
"fmt"
"sync"
"time"
)
func says(wg *sync.WaitGroup, s string) {
defer wg.Done()
for i := 0; i < 50; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
var wg sync.WaitGroup
wg.Add(1)
start := time.Now()
go says(&wg, "Hello")
wg.Wait()
duration := time.Since(start)
fmt.Println("Duration:", duration)
}
Result
- Duration: Approximately 5.5 seconds.
Step 4: 50 Concurrent Programs Without go
Keyword
Alright, so what happens if we run 50 concurrent-like programs without actually using concurrency? Let's find out! I’m feeling super confident this will show some improvement, although not as much as using go
. This should be awesome! Fingers crossed! 🤞
Code Snippet (50 "Concurrent" Without go
)
package main
import (
"fmt"
"sync"
"time"
)
func says(wg *sync.WaitGroup, s string) {
defer wg.Done()
for i := 0; i < 50; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
var wg sync.WaitGroup
wg.Add(50)
start := time.Now()
for i := 0; i < 50; i++ {
says(&wg, "Hello")
}
wg.Wait()
duration := time.Since(start)
fmt.Println("Duration:", duration)
}
Result
- Duration: Approximately 5.5 seconds * 50 = 275 seconds. Still slow!
Alright, picture this: I was like, "Yeah, this is going to be awesome! We'll see some speed-up for sure!" But then... reality hit me like a ton of bricks. 275 seconds?! What on earth just happened? 😱
It's like expecting to ride a super-fast roller coaster but ending up on a snail ride instead. I mean, I was so confident, thinking, "This is it, we're going to see some serious improvement!" But no, it was embarrassingly slow, and I felt like I just discovered gravity for the first time. Seriously, what was I thinking?
So, there I was, facepalming and feeling a mix of shame and anger. But hey, lesson learned! Sometimes, you've got to fail spectacularly to understand why concurrency is such a game-changer. Alright, let's get to the real deal!
Step 5: 50 Concurrent Programs with go
Keyword
Now for the grand finale! Let's run 50 programs concurrently using the go
keyword. This is where the magic happens! 🌟
Code Snippet (50 Concurrent with go
)
package main
import (
"fmt"
"sync"
"time"
)
func says(wg *sync.WaitGroup, s string) {
defer wg.Done()
for i := 0; i < 50; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
var wg sync.WaitGroup
wg.Add(50)
start := time.Now()
for i := 0; i < 50; i++ {
go says(&wg, "Hello")
}
wg.Wait()
duration := time.Since(start)
fmt.Println("Duration:", duration)
}
Result
- Duration: Approximately 5.5 seconds. Boom! 🎉
Conclusion
See the difference? When we ran 50 non-concurrent programs, it took forever. But with concurrency, we did it in the same time as a single run. Concurrency in Go is like having 50 clones of yourself doing the work at the same time. It's faster, cooler, and a lot more fun!
So, get out there and start using concurrency in your Go programs. It's a game-changer! Happy coding, and stay awesome! 😎
Top comments (0)