~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.ConsistencyCheck/VisitorBenchmark.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
 
2
// 
 
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:
 
8
// 
 
9
// The above copyright notice and this permission notice shall be included in all copies or
 
10
// substantial portions of the Software.
 
11
// 
 
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.
 
18
 
 
19
using System;
 
20
using System.Collections.Generic;
 
21
using System.Diagnostics;
 
22
using System.Linq;
 
23
using ICSharpCode.NRefactory.CSharp;
 
24
 
 
25
namespace ICSharpCode.NRefactory.ConsistencyCheck
 
26
{
 
27
        /// <summary>
 
28
        /// Determines the fastest way to retrieve a List{IdentifierExpression} of all identifiers
 
29
        /// in a syntax tree.
 
30
        /// </summary>
 
31
        public class VisitorBenchmark
 
32
        {
 
33
                public static void Run(IEnumerable<SyntaxTree> files)
 
34
                {
 
35
                        files = files.ToList();
 
36
                        
 
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>()) {
 
42
                                                list.Add(node);
 
43
                                        }
 
44
                                });
 
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);
 
52
                                });
 
53
                }
 
54
                
 
55
                static void WalkTreeForEach(AstNode node, List<IdentifierExpression> list)
 
56
                {
 
57
                        foreach (AstNode child in node.Children) {
 
58
                                IdentifierExpression id = child as IdentifierExpression;
 
59
                                if (id != null) {
 
60
                                        list.Add(id);
 
61
                                }
 
62
                                WalkTreeForEach(child, list);
 
63
                        }
 
64
                }
 
65
                
 
66
                static void WalkTreeFor(AstNode node, List<IdentifierExpression> list)
 
67
                {
 
68
                        for (AstNode child = node.FirstChild; child != null; child = child.NextSibling) {
 
69
                                IdentifierExpression id = child as IdentifierExpression;
 
70
                                if (id != null) {
 
71
                                        list.Add(id);
 
72
                                }
 
73
                                WalkTreeFor(child, list);
 
74
                        }
 
75
                }
 
76
                
 
77
                static void WalkTreeNonRecursive(AstNode root, List<IdentifierExpression> list)
 
78
                {
 
79
                        AstNode pos = root;
 
80
                        while (pos != null) {
 
81
                                {
 
82
                                        IdentifierExpression id = pos as IdentifierExpression;
 
83
                                        if (id != null) {
 
84
                                                list.Add(id);
 
85
                                        }
 
86
                                }
 
87
                                if (pos.FirstChild != null) {
 
88
                                        pos = pos.FirstChild;
 
89
                                } else {
 
90
                                        pos = pos.GetNextNode();
 
91
                                }
 
92
                        }
 
93
                }
 
94
                
 
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)
 
99
                        {
 
100
                                list.Add(identifierExpression);
 
101
                                base.VisitIdentifierExpression(identifierExpression);
 
102
                        }
 
103
                }
 
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)
 
108
                        {
 
109
                                list.Add(identifierExpression);
 
110
                                return base.VisitIdentifierExpression(identifierExpression);
 
111
                        }
 
112
                }
 
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)
 
117
                        {
 
118
                                list.Add(identifierExpression);
 
119
                                return base.VisitIdentifierExpression(identifierExpression, data);
 
120
                        }
 
121
                }
 
122
                
 
123
                static void RunTest(string text, IEnumerable<SyntaxTree> files, Action<SyntaxTree, List<IdentifierExpression>> action)
 
124
                {
 
125
                        // validation:
 
126
                        var list = new List<IdentifierExpression>();
 
127
                        foreach (var file in files) {
 
128
                                list.Clear();
 
129
                                action(file, list);
 
130
                                if (!list.SequenceEqual(file.Descendants.OfType<IdentifierExpression>()))
 
131
                                        throw new InvalidOperationException();
 
132
                        }
 
133
                        Stopwatch w = Stopwatch.StartNew();
 
134
                        foreach (var file in files) {
 
135
                                for (int i = 0; i < 20; i++) {
 
136
                                        list.Clear();
 
137
                                        action(file, list);
 
138
                                }
 
139
                        }
 
140
                        w.Stop();
 
141
                        Console.WriteLine(text.PadRight(40) + ": " + w.Elapsed);
 
142
                }
 
143
        }
 
144
}