-
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
Showing
11 changed files
with
692 additions
and
74 deletions.
There are no files selected for viewing
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,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; | ||
} | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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 | ||
``` | ||
|
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
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.