Skip to content

callowaysutton/MultithreadedFizzBuzz

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Multithreaded FizzBuzz

A multithreaded FizzBuzz implementation

Uses waitgroups (https://gobyexample.com/waitgroups) to parallelize FizzBuzz

It is around 4x faster than this one I made in 2 minutes:

package main

import (
	"fmt"
	"os"
	"strconv"
)

func main() {
	input, err := strconv.Atoi(os.Args[1])
	if err != nil {
		panic(err)
	}

	for i := 1; i <= input; i++ {
		switch true {
		case i%3 == 0:
			fmt.Print("Fizz")
			break
		case i%5 == 0:
			fmt.Print("Buzz")
			break
		case i%15 == 0:
			fmt.Print("FizzBuzz")
			break
		default:
			fmt.Printf("%d", i)
			break
		}
		fmt.Print("\n")
	}
}

Which makes sense considering it's using 4 goroutines :P

Multithreaded:

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 1
Milliseconds      : 845
Ticks             : 18456926
TotalDays         : 2.13621828703704E-05
TotalHours        : 0.000512692388888889
TotalMinutes      : 0.0307615433333333
TotalSeconds      : 1.8456926
TotalMilliseconds : 1845.6926

Non-Multithreaded:

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 8
Milliseconds      : 8
Ticks             : 80085322
TotalDays         : 9.26913449074074E-05
TotalHours        : 0.00222459227777778
TotalMinutes      : 0.133475536666667
TotalSeconds      : 8.0085322
TotalMilliseconds : 8008.5322

My implementation is a lot slower or faster depending on the archetecture it is being run on (https://stackoverflow.com/questions/3652056/how-efficient-is-locking-an-unlocked-mutex-what-is-the-cost-of-a-mutex)

About

A multithreaded FizzBuzz implementation

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages