File: switch_vs_ifs.re

package info (click to toggle)
re2c 4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,512 kB
  • sloc: cpp: 34,160; ml: 8,494; sh: 5,311; makefile: 1,014; haskell: 611; python: 431; ansic: 234; javascript: 113
file content (43 lines) | stat: -rw-r--r-- 902 bytes parent folder | download | duplicates (2)
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
# re2py $INPUT -o $OUTPUT

from time import time

/*!rules:re2c:x
    re2c:api = custom;
    re2c:define:YYPEEK    = "str[cur]";
    re2c:define:YYSKIP    = "cur += 1";
    re2c:define:YYBACKUP  = "mar = cur";
    re2c:define:YYRESTORE = "cur = mar";
    re2c:yyfill:enable = 0;
    re2c:indent:top = 1;

    id = [a-zA-Z_][a-zA-Z0-9_]*;

    id { return True }
    *  { return False }
*/

def lex_switch_cases(str):
    cur = 0
    /*!use:re2c:x
        re2c:nested-ifs = 0;
    */

def lex_nested_ifs(str):
    cur = 0
    /*!use:re2c:x*/

STR = b"01234567890123456789012345678901234567890123456789012345678901234567890123456789\0"
TIMES = 1000000

start = time()
for i in range(TIMES):
    lex_nested_ifs(STR)
end = time()
print("nested ifs took {}".format(end - start))

start = time()
for i in range(TIMES):
    lex_switch_cases(STR)
end = time()
print("switch cases took {}".format(end - start))