Skip to content

Commit

Permalink
Add content
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Jun 19, 2023
1 parent dc9787f commit 423d78a
Show file tree
Hide file tree
Showing 11 changed files with 692 additions and 74 deletions.
55 changes: 55 additions & 0 deletions Benchmarks/ArrayBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using BenchmarkDotNet.Attributes;
using System.Collections;
using System.Collections.Generic;

public class ArrayBenchmarks
{
int[]? array;

[Params(0, 1, 10, 1_000)]
public int Count { get; set; }

[GlobalSetup]
public void GlobalSetup()
{
array = Enumerable.Range(0, Count).ToArray();
}

[Benchmark(Baseline = true)]
public int ArrayFor()
{
var sum = 0;
for (var index = 0; index < Count; index++)
sum += array![index];
return sum;
}

[Benchmark]
public int ArrayForeach()
{
var sum = 0;
foreach (var item in array!)
sum += item;
return sum;
}

[Benchmark]
public int SpanFor()
{
var span = array!.AsSpan();
var sum = 0;
for (var index = 0; index < Count; index++)
sum += span[index];
return sum;
}

[Benchmark]
public int SpanForeach()
{
var span = array!.AsSpan();
var sum = 0;
foreach (var item in span)
sum += item;
return sum;
}
}
1 change: 0 additions & 1 deletion Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
var config = DefaultConfig.Instance
.WithSummaryStyle(SummaryStyle.Default.WithRatioStyle(RatioStyle.Trend))
.AddDiagnoser(MemoryDiagnoser.Default)
.AddExporter(MarkdownExporter.Default)
.AddExporter(MarkdownExporter.GitHub);

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
Binary file added slidev/images/qrcode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions slidev/pages/ArrayMemorySpan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
layout: section
---

# Array, Memory & Span

---

# Array, Memory & Span

- Pull
- Random
- Special treatment by the JIT compiler

---

# `for` loop

```csharp
static int Sum(int[] source)
{
var sum = 0;
for (var index = 0; index < source.Length; index++)
sum += source[index];
return sum;
}
```

[SharpLab](https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA+ABATABgLABQ2AjIRjgAQbEB0AMgJYB2AjgNyGEBuAhlBXyg8AnhQC8FAKJMArgFsYQ4ABsYNAEo8mAcxgAKHABoKxHAEoaAFQgBBKEOF6zHIsQCcegMry9gkWedOVyQKZgAXCm85PXCAbQBdCgBnCBkoMBgzQgBvQgp8il5+JPlxChwXAooAM2gKPSLQpgATGAQyiqbW9oAeZNT0tToYHTCACzYutoBqaayCKqqSuQppiRS0jNjmbvjKgowAdmT5FwBfAqA===)

---

# `foreach` loop

```csharp
static int Sum(int[] source)
{
var sum = 0;
foreach (var item in source)
sum += item;
return sum;
}
```

---

* Benchmarks

| Method | Count | Mean | Error | StdDev | Ratio | RatioSD | Allocated | Alloc Ratio |
|------------- |------ |------------:|----------:|----------:|-------------:|--------:|----------:|------------:|
| **ArrayFor** | **10** | **8.0370 ns** | **0.0310 ns** | **0.0290 ns** | **baseline** | **** | **-** | **NA** |
| ArrayForeach | 10 | 3.9263 ns | 0.0192 ns | 0.0180 ns | 2.05x faster | 0.01x | - | NA |
| SpanFor | 10 | 9.6291 ns | 0.0412 ns | 0.0386 ns | 1.20x slower | 0.01x | - | NA |
| SpanForeach | 10 | 4.6778 ns | 0.0797 ns | 0.0746 ns | 1.72x faster | 0.03x | - | NA |
| | | | | | | | | |
| **ArrayFor** | **1000** | **401.9290 ns** | **1.4809 ns** | **1.3852 ns** | **baseline** | **** | **-** | **NA** |
| ArrayForeach | 1000 | 323.1575 ns | 0.8017 ns | 0.7499 ns | 1.24x faster | 0.01x | - | NA |
| SpanFor | 1000 | 326.9202 ns | 1.3916 ns | 1.3017 ns | 1.23x faster | 0.01x | - | NA |
| SpanForeach | 1000 | 323.4267 ns | 1.5112 ns | 1.4136 ns | 1.24x faster | 0.01x | - | NA |

---

# `foreach` compiled

```csharp
```

32 changes: 18 additions & 14 deletions slidev/pages/ConsumingAnEnumerable.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,7 @@ public static int Sum(IEnumerable<int> enumerable)
}
```

---

# `await foreach`

```csharp {all|4}
public static async ValueTask<int> SumAsync(IAsyncEnumerable<int> enumerable, CancellationToken token = default)
{
var sum = 0;
await foreach(var number in enumerable.WithCancellation(token))
sum += number;
return sum;
}
```
[SharpLab](https://sharplab.io/#v2:EYLgxg9gTgpgtADwGwBYA+ABATABgLABQ2AjIRjgAQbEoDchhAbgIZQUDOEArlGDBQF4KAOxgB3ANoBdCgG8sAGgpYAvvSLEAnAAoAylwC22zjz4BKM+rLEkFAJbCALhX1HqAZgA8DxwD4KMMKGMFDMwAA2MGYUhLKEFBQsbOyGghQ46gkAZtAwzGAAFhTaSSKGwCH2wgFBBiFhkWbxCRypANRCtRVQmVQA7K0G6ipAA)

---

Expand Down Expand Up @@ -59,7 +47,23 @@ Does not check for `null`. `Enumerable&lt;T&gt;` should be used when there's not

---

# compiled `await foreach`
# async `foreach`

```csharp {all|4}
public static async ValueTask<int> SumAsync(IAsyncEnumerable<int> enumerable, CancellationToken token = default)
{
var sum = 0;
await foreach(var number in enumerable.WithCancellation(token))
sum += number;
return sum;
}
```

[SharpLab]()

---

# compiled async `foreach`

```csharp {all|4|6|7}
public static async ValueTask<int> SumAsync(IAsyncEnumerable<int> enumerable, CancellationToken token = default)
Expand Down
21 changes: 0 additions & 21 deletions slidev/pages/Linq.md

This file was deleted.

Loading

0 comments on commit 423d78a

Please sign in to comment.