Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal to add @⌛ shortcut #106

Closed
marius311 opened this issue Feb 10, 2021 · 1 comment
Closed

Proposal to add @⌛ shortcut #106

marius311 opened this issue Feb 10, 2021 · 1 comment

Comments

@marius311
Copy link

marius311 commented Feb 10, 2021

I just wanted to ask before maybe cleaning this up for a proper PR.

This package is great, but one minor incovenience is labeling all your timed lines, and also the fact that it can be quite verbose so your code doesn't read as cleanly afterwards (assuming you want to leave the timers in there permanently). This tiny wrapper macro solves both of those things. From its docs:

    @⌛ code ...
    @⌛ function_definition() = .... 

Label a section of code to be timed. The first form uses the code
itselfs as a label, the second uses the function name, and its the
body of the function which is timed. 

To run the timer and print output, returning the result of the
calculation, use

    @show⌛ run_code()

Timing uses `TimerOutputs.get_defaulttimer()`. 

Let me know if you're interested in a PR, but also feel free to pass. Entire code below. Unicode chosen democratically via Slack :)

macro (ex)
    source_str = last(splitpath(string(__source__.file)))*":"*string(__source__.line)
    if _isdef(ex)
        sdef = splitdef(ex)
        sdef[:body] = quote
            @timeit $("$(string(sdef[:name]))(…)  ($source_str)") $(sdef[:body])
        end
        esc(combinedef(sdef))
    else
        :(@timeit $("$(Base._truncate_at_width_or_chars(string(prewalk(rmlines,ex)),26))  ($source_str)") $(esc(ex)))
    end
end


macro show⌛(ex)
    quote
        reset_timer!(get_defaulttimer())
        result = $(esc(ex))
        show(get_defaulttimer())
        result
    end
end
@KristofferC
Copy link
Owner

While cute, I feel that this is something you can quite easily make your own little wrapper for if you have the need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants