-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1624c0
commit 2509fbb
Showing
35 changed files
with
16,585 additions
and
8,999 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
name: Sorting Algorithms | ||
headline: A benchmark to compare the performance of different sorting algorithms in Go. | ||
description: > | ||
This benchmark compares the performance of different sorting algorithms in Go. | ||
Each run creates a random int slice, containing 1000 pseudo-random elements. | ||
It will then sort the slice with the specified algorithm. | ||
It currently compares the performance of the following sorting algorithms: | ||
Built-In `sort` package, Bubble Sort, Insertion Sort, Selection Sort, Merge Sort, Quick Sort. | ||
tags: | ||
- demo | ||
|
||
contributors: | ||
- MarvinJWendt | ||
|
||
meta: | ||
- implementation: Builtin Sort | ||
description: > | ||
This benchmark uses the built-in `sort` package to sort the array. | ||
- implementation: Bubble Sort | ||
description: > | ||
This benchmark uses the Bubble Sort algorithm to sort the array. | ||
Bubble Sort works by repeatedly swapping the adjacent elements, if they are in the wrong order. | ||
- implementation: Insertion Sort | ||
description: > | ||
This benchmark uses the Insertion Sort algorithm to sort the array. | ||
Insertion Sort works by repeatedly inserting the next element into the sorted part of the array. | ||
- implementation: Selection Sort | ||
description: > | ||
This benchmark uses the Selection Sort algorithm to sort the array. | ||
Selection Sort works by repeatedly selecting the smallest element from the unsorted part of the array and putting it at the end of the sorted part of the array. | ||
- implementation: Merge Sort | ||
description: > | ||
This benchmark uses the Merge Sort algorithm to sort the array. | ||
Merge Sort works by repeatedly splitting the array into two halves, sorting them and then merging them back together. | ||
- implementation: Quick Sort | ||
description: > | ||
This benchmark uses the Quick Sort algorithm to sort the array. | ||
Quick Sort works by repeatedly selecting a pivot element, partitioning the array around the pivot and then sorting the two partitions. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package sorting_algos | ||
|
||
import ( | ||
"math/rand" | ||
"sort" | ||
"testing" | ||
) | ||
|
||
type BuiltinSort struct{} | ||
|
||
func (s *BuiltinSort) sort(data []int) { | ||
sort.Ints(data) | ||
} | ||
|
||
func BenchmarkBuiltinSort_sort(b *testing.B) { | ||
var s BuiltinSort | ||
for i := 0; i < b.N; i++ { | ||
data := rand.Perm(1000) | ||
s.sort(data) | ||
} | ||
} | ||
|
||
type BubbleSort struct{} | ||
|
||
func (s *BubbleSort) sort(data []int) { | ||
n := len(data) | ||
for i := 0; i < n; i++ { | ||
for j := 0; j < n-i-1; j++ { | ||
if data[j] > data[j+1] { | ||
data[j], data[j+1] = data[j+1], data[j] | ||
} | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkBubbleSort_sort(b *testing.B) { | ||
var s BubbleSort | ||
for i := 0; i < b.N; i++ { | ||
data := rand.Perm(1000) | ||
s.sort(data) | ||
} | ||
} | ||
|
||
type InsertionSort struct{} | ||
|
||
func (s *InsertionSort) sort(data []int) { | ||
for i := 1; i < len(data); i++ { | ||
key := data[i] | ||
j := i - 1 | ||
|
||
for j >= 0 && data[j] > key { | ||
data[j+1] = data[j] | ||
j-- | ||
} | ||
data[j+1] = key | ||
} | ||
} | ||
|
||
func BenchmarkInsertionSort_sort(b *testing.B) { | ||
var s InsertionSort | ||
for i := 0; i < b.N; i++ { | ||
data := rand.Perm(1000) | ||
s.sort(data) | ||
} | ||
} | ||
|
||
type SelectionSort struct{} | ||
|
||
func (s *SelectionSort) sort(data []int) { | ||
n := len(data) | ||
for i := 0; i < n; i++ { | ||
minIdx := i | ||
for j := i + 1; j < n; j++ { | ||
if data[j] < data[minIdx] { | ||
minIdx = j | ||
} | ||
} | ||
data[i], data[minIdx] = data[minIdx], data[i] | ||
} | ||
} | ||
|
||
func BenchmarkSelectionSort_sort(b *testing.B) { | ||
var s SelectionSort | ||
for i := 0; i < b.N; i++ { | ||
data := rand.Perm(1000) | ||
s.sort(data) | ||
} | ||
} | ||
|
||
type QuickSort struct{} | ||
|
||
func (s *QuickSort) sort(data []int) { | ||
if len(data) < 2 { | ||
return | ||
} | ||
|
||
left, right := 0, len(data)-1 | ||
pivotIndex := rand.Int() % len(data) | ||
data[pivotIndex], data[right] = data[right], data[pivotIndex] | ||
for i := range data { | ||
if data[i] < data[right] { | ||
data[i], data[left] = data[left], data[i] | ||
left++ | ||
} | ||
} | ||
data[left], data[right] = data[right], data[left] | ||
s.sort(data[:left]) | ||
s.sort(data[left+1:]) | ||
} | ||
|
||
func BenchmarkQuickSort_sort(b *testing.B) { | ||
var s QuickSort | ||
for i := 0; i < b.N; i++ { | ||
data := rand.Perm(1000) | ||
s.sort(data) | ||
} | ||
} | ||
|
||
type MergeSort struct{} | ||
|
||
func (s *MergeSort) sort(data []int) []int { | ||
if len(data) <= 1 { | ||
return data | ||
} | ||
|
||
// Divide the array in half | ||
middle := len(data) / 2 | ||
left := s.sort(data[:middle]) | ||
right := s.sort(data[middle:]) | ||
|
||
return s.merge(left, right) | ||
} | ||
|
||
func (s *MergeSort) merge(left, right []int) []int { | ||
var result []int | ||
leftIndex, rightIndex := 0, 0 | ||
|
||
for leftIndex < len(left) && rightIndex < len(right) { | ||
if left[leftIndex] < right[rightIndex] { | ||
result = append(result, left[leftIndex]) | ||
leftIndex++ | ||
} else { | ||
result = append(result, right[rightIndex]) | ||
rightIndex++ | ||
} | ||
} | ||
|
||
// Append any remaining elements | ||
result = append(result, left[leftIndex:]...) | ||
result = append(result, right[rightIndex:]...) | ||
|
||
return result | ||
} | ||
|
||
func BenchmarkMergeSort_sort(b *testing.B) { | ||
var s MergeSort | ||
for i := 0; i < b.N; i++ { | ||
data := rand.Perm(1000) | ||
s.sort(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Synchronization Methods | ||
headline: A benchmark to compare the performance of channel and mutex synchronization in Go. | ||
description: > | ||
This benchmark compares the performance of channel and mutex synchronization in Go. | ||
tags: | ||
- sync | ||
- channel | ||
- mutex | ||
- synchronization | ||
- concurrency | ||
|
||
contributors: | ||
- MarvinJWendt | ||
|
||
meta: | ||
- implementation: Channel | ||
description: > | ||
This benchmark uses a channel to synchronize the goroutines. | ||
The channel is used as a lock, so only one goroutine can access the counter at the same time. | ||
- implementation: Mutex | ||
description: > | ||
This benchmark uses a `sync.Mutex` to lock the counter, before accessing it. | ||
This makes sure, that only one goroutine can access the counter at the same time. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package sync_methods | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func BenchmarkChannel_run(b *testing.B) { | ||
var counter int | ||
channel := make(chan bool, 1) // Buffered channel with capacity of 1 | ||
|
||
b.ResetTimer() | ||
|
||
var wg sync.WaitGroup | ||
for i := 0; i < b.N; i++ { | ||
wg.Add(1) | ||
go func() { | ||
channel <- true // Acquire the "lock" | ||
counter++ | ||
<-channel // Release the "lock" | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
close(channel) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package sync_methods | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func BenchmarkMutex_run(b *testing.B) { | ||
var counter int | ||
var mutex sync.Mutex | ||
|
||
b.ResetTimer() | ||
|
||
var wg sync.WaitGroup | ||
for i := 0; i < b.N; i++ { | ||
wg.Add(1) | ||
go func() { | ||
mutex.Lock() | ||
counter++ | ||
mutex.Unlock() | ||
wg.Done() | ||
}() | ||
} | ||
wg.Wait() | ||
} |
Oops, something went wrong.