diff --git a/M2/Macaulay2/m2/profile.m2 b/M2/Macaulay2/m2/profile.m2 index 370f9cbc38..18b3553acf 100644 --- a/M2/Macaulay2/m2/profile.m2 +++ b/M2/Macaulay2/m2/profile.m2 @@ -1,4 +1,9 @@ --- Copyright 1997 by Daniel R. Grayson +-- Copyright 2024 by Mahrud Sayrafi + +-- 'profile' is an interpreter keyword, defined in d/profiler.dd, +-- which logs statistics of executed M2 code in 'ProfileTable'. +-- TODO: log the relationship between function calls and return +-- in an external format like Graphviz, pprof, etc. needs "methods.m2" @@ -6,6 +11,7 @@ head := () -> ("#run", "%time", "position") form := (ttime, t, n, loc) -> (n, format(4,2,2,2,"e", 100 * t / ttime), loc) tail := (ttime, tticks) -> (tticks, format(4,4,4,4,"e",ttime) | "s", "elapsed total") +-- prints the statistics logged by the profiler in a readable table profileSummary = method(Dispatch => Thing) profileSummary Thing := x -> profileSummary if x === () then "" else first locate x profileSummary String := filename -> ( @@ -21,6 +27,9 @@ profileSummary String := filename -> ( TABLE join({head()}, body, {tail(ttime, tticks)})) profileSummary = new Command from profileSummary +-- prints a list of lines which have been seen by the profiler so far +-- TODO: also highlight missing lines or sections within a line +-- TODO: compute the percentage of covered code coverageSummary = method(Dispatch => Thing) coverageSummary Thing := x -> coverageSummary if x === () then "" else first locate x coverageSummary String := filename -> ( @@ -28,34 +37,3 @@ coverageSummary String := filename -> ( k -> match_filename k); stack join({"covered lines:"}, body)) coverageSummary = new Command from coverageSummary - -end-- - -profile = method() - -record := new MutableHashTable - -profile Function := Function => f -> profile (toString f, f) - -profile(String,Function) := (n,f) -> ( - record#n = m := new MutableList from {0,0.}; - args -> ( - ret := timing f args; - m#0 = m#0 + 1; - m#1 = m#1 + ret#0; - ret#1 - ) - ) - -profileSummary = Command (() -> - scan(sort pairs record, (n,v) -> - if v#0 != 0 then - << n << ": " - << v#0 << " times, used " - << v#1 << " seconds" - << endl - )) - --- Local Variables: --- compile-command: "make -C $M2BUILDDIR/Macaulay2/m2 " --- End: