r/Python • • Aug 19 '17

IPython magic to profile and view your Python code as a heat map

https://github.com/csurfer/pyheatmagic
277 Upvotes

35 comments sorted by

15

u/[deleted] Aug 19 '17

This is awesome! IDEs often have this capability, but with only timeit in notebooks, I find myself having to drop back to an IDE more than I'd like for speed optimizations.

3

u/c5urf3r Aug 19 '17

Glad to be of help. Use it to the full extent and let me know how can I make it can be made better.

2

u/efilon Aug 19 '17

There's also the lprun magic, but OP's too looks prettier.

2

u/c5urf3r Aug 20 '17

Can you provide a link here? Suggestions and ideas are always welcome. In the end, the community as a whole should benefit that's the goal.

3

u/takluyver IPython, Py3, etc Aug 20 '17

lprun is part of the line_profiler package.

5

u/not_perfect_yet Aug 19 '17

Please add a more static picture, it's hard to make out what the heat map does in the gif.

10

u/c5urf3r Aug 19 '17

Personally, I think the demo gif is slow enough but the snapshot will only add to the understanding as you pointed out. Hence added the same as a link underneath the demo gif. Here is the snapshot link http://i.imgur.com/isxRNV0.png Enjoy!

2

u/Goldragon979 Aug 19 '17

If I may ask what does this line do?

b >>= 1

2

u/forfunc Aug 19 '17

It shifts the bits of b by 1 position to the right

3

u/takluyver IPython, Py3, etc Aug 19 '17

Which, for an integer, is equivalent to dividing it by two and rounding down - b //= 2 in Python. The bit-shift operation may be more efficient, though I'd guess that it doesn't make much difference for Python code.

9

u/[deleted] Aug 19 '17

If only we had a profiler to find out.

→ More replies (10)

3

u/stuaxo Aug 19 '17

Making the last frame of the gif stay static for a couple of seconds longer than it takes to read the heatmap would be good.

The first time I saw it, it disappeared just as my eyes were being drawn to it.

GIMP can edit gifs as layers, the duration is in the layer name.

3

u/c5urf3r Aug 19 '17

@stuaxo : There is a link right below the demo which has the snapshot link. Here it is if you missed it http://i.imgur.com/isxRNV0.png

1

u/stuaxo Aug 20 '17

Thanks for that, it's much clearer :)

6

u/ZestyTurtle Aug 19 '17

Could someone ELI5 the benefits of these metrics? Thanks

12

u/takluyver IPython, Py3, etc Aug 19 '17

Sometimes you want your program to run quicker. You could buy a faster computer to do it, but that's expensive, and it might not make much difference.

Maybe we can change the code instead so that it goes faster! How will we do that? First, we need to work out which bits of the code are slow. We call this 'profiling'. We run the code in another program which measures how long each part takes. The bits that take longest are the bits we should try to change.

2

u/c5urf3r Aug 20 '17

Cool definition!

2

u/ZestyTurtle Aug 20 '17

Thanks! Really great ELI5

3

u/chausies Aug 19 '17

Very nice. But for the record, people should know that line_profiler is a much more in-depth solution and works from the terminal for people who don't use any IDE's. That's what most everyone uses to profile and optimize their code.

I really like how the colors give an immediate at-a-glance idea of things, though. Great work!

2

u/c5urf3r Aug 20 '17

You are absolutely right. This is not advertised as a replacement to any profiler.

Profilers are quintessential tools which help us dig deep with respect to performance of the code. But not everyone knows how to use a profiler and make the best use out of it. Even people who do know how to use them sometime wish there was a way with quicker turn around time.

This tool makes it simple for people to get a good idea of areas of contention in their code without the need of digging through profiler output.

One can and should always use profilers like the one you pointed out to dig deep when in need but for most cases this tool helps in giving a good idea of code performance without much fuss.

1

u/dopef123 Aug 20 '17

Does pycharm do anything similar to this??

1

u/c5urf3r Aug 20 '17

Sadly I have always remained in the realm of IPython, BPython and Python interactive shell. So, I am not much help in answering this question.

1

u/[deleted] Aug 20 '17

The paid version has a profiler.

2

u/QQMF Aug 19 '17

Upvoted. I commented on the x-post in /r/programming.