Profiling.sampling – Statistical Profiler

(docs.python.org)

46 points | by djoldman 2 days ago

4 comments

  • wongarsu 1 hour ago
    What I like about profiling.tracing is how you can control it in the same code base that you are profiling. That turns tracing into a feature you can ship, not just a tool you can attach. Profiling.sampling doesn't seem to provide anything similar, it has to run as a separate process
    • boguscoder 29 minutes ago
      It’s not a downside perhaps but a tradeoff. Implementing internal sampling profiler is not impossible in general (though it will depend on exact python implementation) but it would impose different overhead on the target and might skew results a lot more than external one.
  • modeless 55 minutes ago
    This sounds a lot like py-spy. How does it compare?
    • benfrederickson 15 minutes ago
      While there are some implementation differences (py-spy is written in rust, profiling.sampling is a mix of python and C etc), the end result seems pretty similar to me.

      One thing to note is that there are some differences in blocking behaviour of the target process. Py-spy blocks by default and profiling.sampling doesn't. I wrote a bit about why py-spy blocks by default here https://www.benfrederickson.com/why-python-needs-paused-duri... - the first version of py-spy also didn't block and since we got incredibly misleading results at times this was one of the first changes I made to py-spy

  • thomasmg 45 minutes ago
    I'm not trying to criticize, but Python is known to be much slower than eg. Java or Go etc. So for performance-critcal code, why use Python? I find Python to be very good because it is concise and simple, but I have not used it for production so far.
    • hansvm 0 minutes ago
      You use Python when it makes sense for other reasons (library support, coworker familiarity, etc), same as for any other project. Additionally, sometimes performance matters, but perhaps not enough to overcome whatever else is drawing you to Python in the first place.

      Right this second I'm writing something in Python with critical performance requirements. It needs to average processing 25k things per second. That won't be particularly hard, but it's close enough to the edge of what the language is capable of that I do need to be at least a tiny bit careful with the implementation. I'm highly unlikely to need a profiler for this project in particular, but earlier in my career I probably would have needed one.

      Python is fairly commonly used as a glue engine around faster code too, and it's not always obvious when the wrapper code is inducing nontrivial overhead (hidden copies and that sort of thing). Profilers are great for teasing out those sorts of problems. They shine a spotlight on the section of code which should take 0us and is instead dominating your runtime.

    • boguscoder 37 minutes ago
      Even when performance is not critical it’s possible to write (or vibe?) disastrously slow code, so having profiler handy is always a plus. It might be a deciding factor between “we must rewrite it all in Rust” and “oh we added exponential complexity by accident” and save a ton of time
    • spenczar5 38 minutes ago
      Sometimes you get there by accident. You make a thing, it grows or is used in unexpected ways, now suddenly performance matters.

      Sometimes Python is just the language used in the domain. Lots of sciences live on Python because it is easy to teach to grad students and the package ecosystem is strong.

    • IshKebab 15 minutes ago
      Because your colleagues insist on it and now you're stuck with slow Python despite telling them that this is exactly what would happen...
  • krystalgamer 1 hour ago
    wiwm i'd imagine it has a 3rd party tool. very nice to get it out of the box in 3.15