def print_timing(self):
# pylint: disable=no-self-use
# Test the implementation of asttokens.util.walk, which uses the same approach as
# visit_tree(). This doesn't run as a normal unittest, but if you'd like to see timings, e.g.
# after experimenting with the implementation, run this to see them:
#
# nosetests -i print_timing -s tests.test_util
#
import timeit
import textwrap
setup = textwrap.dedent(
'''
import ast, asttokens
source = "foo(bar(1 + 2), 'hello' + ', ' + 'world')"
atok = asttokens.ASTTokens(source, parse=True)
''')
print("ast", sorted(timeit.repeat(
setup=setup, number=10000,
stmt='len(list(ast.walk(atok.tree)))')))
print("util", sorted(timeit.repeat(
setup=setup, number=10000,
stmt='len(list(asttokens.util.walk(atok.tree)))')))
评论列表
文章目录