• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * [The "BSD licence"]
3  * Copyright (c) 2005-2008 Terence Parr
4  * All rights reserved.
5  *
6  * Conversion to C#:
7  * Copyright (c) 2008-2009 Sam Harwell, Pixel Mine, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 namespace Antlr3.Runtime.Test
34 {
35     using System;
36     using Antlr.Runtime;
37     using Antlr.Runtime.Tree;
38     using Microsoft.VisualStudio.TestTools.UnitTesting;
39     using System.Text;
40 
41     [TestClass]
42     public class TestFastLexer
43     {
44         [TestMethod]
TestBasicFastLexer()45         public void TestBasicFastLexer()
46         {
47             string inputElement = "x-2356*Abte+32+eno/6623+y";
48             StringBuilder builder = new StringBuilder( inputElement, 1000000 * inputElement.Length );
49             for ( int i = 0; i < 999999; i++ )
50                 builder.Append( inputElement );
51 
52             string text = builder.ToString();
53             int iterations = 5;
54             // warmup
55             IterateFast( text, iterations );
56             Iterate( text, iterations );
57 
58             var time = Iterate( text, iterations );
59             var timeFast = IterateFast( text, iterations );
60 
61             Console.WriteLine( "Elapsed time (norm): {0} seconds.", time.TotalSeconds );
62             Console.WriteLine( "Elapsed time (fast): {0} seconds.", timeFast.TotalSeconds );
63         }
64 
Iterate( string text, int count )65         TimeSpan Iterate( string text, int count )
66         {
67             DateTime start = DateTime.Now;
68 
69             for ( int i = 0; i < count; i++ )
70             {
71                 SimpleExpressionLexer lexer = new SimpleExpressionLexer( new ANTLRStringStream( text ) );
72                 CommonTokenStream tokens = new CommonTokenStream( lexer );
73                 tokens.Fill();
74             }
75 
76             return DateTime.Now - start;
77         }
78 
IterateFast( string text, int count )79         TimeSpan IterateFast( string text, int count )
80         {
81             DateTime start = DateTime.Now;
82 
83             for ( int i = 0; i < count; i++ )
84             {
85                 FastSimpleExpressionLexer lexer = new FastSimpleExpressionLexer( new SlimStringStream( text ) );
86                 FastTokenStream tokens = new FastTokenStream( lexer );
87                 //FastTokenStream<SlimToken> tokens = new FastTokenStream<SlimToken>( lexer );
88                 tokens.FillBuffer();
89             }
90 
91             return DateTime.Now - start;
92         }
93     }
94 }
95