Skip to content

Commit

Permalink
removed old profile code
Browse files Browse the repository at this point in the history
  • Loading branch information
mahrud committed Aug 27, 2024
1 parent 7880f2d commit 01db2c9
Showing 1 changed file with 10 additions and 32 deletions.
42 changes: 10 additions & 32 deletions M2/Macaulay2/m2/profile.m2
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
-- 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"

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 -> (
Expand All @@ -21,41 +27,13 @@ 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 -> (
body := sortByName select(toString \ keys ProfileTable,
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:

0 comments on commit 01db2c9

Please sign in to comment.