Almost done
This commit is contained in:
parent
e791a1e82d
commit
bee6519919
@ -17,6 +17,7 @@ def read_md():
|
|||||||
md = [line.strip() for line in f.readlines() if line.strip() != ""][1:]
|
md = [line.strip() for line in f.readlines() if line.strip() != ""][1:]
|
||||||
return md
|
return md
|
||||||
|
|
||||||
|
|
||||||
def write_md(results):
|
def write_md(results):
|
||||||
"""
|
"""
|
||||||
Write to README.md
|
Write to README.md
|
||||||
@ -24,36 +25,49 @@ def write_md(results):
|
|||||||
with open("../dummy.md", "w") as f:
|
with open("../dummy.md", "w") as f:
|
||||||
f.write(results)
|
f.write(results)
|
||||||
|
|
||||||
|
|
||||||
def _file_stem(f):
|
def _file_stem(f):
|
||||||
return f[-5:]
|
return f[-5:]
|
||||||
|
|
||||||
|
|
||||||
def get_problems(path=None, ext=None):
|
def get_problems(path=None, ext=None):
|
||||||
wd = os.getcwd()
|
wd = os.getcwd()
|
||||||
os.chdir(Path(path))
|
os.chdir(Path(path))
|
||||||
scripts = sorted([script.as_posix() for script in Path(".").rglob(f"*[0-9].{ext}")], key=_file_stem)
|
scripts = sorted(
|
||||||
|
[script.as_posix() for script in Path(".").rglob(f"*[0-9].{ext}")],
|
||||||
|
key=_file_stem,
|
||||||
|
)
|
||||||
os.chdir(wd)
|
os.chdir(wd)
|
||||||
return scripts
|
return scripts
|
||||||
|
|
||||||
def timing_Python(path=None):
|
|
||||||
|
def timing_Python(path=None, exec_only=1):
|
||||||
wd = os.getcwd()
|
wd = os.getcwd()
|
||||||
scripts = get_problems(path=path, ext="py")
|
scripts = get_problems(path=path, ext="py")
|
||||||
os.chdir(Path(path))
|
os.chdir(Path(path))
|
||||||
python_timings, python_results = [], []
|
python_timings, python_results = [], []
|
||||||
for script in scripts[:5]:
|
for script in scripts[:exec_only]:
|
||||||
_res = run(["python3", f"{script}"], capture_output=True, text=True).stdout.split("\n")
|
print(f"Executing {script}")
|
||||||
|
_res = run(
|
||||||
|
["python3", f"{script}"], capture_output=True, text=True
|
||||||
|
).stdout.split("\n")
|
||||||
python_timings.append(_res[0])
|
python_timings.append(_res[0])
|
||||||
python_results.append(_res[2])
|
python_results.append(_res[2])
|
||||||
|
|
||||||
os.chdir(wd)
|
os.chdir(wd)
|
||||||
return python_timings, python_results
|
return python_timings, python_results
|
||||||
|
|
||||||
def timing_Julia(path=None):
|
|
||||||
|
def timing_Julia(path=None, exec_only=1):
|
||||||
wd = os.getcwd()
|
wd = os.getcwd()
|
||||||
scripts = get_problems(path="Julia", ext="jl")
|
scripts = get_problems(path="Julia", ext="jl")
|
||||||
os.chdir(Path(path))
|
os.chdir(Path(path))
|
||||||
julia_problem_numbers, julia_timings, julia_results = [], [], []
|
julia_problem_numbers, julia_timings, julia_results = [], [], []
|
||||||
for script in scripts[:5]:
|
for script in scripts[:exec_only]:
|
||||||
_res = run(["julia", f"{script}"], capture_output=True, text=True).stdout.split("\n")
|
print(f"Executing {script}")
|
||||||
|
_res = run(["julia", f"{script}"], capture_output=True, text=True).stdout.split(
|
||||||
|
"\n"
|
||||||
|
)
|
||||||
julia_problem_numbers.append(_res[0])
|
julia_problem_numbers.append(_res[0])
|
||||||
julia_timings.append(_res[1].split("(")[0])
|
julia_timings.append(_res[1].split("(")[0])
|
||||||
julia_results.append(_res[3])
|
julia_results.append(_res[3])
|
||||||
@ -61,42 +75,92 @@ def timing_Julia(path=None):
|
|||||||
os.chdir(wd)
|
os.chdir(wd)
|
||||||
return julia_problem_numbers, julia_timings, julia_results
|
return julia_problem_numbers, julia_timings, julia_results
|
||||||
|
|
||||||
def execute():
|
|
||||||
|
def execute(combine_columns=True, nproblems=-1):
|
||||||
"""
|
"""
|
||||||
Execute scripts and return md formatted table
|
Execute scripts and return md formatted table
|
||||||
"""
|
"""
|
||||||
python_timings, python_results = timing_Python(path="Python")
|
python_timings, python_results = timing_Python(path="Python", exec_only=nproblems)
|
||||||
julia_problem_numbers, julia_timings, julia_results = timing_Julia(path="Julia")
|
julia_n_problem, julia_timings, julia_results = timing_Julia(
|
||||||
|
path="Julia", exec_only=nproblems
|
||||||
|
)
|
||||||
|
|
||||||
for lang in ["Python", "Julia"]:
|
assert len(python_timings) == len(julia_timings)
|
||||||
if lang == "Python":
|
|
||||||
headers=["Problem #", "Result", "Execution time (Python)"]
|
for i, (pr, jr) in enumerate(zip(python_results, julia_results), start=1):
|
||||||
value_matrix=[[f"{int(t[25:28]):03d}", int(r[22:]), t[29:]] for t,r in zip(python_timings, python_results)]
|
try:
|
||||||
elif lang == "Julia":
|
assert int(pr[22:]) == int(jr[23:])
|
||||||
headers=["Problem #", "Result", "Execution time (Julia)"]
|
except ValueError:
|
||||||
value_matrix=[[f"{int(p[-5:-1]):03d}", int(r[23:]), t] for p,t,r in zip(julia_problem_numbers, julia_timings, julia_results)]
|
print(f"Results for problem {i} are not identical!\n")
|
||||||
print(f"Printing for {lang}")
|
continue
|
||||||
writer = MarkdownTableWriter(
|
|
||||||
|
if combine_columns:
|
||||||
|
headers = [
|
||||||
|
"Problem #",
|
||||||
|
"Result",
|
||||||
|
"T_exec (Python)",
|
||||||
|
"T_exec (Julia)",
|
||||||
|
]
|
||||||
|
value_matrix = [
|
||||||
|
[f"{int(t_python[25:28]):03d}", int(r_python[22:]), t_python[29:], t_julia]
|
||||||
|
for t_python, r_python, t_julia in zip(
|
||||||
|
python_timings, python_results, julia_timings
|
||||||
|
)
|
||||||
|
]
|
||||||
|
writer_comb = MarkdownTableWriter(
|
||||||
table_name="Project Euler Solutions",
|
table_name="Project Euler Solutions",
|
||||||
headers=headers, #["Problem #", "Result", f"Execution time ({lang})"],
|
headers=headers,
|
||||||
value_matrix=value_matrix, #[[f"{int(t[25:28]):03d}", int(r[22:]), t[29:]] for t,r in zip(python_timings, python_results)],
|
value_matrix=value_matrix,
|
||||||
# headers=["Problem #", "Result", "Execution time (Julia)"],
|
|
||||||
# value_matrix=[[f"{int(p[-5:-1]):03d}", int(r[23:]), t] for p,t,r in zip(julia_problem_numbers, julia_timings, julia_results)],
|
|
||||||
column_styles=[
|
column_styles=[
|
||||||
Style(align="center"),
|
Style(align="center"),
|
||||||
Style(align="right", thousand_separator="_"),
|
Style(align="right", thousand_separator="_"),
|
||||||
Style(align="right"),
|
Style(align="right"),
|
||||||
|
Style(align="right"),
|
||||||
], # specify styles for each column
|
], # specify styles for each column
|
||||||
margin=1 # add a whitespace for both sides of each cell
|
margin=1, # add a whitespace for both sides of each cell
|
||||||
)
|
)
|
||||||
writer.write_table()
|
writer_comb.write_table()
|
||||||
print(writer.dumps())
|
writer_comb.write_null_line()
|
||||||
# return writer.dumps()
|
|
||||||
|
else:
|
||||||
|
languages = ["Python", "Julia"]
|
||||||
|
|
||||||
|
for lang in languages:
|
||||||
|
if lang == "Python":
|
||||||
|
headers = ["Problem #", "Result", "T_exec (Python)"]
|
||||||
|
value_matrix = [
|
||||||
|
[f"{int(t_python[25:28]):03d}", int(r_python[22:]), t_python[29:]]
|
||||||
|
for t_python, r_python in zip(python_timings, python_results)
|
||||||
|
]
|
||||||
|
elif lang == "Julia":
|
||||||
|
headers = ["Problem #", "Result", "T_exec (Julia)"]
|
||||||
|
value_matrix = [
|
||||||
|
[f"{int(p_julia[-5:-1]):03d}", int(r_julia[23:]), t_julia]
|
||||||
|
for p_julia, r_julia, t_julia in zip(
|
||||||
|
julia_n_problem, julia_results, julia_timings
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
writer = MarkdownTableWriter(
|
||||||
|
table_name=f"Project Euler Solutions for {lang}",
|
||||||
|
headers=headers,
|
||||||
|
value_matrix=value_matrix,
|
||||||
|
column_styles=[
|
||||||
|
Style(align="center"),
|
||||||
|
Style(align="right", thousand_separator="_"),
|
||||||
|
Style(align="right"),
|
||||||
|
], # specify styles for each column
|
||||||
|
margin=1, # add a whitespace for both sides of each cell
|
||||||
|
)
|
||||||
|
writer.write_table()
|
||||||
|
writer.write_null_line()
|
||||||
|
# print(writer.dumps())
|
||||||
|
# return writer.dumps()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
md = read_md()
|
md = read_md()
|
||||||
# print(md)
|
# print(md)
|
||||||
res = execute()
|
res = execute(combine_columns=True)
|
||||||
# print(res)
|
# print(res)
|
||||||
# write_md(res)
|
# write_md(res)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user