Python Performance Analysis--cprofile

Source: Internet
Author: User

The Python standard library provides three modules for analyzing program performance, Cprofile, profile and hotshot, plus an auxiliary module stats. These modules provide deterministic analysis of Python programs, as well as a report generation tool that allows users to quickly check and analyze results

CProfile: Based on the Lsprof C language implementation of the extended application, the operating cost is reasonable, suitable for the analysis of long-running programs, the recommended use of this module;

The results of using the Cprofile analysis can be output to the specified file, but the contents of the file are saved in binary mode and garbled when opened with a text editor. As a result, Python provides a pstats module to analyze the file contents of the Cprofile output. It supports a variety of forms of report output, the text interface is a more practical tool. Very simple to use: (can view official documents)

From OPENPYXL import Load_workbookdef do_work ():    wb = Load_workbook (filename=u ' province_city.xlsx ')    Sheetnames = Wb.get_sheet_names ()    ws = Wb.get_sheet_by_name (sheetnames[0])    result = {}    for Rx in range (2, WS . Max_row + 1):        province = Ws.cell (Row=rx, column=1). Value        citys = [Ws.cell (Row=rx, Column=col). Value for col in Ra Nge (2, Ws.max_column) if                 Ws.cell (Row=rx, Column=col). value]        print (province)        if province:            result[ Province] = citys    print (result) with    open (' Province_city.json ', ' W ', encoding= ' utf-8 ') as F:        Json.dump (Result, F, ensure_ascii=false) Cprofile.run (' Do_work () ', ' restats ') p = pstats. Stats (' Restats ') p.strip_dirs (). Sort_stats (' time '). Print_stats ()

  

Output Result:

Learning Technology Exchange Group: 226704167, willing to progress together with you!

Python Performance Analysis--cprofile

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.