37 Generates a large Fortran program in memory and then measures how long 38 it takes fparser2 to parse it. This is based on the benchmark suggested 39 by Ondřej Čertík via Ioannis Nikiteas. 42 from time
import perf_counter
49 def gen_sub(num: int):
51 Constructs a Fortran subroutine named g<num>. 53 :param num: the number of the subroutine (used to name it). 55 :returns: Fortran subroutine. 59 sub = f
"""subroutine g{num}(x) 60 integer, intent(inout) :: x 72 def create_bench(num_routines: int):
74 Creates the Fortran benchmark code. 76 :param num_routines: the number of subroutines to create. 78 :returns: benchmark Fortran code. 82 code = [
"program bench3",
"implicit none",
"integer :: c",
"c = 0"]
83 for i
in range(1, num_routines + 1):
84 code.append(f
"call g{i}(c)")
86 code.append(
"print *, c")
87 code.append(
"contains")
89 for i
in range(1, num_routines + 1):
90 code.append(gen_sub(i))
91 code.append(
"end program")
93 return "\n".join(code)
96 def runner(num_routines: int):
98 Entry point for running the benchmark. 100 :param num_routines: the number of subroutines to create in the \ 103 :raises ValueError: if num_routines < 1. 108 f
"Number of routines to create must be a positive, " 109 f
"non-zero integer but got: {num_routines}" 112 print(f
"Constructing benchmark code with {num_routines} subroutines...")
113 code = create_bench(num_routines)
114 reader = FortranStringReader(code)
116 reader.set_format(FortranFormat(
True,
True))
118 fparser = ParserFactory().create()
120 print(
"Parsing benchmark code...")
121 tstart = perf_counter()
123 tstop = perf_counter()
125 print(f
"Time taken for parse = {tstop - tstart:.2f}s")
128 if __name__ ==
"__main__":