File: switch_vs_ifs.py

package info (click to toggle)
re2c 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 50,052 kB
  • sloc: cpp: 32,477; ml: 8,279; sh: 5,265; makefile: 968; haskell: 612; python: 428; ansic: 227; javascript: 111; java: 3
file content (124 lines) | stat: -rw-r--r-- 3,998 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Generated by re2py
# re2py $INPUT -o $OUTPUT

from time import time



def lex_switch_cases(str):
    cur = 0
    
    yystate = 0
    while True:
        match yystate:
            case 0:
                yych = str[cur]
                cur += 1
                match yych:
                    case 0x41|0x42|0x43|0x44|0x45|0x46|0x47|0x48|0x49|0x4A|0x4B|0x4C|0x4D|0x4E|0x4F|0x50|0x51|0x52|0x53|0x54|0x55|0x56|0x57|0x58|0x59|0x5A|0x5F|0x61|0x62|0x63|0x64|0x65|0x66|0x67|0x68|0x69|0x6A|0x6B|0x6C|0x6D|0x6E|0x6F|0x70|0x71|0x72|0x73|0x74|0x75|0x76|0x77|0x78|0x79|0x7A:
                        yystate = 2
                        continue
                    case _:
                        yystate = 1
                        continue
            case 1:
                return False
            case 2:
                yych = str[cur]
                match yych:
                    case 0x30|0x31|0x32|0x33|0x34|0x35|0x36|0x37|0x38|0x39|0x41|0x42|0x43|0x44|0x45|0x46|0x47|0x48|0x49|0x4A|0x4B|0x4C|0x4D|0x4E|0x4F|0x50|0x51|0x52|0x53|0x54|0x55|0x56|0x57|0x58|0x59|0x5A|0x5F|0x61|0x62|0x63|0x64|0x65|0x66|0x67|0x68|0x69|0x6A|0x6B|0x6C|0x6D|0x6E|0x6F|0x70|0x71|0x72|0x73|0x74|0x75|0x76|0x77|0x78|0x79|0x7A:
                        cur += 1
                        yystate = 2
                        continue
                    case _:
                        yystate = 3
                        continue
            case 3:
                return True
            case _:
                raise "internal lexer error"


def lex_nested_ifs(str):
    cur = 0
    
    yystate = 0
    while True:
        match yystate:
            case 0:
                yych = str[cur]
                cur += 1
                if yych <= 0x5E:
                    if yych <= 0x40:
                        yystate = 1
                        continue
                    if yych <= 0x5A:
                        yystate = 2
                        continue
                    yystate = 1
                    continue
                else:
                    if yych == 0x60:
                        yystate = 1
                        continue
                    if yych <= 0x7A:
                        yystate = 2
                        continue
                    yystate = 1
                    continue
            case 1:
                return False
            case 2:
                yych = str[cur]
                if yych <= 0x5A:
                    if yych <= 0x2F:
                        yystate = 3
                        continue
                    if yych <= 0x39:
                        cur += 1
                        yystate = 2
                        continue
                    if yych >= 0x41:
                        cur += 1
                        yystate = 2
                        continue
                    yystate = 3
                    continue
                else:
                    if yych <= 0x5F:
                        if yych >= 0x5F:
                            cur += 1
                            yystate = 2
                            continue
                        yystate = 3
                        continue
                    else:
                        if yych <= 0x60:
                            yystate = 3
                            continue
                        if yych <= 0x7A:
                            cur += 1
                            yystate = 2
                            continue
                        yystate = 3
                        continue
            case 3:
                return True
            case _:
                raise "internal lexer error"


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))