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

Add @timeline macro? (macro to show file/line info with timing results) #44375

Closed
StevenWhitaker opened this issue Feb 28, 2022 · 4 comments
Closed

Comments

@StevenWhitaker
Copy link

#42431 adds a @showtime macro to show the expression being timed along with the timing results. Would it also be worth adding a @timeline macro? (Ideas for alternative names for the macro are also welcome.)

I generally find myself having timing statements scattered about my code (usually when debugging), so to me having file and line info is more useful than just showing the timed expression. With #42431 I can now manually annotate each timing statement with file/line info, but if I modify my code I have to manually update the line numbers.

I wrote my own @timeline macro here, but it would be more convenient if (an updated version of) it was in Base. (I had to copy-paste Julia's @time code to allow nested calls to @timeline to print properly, but with #42431 I think I can avoid that code duplication.) I also printed the file/line info in green to more easily distinguish between location and timing information, but that's not strictly necessary. (Would it be possible to do that without changing Base.time_print to use printstyled?)

When printing file info, there's also the question of whether to print the full path, or just the file name, or something in between. I included a depth argument in @timeline to allow the user to specify how far up the file path to display (default is to print just the file name for brevity).

I'd be happy to submit a PR for a @timeline macro if people think this is a good idea.

@fredrikekre
Copy link
Member

Perhaps this would make more sense as a feature for https://github.com/KristofferC/TimerOutputs.jl ? I don't think it currently keeps track of location info, but shouldn't be hard to add.

@StevenWhitaker
Copy link
Author

That's a possibility. I would prefer having it in Base because its implementation is very simple and very similar to @showtime, and I would mainly use it for quick debugging, whereas TimerOutputs.jl seems to be more for a more thorough timing analysis. Furthermore, when working with multiple interrelated packages it becomes more of a pain to use an external package than relying on Base functionality. (I would have to add TimerOutputs, add @timeit to wherever I need it, remove @timeit statements when done, rm TimerOutputs, and do this for each package I'm working with. Or maybe there's a better way? Even if there is, I often don't want the extra TimerOutputs.jl functionality anyway.)

@giordano
Copy link
Contributor

You can already show file:line, although it requires a little bit of manual work:

shell> cat /tmp/foo.jl
@time "$(@__FILE__):$(@__LINE__)" 1 + 1
@time "$(@__FILE__):$(@__LINE__)" sin(1)

julia> include("/tmp/foo.jl");
/tmp/foo.jl:1: 0.000000 seconds
/tmp/foo.jl:2: 0.000001 seconds

@StevenWhitaker
Copy link
Author

Ah, I hadn't thought about using those macros (because using them in this context wasn't possible until now). What I have to type is a bit more verbose than I would like (and literally every character requires the shift key on my keyboard 😂), but at least it's simple. The output does become a bit cumbersome if the file path is long, especially when there is compilation and GC time to report. But I think this solution will suffice for now.

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

3 participants