forked from bmw-software-engineering/trlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bmw-software-engineering#43 Improve performance
Replace the char classification functions with more efficient, but equivalent, implementations. This reduces token() runtime from 18.2s to 15.1 which is a 17% improvement.
- Loading branch information
1 parent
5b0f823
commit d08ad1d
Showing
3 changed files
with
67 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import unittest | ||
import re | ||
|
||
from trlc.errors import Message_Handler | ||
from trlc.lexer import Lexer_Base | ||
|
||
|
||
class Potato(Lexer_Base): | ||
def file_location(self): | ||
pass | ||
|
||
def token(self): | ||
pass | ||
|
||
|
||
class Test_Lexer_Base(unittest.TestCase): | ||
def setUp(self): | ||
self.lexer = Potato(mh = Message_Handler(), | ||
content = "") | ||
self.test_range = 0xffff | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
@staticmethod | ||
def reference_is_alpha(char): | ||
assert isinstance(char, str) and len(char) == 1 | ||
return ord('a') <= ord(char) <= ord('z') or \ | ||
ord('A') <= ord(char) <= ord('Z') | ||
|
||
@staticmethod | ||
def reference_is_numeric(char): | ||
assert isinstance(char, str) and len(char) == 1 | ||
return ord('0') <= ord(char) <= ord('9') | ||
|
||
@staticmethod | ||
def reference_is_alnum(char): | ||
assert isinstance(char, str) and len(char) == 1 | ||
return ord('a') <= ord(char) <= ord('z') or \ | ||
ord('A') <= ord(char) <= ord('Z') or \ | ||
ord('0') <= ord(char) <= ord('9') | ||
|
||
def testIsAlpha(self): | ||
for i in range(self.test_range): | ||
c = chr(i) | ||
self.assertEqual(self.reference_is_alpha(c), | ||
self.lexer.is_alpha(c), | ||
"mismatch for codepoint %u (%s)" % (i, repr(c))) | ||
|
||
def testIsDigit(self): | ||
for i in range(self.test_range): | ||
c = chr(i) | ||
self.assertEqual(self.reference_is_numeric(c), | ||
self.lexer.is_numeric(c), | ||
"mismatch for codepoint %u (%s)" % (i, repr(c))) | ||
|
||
def testIsAlnum(self): | ||
for i in range(self.test_range): | ||
c = chr(i) | ||
self.assertEqual(self.reference_is_alnum(c), | ||
self.lexer.is_alnum(c), | ||
"mismatch for codepoint %u (%s)" % (i, repr(c))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters