Minor improvements in docs, part 2
This commit is contained in:
Родитель
74d79336b4
Коммит
764bd31b4e
|
@ -11,42 +11,44 @@ PM> Install-Package BenchmarkDotNet
|
|||
|
||||
|
||||
## 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:
|
||||
|
||||
```cs
|
||||
public class Md5VsSha256
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Running;
|
||||
|
||||
namespace MyBenchmarks
|
||||
{
|
||||
private const int N = 10000;
|
||||
private readonly byte[] data;
|
||||
|
||||
private readonly SHA256 sha256 = SHA256.Create();
|
||||
private readonly MD5 md5 = MD5.Create();
|
||||
|
||||
public Md5VsSha256()
|
||||
public class Md5VsSha256
|
||||
{
|
||||
data = new byte[N];
|
||||
new Random(42).NextBytes(data);
|
||||
private const int N = 10000;
|
||||
private readonly byte[] data;
|
||||
|
||||
private readonly SHA256 sha256 = SHA256.Create();
|
||||
private readonly MD5 md5 = MD5.Create();
|
||||
|
||||
public Md5VsSha256()
|
||||
{
|
||||
data = new byte[N];
|
||||
new Random(42).NextBytes(data);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public byte[] Sha256() => sha256.ComputeHash(data);
|
||||
|
||||
[Benchmark]
|
||||
public byte[] Md5() => md5.ComputeHash(data);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public byte[] Sha256()
|
||||
public class Program
|
||||
{
|
||||
return sha256.ComputeHash(data);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public byte[] Md5()
|
||||
{
|
||||
return md5.ComputeHash(data);
|
||||
}
|
||||
}
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var summary = BenchmarkRunner.Run<Md5VsSha256>();
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var summary = BenchmarkRunner.Run<Md5VsSha256>();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
@ -10,34 +10,44 @@ Create new console application and install the [BenchmarkDotNet](https://www.nug
|
|||
* *Languages:* C#, F#, VB
|
||||
|
||||
## 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 the [MD5](https://en.wikipedia.org/wiki/MD5) and [SHA256](https://en.wikipedia.org/wiki/SHA-2) cryptographic hash functions:
|
||||
|
||||
```cs
|
||||
public class Md5VsSha256
|
||||
{
|
||||
private readonly byte[] data = new byte[10000];
|
||||
private readonly SHA256 sha256 = SHA256.Create();
|
||||
private readonly MD5 md5 = MD5.Create();
|
||||
using System;
|
||||
using System.Security.Cryptography;
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Running;
|
||||
|
||||
public Md5VsSha256()
|
||||
namespace MyBenchmarks
|
||||
{
|
||||
public class Md5VsSha256
|
||||
{
|
||||
new Random(42).NextBytes(data);
|
||||
private const int N = 10000;
|
||||
private readonly byte[] data;
|
||||
|
||||
private readonly SHA256 sha256 = SHA256.Create();
|
||||
private readonly MD5 md5 = MD5.Create();
|
||||
|
||||
public Md5VsSha256()
|
||||
{
|
||||
data = new byte[N];
|
||||
new Random(42).NextBytes(data);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public byte[] Sha256() => sha256.ComputeHash(data);
|
||||
|
||||
[Benchmark]
|
||||
public byte[] Md5() => md5.ComputeHash(data);
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
public byte[] Sha256() => sha256.ComputeHash(data);
|
||||
|
||||
[Benchmark]
|
||||
public byte[] Md5() => md5.ComputeHash(data);
|
||||
}
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
public class Program
|
||||
{
|
||||
var summary = BenchmarkRunner.Run<Md5VsSha256>();
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var summary = BenchmarkRunner.Run<Md5VsSha256>();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
Загрузка…
Ссылка в новой задаче