in ,

emeryberger / scalene, Hacker News

emeryberger / scalene, Hacker News


                    

        

by


Emery Berger

Scalene is a high-performance CPUandmemory profiler for Python that does a few things that other Python profilers do not and cannot do. It runs orders of magnitude faster than other profilers while delivering far more detailed information.

Scalene is

    fast. It uses sampling instead of instrumentation or relying on Python’s tracing facilities. Its overhead is typically no more than – (******************************************************************************************************************************% (and often less).

      Scalene isprecise. Unlike most other Python profilers, Scalene performs CPU profiling


      at the line level, pointing to the specific lines of code that are responsible for the execution time in your program. This level of detail can be much more useful information than the function-level profiles returned by most profilers.

          Scalene


          profiles memory usage. In addition to tracking CPU usage, Scalene also points to the specific lines of code responsible for memory growth. It accomplishes this via an included specialized memory allocator.

                (****************************************

                Installation

                Scalene is distributed as apip package. You can install it as follows:


                **************% pip install scalene****************************


                NOTE


                : Currently, installing Scalene in this way does not install its memory profiling library, so you will only be able to use it to perform CPU profiling. To take advantage of its memory profiling capability, you will need to download this repository.

                The following command will run Scalene to only perform line-level CPU profiling on a provided example program.


                % python -m scalene test / testme.py****************************

                To perform both line-level CPU and memory profiling, you first need to build the specialized memory allocator by running (make:


                **************% make****************************

                Profiling on a Mac OS X system:


                % DYLD_INSERT_LIBRARIES=$ PWD / libscalene.dylib PYTHONMALLOC=malloc python -m scalene test / testme.py****************************

                Profiling on a Linux system:


                % LD_PRELOAD=$ PWD / libscalene.so PYTHONMALLOC=malloc python -m scalene test / testme.py****************************

                ******************** Performance and Features


                Below is a table comparing various profilers to scalene, running on an example Python program (, by Gorelick and Ozsvald. All of these were run on a MacBook Pro.

                **********************

                Time (seconds)

                Slowdownscalene****************************

                Line-level?

                CPU?

                Memory?
                Unmodified code? (************************************** (**************************************original program(7.) ************************************************************************************************** s


                **************************** (1.) x


                ************************ (************************************** (****************************************** (************************************** (******************************************cProfile******************************(***************************************************************************************************************************************. ****************************************************************************************************************************** (s) 1). (x) function-level ✔️

                ✔️ (************************************ Profile**************************************************************************************************************************************** (s) 35 ***************************************************************************** (x) function-level ✔️ ✔️ (************************************ yappi****************************************************************************************************************** (s) ****************************************

                  ************************************************************************************ (x) ******************************************function-level ✔️

                ✔️ (************************************ line_profiler****************************************(*****************************************************************************************. *********************************************************************************************************************** (s) (***************************************************************************************************************************************. ***************************************************************************************************************************************** (x) **************************************** ✔️ ✔️ (********************************** needs @ profile (decorators) (************************************ memory_profileraborted after (minutes) >(x) ✔️

                ✔️ needs @ profile (decorators) (************************************ (**************************************** (************************************** (******************************************scalene***********************************************(CPU only)(************************************** 8. **************************************************************************************************************** (s) (1.) ***************************************************************************************************************************************** (x) ********************************************** ✔️

                ✔️ ✔️ (************************************ (******************************************scalene***********************************************(CPU memory) 9). (s) **************************************** (1.) ******************************************************************************************************************************** (x) ******************************************** ✔️

                ✔️ ✔️ ✔️ (********************************************** (*********************


                ******************************************************** Output

                ********************

                Scalene prints annotated source code for the program being profiled and any modules it uses in the same directory or subdirectories. Here is a snippet frompystone.py, just using CPU profiling:


                benchmarks / pystone.py:% of CPU time=(******************************************************************************************. (% out of 3.) s.   Line | CPU% | benchmarks / pystone.py   [… lines omitted …]    141 0. 69% | def Proc1 (PtrParIn):    141 1. (**************************************************************************************% | PtrParIn.PtrComp=NextRecord=PtrGlb.copy ()    143 0. 69% | PtrParIn.IntComp=5    143 2. 64% | NextRecord.IntComp=PtrParIn.IntComp    145 0. 139% | NextRecord.PtrComp=PtrParIn.PtrComp    145 2. 137% | NextRecord.PtrComp=Proc3 (NextRecord.PtrComp)    145 0. 69% | if NextRecord.Discr==Ident1:    145 0. 41% | NextRecord.IntComp=6    147 3. 31% | NextRecord.EnumComp=Proc6 (PtrParIn.EnumComp)    147 0. 41% | NextRecord.PtrComp=PtrGlb.PtrComp    149 2. (**********************************************************************************************************************% | NextRecord.IntComp=Proc7 (NextRecord.IntComp, 11)    149 | else:    278 | PtrParIn=NextRecord.copy ()    278 0. 41% | NextRecord.PtrComp=None    823 | return PtrParIn****************************

                And here is an example with memory profiling enabled, running the Julia benchmark.


                benchmarks / julia1_nopil.py:% of CPU time=(************************************************************************************************************************** (% out of 9.) **************************************************************************************************************************************** s.   Line | CPU% | Memory (MB) | benchmarks / julia1_nopil.py      1 | | | # Pasted from Chapter 2, High Performance Python – O’Reilly Media;      2 | | | # minor modifications for Python 3 by Emery Berger      3 | | |      4 | | | “Julia set generator without optional PIL-based image drawing”      5 | | | import time      6 | | | # area of ​​complex space to investigate      7 | | | x1, x2, y1, y2=-1.8, 1.8, -1.8, 1.8      8 | | | c_real, c_imag=-0. (**********************************************************, -. 01575879      9 | | |     13 | | # @ profile     15 | 0. 15 def calculate_z_serial_purepython (maxiter, zs, cs):     15 | | “Calculate output list using Julia update rule”     17 | 0. 15 output=[0] * len (zs)     17 0. 44% |

                for i in range (len (zs)):     19 | | n=0     19 0. (**************************************************************************************************************************% | – z=zs [i]     19 0. (**************************************************************************************************************% | –

                c=cs [i]     19 (********************************************************************************************************************************. ******************************************************************************************% | | while abs (z)************************************************************************************************************************% | – (****************************************************************. 06 n =1     23 0. (**************************************************************************************************************************% | | output [i]=n     23 | | return output****************************

                Positive memory numbers indicate memory consumption in megabytes; negative memory numbers indicate memory reclamation. Note that because of the way Python’s memory management works, frequent allocation and de-allocation (as in lines – 21 above) show up as high positive memory on one line followed by an (approximately) corresponding negative memory on the following line (s).

                Logo created by Sophia Berger

                   (****************************************************************************************

                Read More

                **********************************

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Trump's Manic Stock Market Gaffe Completely Dumbfounds Twitter, Crypto Coins News

Trump's Manic Stock Market Gaffe Completely Dumbfounds Twitter, Crypto Coins News

Dealmaster: Fast-charge your iPhone with this $ 12 Anker Lightning cable, Ars Technica

Dealmaster: Fast-charge your iPhone with this $ 12 Anker Lightning cable, Ars Technica