Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinJWendt committed Dec 19, 2023
1 parent a1624c0 commit 2509fbb
Show file tree
Hide file tree
Showing 35 changed files with 16,585 additions and 8,999 deletions.
21,348 changes: 13,595 additions & 7,753 deletions benchmarks.json

Large diffs are not rendered by default.

358 changes: 179 additions & 179 deletions benchmarks/array-vs-slice/output.bench

Large diffs are not rendered by default.

256 changes: 128 additions & 128 deletions benchmarks/concurrent-map-access/output.bench

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions benchmarks/sorting-algos/_meta.yaml
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.
420 changes: 420 additions & 0 deletions benchmarks/sorting-algos/output.bench

Large diffs are not rendered by default.

161 changes: 161 additions & 0 deletions benchmarks/sorting-algos/sort_test.go
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)
}
}
25 changes: 25 additions & 0 deletions benchmarks/sync-methods/_meta.yaml
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.
26 changes: 26 additions & 0 deletions benchmarks/sync-methods/channel_test.go
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)
}
25 changes: 25 additions & 0 deletions benchmarks/sync-methods/mutex_test.go
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()
}
Loading

0 comments on commit 2509fbb

Please sign in to comment.