1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
3
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4
// software and associated documentation files (the "Software"), to deal in the Software
5
// without restriction, including without limitation the rights to use, copy, modify, merge,
6
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7
// to whom the Software is furnished to do so, subject to the following conditions:
9
// The above copyright notice and this permission notice shall be included in all copies or
10
// substantial portions of the Software.
12
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17
// DEALINGS IN THE SOFTWARE.
20
using System.Collections.Generic;
21
using System.Diagnostics;
23
using ICSharpCode.NRefactory.CSharp;
25
namespace ICSharpCode.NRefactory.ConsistencyCheck
28
/// Determines the fastest way to retrieve a List{IdentifierExpression} of all identifiers
31
public class VisitorBenchmark
33
public static void Run(IEnumerable<SyntaxTree> files)
35
files = files.ToList();
37
RunTest("recursive method using for", files, (syntaxTree, list) => WalkTreeFor(syntaxTree, list));
38
RunTest("recursive method using foreach", files, (syntaxTree, list) => WalkTreeForEach(syntaxTree, list));
39
RunTest("non-recursive loop", files, (syntaxTree, list) => WalkTreeNonRecursive(syntaxTree, list));
40
RunTest("foreach over Descendants.OfType()", files, (syntaxTree, list) => {
41
foreach (var node in syntaxTree.Descendants.OfType<IdentifierExpression>()) {
45
RunTest("DepthFirstAstVisitor", files, (syntaxTree, list) => syntaxTree.AcceptVisitor(new DepthFirst(list)));
46
RunTest("DepthFirstAstVisitor<object>", files, (syntaxTree, list) => syntaxTree.AcceptVisitor(new DepthFirst<object>(list)));
47
RunTest("DepthFirstAstVisitor<object, object>", files, (syntaxTree, list) => syntaxTree.AcceptVisitor(new DepthFirst<object, object>(list), null));
48
RunTest("ObservableAstVisitor", files, (syntaxTree, list) => {
49
var visitor = new ObservableAstVisitor();
50
visitor.EnterIdentifierExpression += list.Add;
51
syntaxTree.AcceptVisitor(visitor);
55
static void WalkTreeForEach(AstNode node, List<IdentifierExpression> list)
57
foreach (AstNode child in node.Children) {
58
IdentifierExpression id = child as IdentifierExpression;
62
WalkTreeForEach(child, list);
66
static void WalkTreeFor(AstNode node, List<IdentifierExpression> list)
68
for (AstNode child = node.FirstChild; child != null; child = child.NextSibling) {
69
IdentifierExpression id = child as IdentifierExpression;
73
WalkTreeFor(child, list);
77
static void WalkTreeNonRecursive(AstNode root, List<IdentifierExpression> list)
82
IdentifierExpression id = pos as IdentifierExpression;
87
if (pos.FirstChild != null) {
90
pos = pos.GetNextNode();
95
class DepthFirst : DepthFirstAstVisitor {
96
readonly List<IdentifierExpression> list;
97
public DepthFirst(List<IdentifierExpression> list) { this.list = list; }
98
public override void VisitIdentifierExpression(IdentifierExpression identifierExpression)
100
list.Add(identifierExpression);
101
base.VisitIdentifierExpression(identifierExpression);
104
class DepthFirst<T> : DepthFirstAstVisitor<T> {
105
readonly List<IdentifierExpression> list;
106
public DepthFirst(List<IdentifierExpression> list) { this.list = list; }
107
public override T VisitIdentifierExpression(IdentifierExpression identifierExpression)
109
list.Add(identifierExpression);
110
return base.VisitIdentifierExpression(identifierExpression);
113
class DepthFirst<T, S> : DepthFirstAstVisitor<T, S> {
114
readonly List<IdentifierExpression> list;
115
public DepthFirst(List<IdentifierExpression> list) { this.list = list; }
116
public override S VisitIdentifierExpression(IdentifierExpression identifierExpression, T data)
118
list.Add(identifierExpression);
119
return base.VisitIdentifierExpression(identifierExpression, data);
123
static void RunTest(string text, IEnumerable<SyntaxTree> files, Action<SyntaxTree, List<IdentifierExpression>> action)
126
var list = new List<IdentifierExpression>();
127
foreach (var file in files) {
130
if (!list.SequenceEqual(file.Descendants.OfType<IdentifierExpression>()))
131
throw new InvalidOperationException();
133
Stopwatch w = Stopwatch.StartNew();
134
foreach (var file in files) {
135
for (int i = 0; i < 20; i++) {
141
Console.WriteLine(text.PadRight(40) + ": " + w.Elapsed);