{"id":329450,"date":"2023-05-29T16:34:00","date_gmt":"2023-05-29T16:34:00","guid":{"rendered":"http:\/\/itteacheritfreelance.hk\/wordpress\/?guid=d8aa21b0196913fadf422b8332c04181"},"modified":"2023-05-29T16:34:00","modified_gmt":"2023-05-29T16:34:00","slug":"the-python-language-summit-2023-towards-native-profiling-for-python","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/05\/29\/the-python-language-summit-2023-towards-native-profiling-for-python\/","title":{"rendered":"The Python Language Summit 2023: Towards Native Profiling for Python"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<div class=\"markdown-body container-fluid comment-inner comment-enabled\" data-hard-breaks=\"true\" id=\"doc\">Joannah Nanjekye came to the <a href=\"https:\/\/pyfound.blogspot.com\/2023\/05\/the-python-language-summit-2023_29.html\" rel=\"nofollow\" >Python Language Summit 2023<\/a> to discuss innovations by <a href=\"https:\/\/github.com\/plasma-umass\/scalene\">Scalene<\/a>, a sampling-based Python profiler that can distinguish between native code and Python code in its reports. After its initial release in late 2019, Scalene has become one of the most popular Python profiling tools. It has now been downloaded 500,000 times from PyPI.<\/div>\n<div class=\"markdown-body container-fluid comment-inner comment-enabled\" data-hard-breaks=\"true\" id=\"doc\"><\/div>\n<div class=\"markdown-body container-fluid comment-inner comment-enabled\" data-hard-breaks=\"true\" id=\"doc\"><\/div>\n<div class=\"markdown-body container-fluid comment-inner comment-enabled\" data-hard-breaks=\"true\" id=\"doc\"><\/div>\n<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"tr-caption-container\" style=\"margin-left: auto; margin-right: auto;\">\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><a href=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEi4ACb3kr0fWAbdjl0tzHpGzioUYGYX9wrXnK915nFqYIKZPU_-DRDuiT1RVqY9FUs4-C4mwMeEnoKWP284KUtqm2Y7O4MJTSrUIHEeptUZl4U_dB4Q6Q3j_7mWveS-ksCQTD662HbZW3tInYRVrKia0qFxl6VC-TDg3pIrv3qcv6totiI\/s1180\/image_2023-05-27_222414441.png\" style=\"margin-left: auto; margin-right: auto;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" data-original-height=\"480\" data-original-width=\"1180\" height=\"130\" src=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEi4ACb3kr0fWAbdjl0tzHpGzioUYGYX9wrXnK915nFqYIKZPU_-DRDuiT1RVqY9FUs4-C4mwMeEnoKWP284KUtqm2Y7O4MJTSrUIHEeptUZl4U_dB4Q6Q3j_7mWveS-ksCQTD662HbZW3tInYRVrKia0qFxl6VC-TDg3pIrv3qcv6totiI\/s320\/image_2023-05-27_222414441.png\" width=\"320\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td class=\"tr-caption\" style=\"text-align: center;\">The Scalene project logo<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"markdown-body container-fluid comment-inner comment-enabled\" data-hard-breaks=\"true\" id=\"doc\"><\/p>\n<p><span>A profiler is a tool that can monitor a program as it is running. Once the program has run, the profiler can provide a report analysing which lines of code were visited most often, which were the most expensive in terms of time spent, and which were the most expensive in terms of memory usage. Profilers can therefore be hugely useful tools for addressing performance issues in code. If you\u2019re unsure where your program is spending most of its time, it can be hard to optimise it.<\/span><\/p>\n<p><span>Profilers can be split into two broad categories: trace-based profilers and sampling-based profilers. Trace-based profilers work by intercepting each function call as your program is running and logging information about the time spent, memory usage, etc. Sampling-based profilers, meanwhile, take snapshots of your program at periodic intervals to monitor these things. A trace-based profiler has the advantage that it can provide a granular and precise level of detail about which lines of code were executed and when each function call finishes; this makes it ideal for use as a tool to monitor test coverage, for example. However, injecting tracing hooks into each function call can sometimes slow down a program and distort the analysis of where most time was spent. As a result, sampling-based profilers are sometimes preferred for profiling performance.<\/span><\/p>\n<p><span>Scalene is a sampling-based profiler, and aims to address the shortcomings of previous sampling-based profilers for Python. One of the key challenges sampling-based profilers have faced in the past has been accurately measuring the time Python programs spend in \u201cnative code\u201d.<\/span><\/p>\n<p><span><br \/><\/span><\/p>\n<table align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"tr-caption-container\" style=\"margin-left: auto; margin-right: auto;\">\n<tbody>\n<tr>\n<td style=\"text-align: center;\"><a href=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEghK9GiEpyTkxBrAoaC_bG_5ophtqVcxtc-70dyCa5Y2g35CoVdQiPjiTc2lr4x4UeRq6bJW-TwhfT0kDykcpTS3GZubW9BwrnqoWxnTHe5L5Vih6H-NsAkq_OrmCiWQyWTwG7u_2rQIH6mIsIHcRAtS2KHIyIrvofK1TIpIrSR-iJrOCw\/s1192\/Nanjekye%20slide%20screenshot.png\" style=\"margin-left: auto; margin-right: auto;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" data-original-height=\"791\" data-original-width=\"1192\" height=\"212\" src=\"https:\/\/blogger.googleusercontent.com\/img\/b\/R29vZ2xl\/AVvXsEghK9GiEpyTkxBrAoaC_bG_5ophtqVcxtc-70dyCa5Y2g35CoVdQiPjiTc2lr4x4UeRq6bJW-TwhfT0kDykcpTS3GZubW9BwrnqoWxnTHe5L5Vih6H-NsAkq_OrmCiWQyWTwG7u_2rQIH6mIsIHcRAtS2KHIyIrvofK1TIpIrSR-iJrOCw\/s320\/Nanjekye%20slide%20screenshot.png\" width=\"320\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td class=\"tr-caption\" style=\"text-align: center;\"><span style=\"text-align: left;\">Slide from Nanjekye\u2019s talk, illustrating sampling-based profiling<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/p>\n<h2 class=\"raw\">Handling the problem of native code<\/h2>\n<p><span>\u201cNative code\u201d, also sometimes referred to as \u201cmachine code\u201d, refers to code consisting of low-level instructions that can be interpreted directly by the hardware processor. Using extensions to Python written in <\/span><a href=\"https:\/\/en.wikipedia.org\/wiki\/C_(programming_language)\" rel=\"noopener\" ><span>C<\/span><\/a><span>, <\/span><a href=\"https:\/\/en.wikipedia.org\/wiki\/C%2B%2B\" rel=\"noopener\" ><span>C++<\/span><\/a><span> or <\/span><a href=\"https:\/\/www.rust-lang.org\/\" rel=\"noopener\" ><span>Rust<\/span><\/a><span> that will compile to native code \u2013 such as <\/span><a href=\"https:\/\/numpy.org\/\" rel=\"noopener\" ><span>NumPy<\/span><\/a><span>, <\/span><a href=\"https:\/\/scikit-learn.org\/stable\/\" rel=\"noopener\" ><span>scikit-learn<\/span><\/a><span>, and <\/span><a href=\"https:\/\/www.tensorflow.org\/\" rel=\"noopener\" ><span>TensorFlow<\/span><\/a><span> \u2013 can lead to dramatic speedups for a program written in Python.<\/span><\/p>\n<p><span>It also, however, makes life difficult for sampling-based profilers. Samplers often use Python\u2019s <\/span><a href=\"https:\/\/docs.python.org\/3\/library\/signal.html\" rel=\"noopener\" ><code>signal<\/code><\/a><span> module as a way of knowing when to take a periodic snapshot of a program as it is running. However, due to the way the <\/span><code>signal<\/code><span> module works, no signalling events will be delivered while a Python program is spending time in a function that has been compiled to native code via an extension module. The upshot of this is that sample-based profilers are often \u201cflying blind\u201d for Python code that makes extensive use of C extensions, and will sometimes erroneously report that no time at all was spent executing native code, even if the program in fact spent the majority of its time there.<\/span><\/p>\n<p><span>Scalene\u2019s solution to this problem is to monitor delays in signal delivery. It uses this information to deduce the amount of time that the program spent outside CPython\u2019s main interpreter loop (due to the use of native, compiled code from an extension module). Further details on Scalene\u2019s methods, and comparisons with other leading Python profilers, can be found in a recent paper by Emery D. Berger, Sam Stern and Juan Altmayer Pizzorno, <\/span><a href=\"https:\/\/arxiv.org\/pdf\/2212.07597.pdf\" rel=\"noopener\" ><span>\u201cTriangulating Python Performance Issues with Scalene\u201d<\/span><\/a><span>.<\/span><\/p>\n<p><span>Nanjekye also detailed Scalene\u2019s sophisticated approach to measuring performance in child threads. Signal-based profilers often struggle with multi-threaded code, as signals can only be delivered and received from the main thread in Python. Scalene\u2019s solution is to monkey-patch functions that might block the main thread, and add timeouts to these functions. This allows signals to be delivered even in multithreaded code.<\/span><\/p>\n<h2 class=\"raw\"><span>Discussion<\/span><\/h2>\n<p><span>Nanjekye asked attendees at the Language Summit if they would be interested in integrating Scalene&#8217;s ideas into the standard library&#8217;s <code><a href=\"https:\/\/docs.python.org\/3\/library\/profile.html\" rel=\"nofollow\" >cProfile<\/a><\/code> module, which was met with a somewhat muted response.<\/span><\/p>\n<p><span>Pablo Galindo Salgado, a leading contributor to the <\/span><a href=\"https:\/\/github.com\/bloomberg\/memray\" rel=\"noopener\" ><span>Memray<\/span><\/a><span> profiler, criticised Scalene\u2019s signal-based appr<\/span>oach, arguing it relied on inherently brittle monkey-patching of the standard library. It also reported unreliable timings, Salgado said: for example, if code in a C extension checks for signals to support CTRL-C, the resulting delays measured by Scalene will be distorted.<\/p>\n<p><span>Salgado argued that integration with&nbsp;<\/span><a href=\"https:\/\/docs.python.org\/3.12\/howto\/perf_profiling.html#perf-profiling\" rel=\"noopener\" ><span>the <\/span><code>perf<\/code><span> profiler<\/span><\/a><span>, which Python is introducing support for in Python 3.12, would be a better option for users. Mark Shannon, however, argued that <\/span><code>perf<\/code><span> distorted the execution time of Python programs; Salgado responded that Scalene did as well, as the use of signals came with its own overhead.<\/span><\/p>\n<p><span>Nanjekye argued that the huge popularity of Scalene in the Python ecosystem was evidence that it had proved its worth. Carol Willing concurred, noting that Scalene was an especially useful tool with code that made heavy use of libraries such as NumPy, Scikit-Learn and PyTorch.<\/span><\/p>\n<\/div>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Joannah Nanjekye came to the Python Language Summit 2023 to discuss innovations by Scalene, a sampling-based Python profiler that can distinguish between native code and Python code in its reports. After its initial release in late 2019, Scalene has b&#8230;<\/p>\n<\/div>","protected":false},"author":2053,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"The Python Language Summit 2023: Towards Native Profiling for Python - ITTeacherITFreelance.hk","description":"Joannah Nanjekye came to the Python Language Summit 2023 to discuss innovations by Scalene, a sampling-based Python profiler that can distinguish between native"},"footnotes":""},"categories":[10700],"tags":[10798],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329450"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2053"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329450"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329450\/revisions"}],"predecessor-version":[{"id":329451,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329450\/revisions\/329451"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}