You can get detailed performance per function call with this snippet. Edit the lark main and add the setup to the beginning and the result printing to the bottom. Optimally, lark could have a command line option which activates this on its main.
import sys
import io
import pstats
import cProfile
profiler = cProfile.Profile()
profiler.enable()
# Core here
profiler.disable()
with io.StringIO() as outputstream:
profiller_status = pstats.Stats( profiler, stream=outputstream )
profiller_status.sort_stats( "time" )
profiller_status.print_stats()
sys.stderr.write( outputstream.getvalue()[:2000] + '\n' )
Running this solemnly will output:
1 function calls in 0.000 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Adding a few cool stuff inside it like:
import mmap
import json
# https://docs.python.org/3.9/library/mmap.html
# https://blog.askesis.pl/post/2019/02/mmap.html
memory = mmap.mmap( -1, 20, access=mmap.ACCESS_WRITE )
memory.write( b'{ "var": 10 }' )
end_of_the_file = memory.tell()
print( 'memory {:,}, end_of_the_file {}\n'.format( len( memory ), end_of_the_file ) )
memory.seek( 0 )
print( 'raw', memory.read( end_of_the_file ) )
memory.seek( 0 )
loaded = json.loads( memory.read( end_of_the_file ) )
print( 'json', loaded )
print( 'loaded', memory[:end_of_the_file] )
Will output:
3,031 function calls (2,921 primitive calls) in 0.006 seconds
Ordered by: internal time
ncalls tottime percall cumtime percall filename:lineno(function)
17 0.001 0.000 0.001 0.000 {built-in method nt.stat}
15/6 0.000 0.000 0.001 0.000 F:\Python\lib\sre_parse.py:475(_parse)
4 0.000 0.000 0.000 0.000 {built-in method marshal.loads}
4 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap_external>:914(get_data)
4 0.000 0.000 0.001 0.000 <frozen importlib._bootstrap_external>:793(get_code)
25/6 0.000 0.000 0.000 0.000 F:\Python\lib\sre_compile.py:71(_compile)
4 0.000 0.000 0.000 0.000 {method 'read' of '_io.FileIO' objects}
14 0.000 0.000 0.000 0.000 F:\Python\lib\sre_compile.py:276(_optimize_charset)
6 0.000 0.000 0.001 0.000 <frozen importlib._bootstrap_external>:1356(find_spec)
4 0.000 0.000 0.000 0.000 {built-in method builtins.print}
102 0.000 0.000 0.000 0.000 F:\Python\lib\sre_parse.py:233(__next)
31/12 0.000 0.000 0.000 0.000 F:\Python\lib\sre_parse.py:174(getwidth)
3 0.000 0.000 0.000 0.000 {built-in method builtins.__build_class__}
2 0.000 0.000 0.000 0.000 {built-in method _imp.create_builtin}
6/2 0.000 0.000 0.006 0.003 <frozen importlib._bootstrap>:978(_find_and_load)
123 0.000 0.000 0.000 0.000 F:\Python\lib\sre_parse.py:164(__getitem__)
39 0.000 0.000 0.000 0.000 {method 'format' of 'str' objects}
1 0.000 0.000 0.000 0.00
To visualize the data, you can adapt the script to use some tool as snakeviz
:
https://stackoverflow.com/questions/29630667/how-can-i-analyze-a-file-created-with-pstats-dump-statsfilename-off-line
import os
import sys
import snakeviz
from snakeviz.cli import main
...
profiler.disable()
profilepath = "path_to_data.prof"
profiler.dump_stats(profilepath)
sys.argv.append(profilepath)
sys.exit(main())
It is a webpage you can visuallly browse the cPrifle results: