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