Minor improvements in docs, part 2
This commit is contained in:
Родитель
74d79336b4
Коммит
764bd31b4e
|
@ -11,10 +11,17 @@ PM> Install-Package BenchmarkDotNet
|
||||||
|
|
||||||
|
|
||||||
## Step 2. Design a benchmark
|
## Step 2. Design a benchmark
|
||||||
Write a class with methods that you want to measure and mark them with the `Benchmark` attribute. In the following example, we
|
Create a new console application, write a class with methods that you want to measure and mark them with the `Benchmark` attribute. In the following example, we
|
||||||
compare [MD5](https://en.wikipedia.org/wiki/MD5) and [SHA256](https://en.wikipedia.org/wiki/SHA-2) cryptographic hash functions:
|
compare [MD5](https://en.wikipedia.org/wiki/MD5) and [SHA256](https://en.wikipedia.org/wiki/SHA-2) cryptographic hash functions:
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
|
using System;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
using BenchmarkDotNet.Running;
|
||||||
|
|
||||||
|
namespace MyBenchmarks
|
||||||
|
{
|
||||||
public class Md5VsSha256
|
public class Md5VsSha256
|
||||||
{
|
{
|
||||||
private const int N = 10000;
|
private const int N = 10000;
|
||||||
|
@ -30,16 +37,10 @@ public class Md5VsSha256
|
||||||
}
|
}
|
||||||
|
|
||||||
[Benchmark]
|
[Benchmark]
|
||||||
public byte[] Sha256()
|
public byte[] Sha256() => sha256.ComputeHash(data);
|
||||||
{
|
|
||||||
return sha256.ComputeHash(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Benchmark]
|
[Benchmark]
|
||||||
public byte[] Md5()
|
public byte[] Md5() => md5.ComputeHash(data);
|
||||||
{
|
|
||||||
return md5.ComputeHash(data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Program
|
public class Program
|
||||||
|
@ -49,6 +50,7 @@ public class Program
|
||||||
var summary = BenchmarkRunner.Run<Md5VsSha256>();
|
var summary = BenchmarkRunner.Run<Md5VsSha256>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The `BenchmarkRunner.Run<Md5VsSha256>()` call runs your benchmarks and print results to console output.
|
The `BenchmarkRunner.Run<Md5VsSha256>()` call runs your benchmarks and print results to console output.
|
||||||
|
|
|
@ -10,19 +10,28 @@ Create new console application and install the [BenchmarkDotNet](https://www.nug
|
||||||
* *Languages:* C#, F#, VB
|
* *Languages:* C#, F#, VB
|
||||||
|
|
||||||
## Design a benchmark
|
## Design a benchmark
|
||||||
|
Create a new console application, write a class with methods that you want to measure and mark them with the `Benchmark` attribute. In the following example, we
|
||||||
Write a class with methods that you want to measure and mark them with the `Benchmark` attribute. In the following example, we
|
|
||||||
compare the [MD5](https://en.wikipedia.org/wiki/MD5) and [SHA256](https://en.wikipedia.org/wiki/SHA-2) cryptographic hash functions:
|
compare the [MD5](https://en.wikipedia.org/wiki/MD5) and [SHA256](https://en.wikipedia.org/wiki/SHA-2) cryptographic hash functions:
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
|
using System;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using BenchmarkDotNet.Attributes;
|
||||||
|
using BenchmarkDotNet.Running;
|
||||||
|
|
||||||
|
namespace MyBenchmarks
|
||||||
|
{
|
||||||
public class Md5VsSha256
|
public class Md5VsSha256
|
||||||
{
|
{
|
||||||
private readonly byte[] data = new byte[10000];
|
private const int N = 10000;
|
||||||
|
private readonly byte[] data;
|
||||||
|
|
||||||
private readonly SHA256 sha256 = SHA256.Create();
|
private readonly SHA256 sha256 = SHA256.Create();
|
||||||
private readonly MD5 md5 = MD5.Create();
|
private readonly MD5 md5 = MD5.Create();
|
||||||
|
|
||||||
public Md5VsSha256()
|
public Md5VsSha256()
|
||||||
{
|
{
|
||||||
|
data = new byte[N];
|
||||||
new Random(42).NextBytes(data);
|
new Random(42).NextBytes(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -40,6 +49,7 @@ public class Program
|
||||||
var summary = BenchmarkRunner.Run<Md5VsSha256>();
|
var summary = BenchmarkRunner.Run<Md5VsSha256>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The `BenchmarkRunner.Run<Md5VsSha256>()` call runs your benchmarks and print results to console output.
|
The `BenchmarkRunner.Run<Md5VsSha256>()` call runs your benchmarks and print results to console output.
|
||||||
|
|
Загрузка…
Ссылка в новой задаче