-
-
Save notmyname/19a9ff6c8a037f471e87 to your computer and use it in GitHub Desktop.
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
python ./speed.py test_any | |
1.35776686668 | |
7 0 SETUP_LOOP 30 (to 33) | |
3 LOAD_GLOBAL 0 (FORBIDDEN_CHARS) | |
6 GET_ITER | |
>> 7 FOR_ITER 22 (to 32) | |
10 STORE_FAST 1 (c) | |
8 13 LOAD_FAST 1 (c) | |
16 LOAD_FAST 0 (path) | |
19 COMPARE_OP 6 (in) | |
22 POP_JUMP_IF_FALSE 7 | |
9 25 LOAD_GLOBAL 1 (True) | |
28 RETURN_VALUE | |
29 JUMP_ABSOLUTE 7 | |
>> 32 POP_BLOCK | |
10 >> 33 LOAD_GLOBAL 2 (False) | |
36 RETURN_VALUE | |
****************************** | |
14 0 LOAD_GLOBAL 0 (any) | |
15 3 LOAD_CLOSURE 0 (path) | |
6 BUILD_TUPLE 1 | |
9 LOAD_CONST 1 (<code object <genexpr> at 0x10f1f81b0, file "./speed.py", line 15>) | |
12 MAKE_CLOSURE 0 | |
16 15 LOAD_GLOBAL 1 (FORBIDDEN_CHARS) | |
18 GET_ITER | |
19 CALL_FUNCTION 1 | |
22 CALL_FUNCTION 1 | |
25 RETURN_VALUE |
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
FORBIDDEN_CHARS = "\'\"`<>" | |
PATH = '/v1/AUTH_test/test/test' | |
BAD_PATH = '/v1/AUTH_test/test/test\'' | |
def test_loop(path): | |
for c in FORBIDDEN_CHARS: | |
if c in path: | |
return True | |
return False | |
def test_any(path): | |
return any( | |
(c in path) | |
for c in FORBIDDEN_CHARS | |
) | |
if __name__ == '__main__': | |
import sys | |
if not len(sys.argv) > 1: | |
sys.exit('USAGE: %s <test_any|test_loop> [--bad]' % sys.argv[0]) | |
import timeit | |
tests = [ | |
test_loop, | |
test_any, | |
] | |
for test_f in tests: | |
if sys.argv[1] == test_f.__name__: | |
break | |
else: | |
sys.exit('ERROR: unknown test %s' % sys.argv[1]) | |
if '--bad' in sys.argv: | |
path = 'BAD_PATH' | |
else: | |
path = 'PATH' | |
setup_stmt = 'from __main__ import %s, %s' % (test_f.__name__, path) | |
run_stmt = '%s(%s)' % (test_f.__name__, path) | |
print(timeit.timeit(run_stmt, setup=setup_stmt)) | |
import dis | |
dis.dis(test_loop) | |
print '*' * 30 | |
dis.dis(test_any) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment