-
Notifications
You must be signed in to change notification settings - Fork 272
/
Perf.XmlNodeList.cs
49 lines (42 loc) · 1.44 KB
/
Perf.XmlNodeList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Xml;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using MicroBenchmarks;
namespace XmlDocumentTests.XmlNodeListTests
{
[BenchmarkCategory(Categories.Libraries)]
public class Perf_XmlNodeList
{
private readonly Consumer _consumer = new Consumer();
private XmlNodeList _list;
[GlobalSetup(Target = nameof(Enumerator))]
public void SetupEnumerator()
{
var doc = new XmlDocument();
doc.LoadXml("<a><sub1/><sub1/><sub2/><sub1/><sub2/><sub1/><sub2/><sub1/><sub2/><sub1/><sub2/><sub2/></a>");
_list = doc.DocumentElement.ChildNodes;
}
[Benchmark]
public void Enumerator()
{
XmlNodeList list = _list;
Consumer consumer = _consumer;
foreach (var element in list)
{
consumer.Consume(element);
}
}
[GlobalSetup(Target = nameof(GetCount))]
public void SetupGetCount()
{
var doc = new XmlDocument();
doc.LoadXml("<a><sub1/><sub2/></a>");
_list = doc.DocumentElement.ChildNodes;
}
[Benchmark]
public int GetCount() => _list.Count;
}
}