Method for analyzing code performance, processing device and storage medium
Patent Information
- Application Number
- CN202211644684.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-20
- Publication Date
- 2026-10-30
- Estimated Expiration
- 2042-12-20
AI Technical Summary
[0016]以上方案,通过统计目标代码中各目标运算符的操作次数,来确定目标代码的总耗时。由于代码通常包括各种计算语句,而计算语句中又包括各种运算符以及对应的操作数,因此,统计出目标代码中各目标运算符的操作次数后,可以基于各目标运算符的操作次数,确定出运行这些目标运算符所消耗的总时间,从而确定出目标代码的总耗时,进而可以根据确定出的目标代码的总耗时,分析目标代码的性能,一定程度上提高了代码性能分析的准确性。
Smart Images

Figure CN115981660B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software development technology, and in particular to a method, processing device, and storage medium for analyzing code performance. Background Technology
[0002] After developing a piece of code, developers typically need to analyze its performance to determine the overhead of the processor when running the code, and then optimize and improve the code.
[0003] Accurate performance analysis of code can improve the efficiency and quality of code development to some extent. Therefore, how to accurately analyze code performance has become an urgent problem to be solved. Summary of the Invention
[0004] The main technical problem solved by this invention is to provide a method, processing device and storage medium for analyzing code performance, which can improve the accuracy of code performance analysis.
[0005] To address the aforementioned technical problems, this application provides a technical solution: a method for analyzing code performance, the method comprising: counting the number of operations of each target operator in the target code; determining the total execution time of the target code based on the number of operations of each target operator; and analyzing the performance of the target code based on the total execution time of the target code.
[0006] The step of counting the number of operations of each target operator in the target code includes: determining the data type corresponding to the operand of each target operator; and counting the number of operations of each target operator for each data type.
[0007] The target operators include unary operators. The step of counting the number of operations of each target operator for each data type includes: for each unary operator, counting the number of operations of the unary operator for each data type based on the occurrence of the operands of the unary operator for each data type.
[0008] The target operators include multi-ary operators. The step of counting the number of operations of each target operator for each data type includes: determining the reference operand corresponding to the multi-ary operator, wherein the reference operand is the operand with the highest data type priority among the multiple operands of the multi-ary operator; and for the multi-ary operator, counting the number of operations of the multi-ary operator for each data type based on the occurrence frequency of the reference operand corresponding to each data type of the multi-ary operator.
[0009] The step of determining the total time consumption of the target code based on the number of operations of each target operator includes: determining the operation time of each target operator for each data type based on the number of operations of each target operator for each data type; and taking the sum of the operation times of the target operators for each data type as the total time consumption of the target code.
[0010] The target operators include multiplication and addition operators. After counting the number of operations of each target operator in the target code, the method further includes: in response to determining that the calculation statement of the target code includes multiplication and addition operations, counting the number of operations of the multiplication and addition operations in the target code, subtracting the number of operations of the multiplication and addition operations from the number of operations of the multiplication operator to obtain the adjusted number of operations of the multiplication operator, and subtracting the number of operations of the multiplication and addition operations from the number of operations of the addition operator to obtain the adjusted number of operations of the addition operator; determining the total time consumption of the target code based on the number of operations of each target operator includes: determining the total time consumption of the target code based on the adjusted number of operations of each target operator and the number of operations of the multiplication and addition operations.
[0011] The determination that the calculation statement of the target code includes a multiplication-addition operation includes: storing the address of the result of the multiplication operation in the calculation statement, and the addresses of the two operands of the addition operation in the calculation statement; in response to one of the addresses of the two operands of the addition operation being the same as the address of the result of the multiplication operation, determining that the calculation statement includes the multiplication-addition operation.
[0012] The step of analyzing the performance of the target code based on the total time consumed by the target code includes: determining that the performance of the target code meets the requirements if the total time consumed by the target code is less than a set time threshold; or determining that the performance of the target code does not meet the requirements if the total time consumed by the target code is greater than or equal to the set time threshold.
[0013] Wherein, the target operator includes all operators in the target code; and / or, after counting the number of operations of each target operator in the target code, the method further includes: outputting statistical results, the statistical results including the number of operations of each target operator.
[0014] To solve the above-mentioned technical problems, another technical solution adopted in this application is: to provide a processing device, including a memory and a processor coupled to each other, wherein the memory stores program instructions; and the processor is used to execute the program instructions stored in the memory to implement the above-mentioned code performance analysis method.
[0015] To solve the above-mentioned technical problems, another technical solution adopted in this application is to provide a computer-readable storage medium for storing program instructions that can be executed to implement the above-mentioned code performance analysis method.
[0016] The above approach determines the total execution time of the target code by counting the number of operations performed by each target operator. Since code typically includes various calculation statements, which in turn contain various operators and their corresponding operands, counting the number of operations performed by each target operator allows us to determine the total time consumed by running these operators, thus determining the total execution time of the target code. Based on this total execution time, we can then analyze the performance of the target code, improving the accuracy of code performance analysis to some extent. Attached Figure Description
[0017] Figure 1 This is a flowchart illustrating an embodiment of the code performance analysis method provided in this application;
[0018] Figure 2 This is a flowchart illustrating another embodiment of the code performance analysis method provided in this application;
[0019] Figure 3 This is a schematic diagram of the framework of an embodiment of the code performance analysis device provided in this application;
[0020] Figure 4 This is a schematic diagram of the framework of an embodiment of the processing device provided in this application;
[0021] Figure 5 This is a schematic diagram of a framework of an embodiment of the computer-readable storage medium provided in this application. Detailed Implementation
[0022] To make the purpose, technical solution and effects of this application clearer and more explicit, the following describes this application in further detail with reference to the accompanying drawings and embodiments.
[0023] Please see Figure 1 , Figure 1 This is a flowchart illustrating an embodiment of the code performance analysis method provided in this application. The method can be executed by a computer device, which can be a device running the target code, or other devices with computing capabilities; this embodiment does not limit the scope of the method. It should be noted that if substantially the same result is obtained, the method of this invention is not necessarily identical. Figure 1 The illustrated process sequence is limited. For example... Figure 1 As shown, the method includes the following steps:
[0024] S101: Count the number of operations of each target operator in the target code.
[0025] It is understood that the target code in this embodiment can be a part of the code that implements a certain function in a project, or it can be all the code corresponding to a project. This embodiment does not make any specific limitations on this.
[0026] The target operator can be a unary operator or a multi-ary operator. Multi-ary operators can be binary operators or ternary operators. Unary operators have one operand, such as the logical NOT operator "!", the bitwise NOT operator "~", the increment operator "++", the decrement operator "--", the pointer operator "*", and the address-of operator "&". Binary operators have two operands, such as the addition operator "+", the subtraction operator "-", the multiplication operator "*", the division operator " / ", and the modulo operator "%". Ternary operators have three operands, such as the conditional operator "?:".
[0027] In some implementations, the number of operations performed by each target operator in the target code can be directly counted. Alternatively, the data types corresponding to the operands of each target operator can be determined first, and then the number of operations performed by each target operator in the target code for each data type can be counted.
[0028] It should be noted that, in this embodiment, the statistical target operators can be a subset of operators in the target code. For example, only the number of operations of operators whose operation time is greater than or equal to a preset operation time is counted (i.e., only the number of operations of operators that have a significant impact on code execution time is counted, while the number of operations of operators whose operation time is less than the preset operation time is not counted). The preset operation time is set according to actual needs. Alternatively, in this embodiment, to further improve the accuracy of the determined total execution time of the target code, the statistical target operators include all operators in the target code.
[0029] S102: Determine the total execution time of the target code based on the number of operations performed by each target operator.
[0030] Since code typically includes various computational statements, which in turn include operators and their corresponding operands, the sum of the operation times of each target operator in the target code can be obtained by counting the number of operations performed by each target operator. This sum of the operation times of the target operators in the target code can then be used as the total execution time of the target code.
[0031] Furthermore, in this embodiment, considering that the processor of a computer device may include hardware circuit units that implement certain specific operations, the number of operations for these specific operations can be counted separately. Since the number of operations for these specific operations includes the number of operations for some target operators, it is necessary to subtract the number of operations for some target operators from the count of operations for the specific operations to obtain the adjusted number of operations for the target operators. Then, based on the adjusted number of operations for each target operator and the number of operations for the specific operations, the total execution time of the target code is determined. In this embodiment, the specific operation can be a multiplication-addition operation. In other embodiments, the specific operation can be other operations, which are not specifically limited here.
[0032] S103: Analyze the performance of the target code based on the total execution time of the target code.
[0033] In this embodiment, the total execution time of the target code can be compared with a set time threshold to analyze the performance of the target code. For example, if the total execution time of the target code is less than the set time threshold, the performance of the target code is determined to meet the requirements. If the total execution time of the target code is greater than or equal to the set time threshold, the performance of the target code is determined to not meet the requirements and further optimization is needed. The set time threshold is determined according to the performance requirements of the target code.
[0034] In this embodiment, the total execution time of the target code is determined by counting the number of operations of each target operator in the target code. Since code typically includes various calculation statements, and these calculation statements contain various operators and their corresponding operands, counting the number of operations of each target operator in the target code allows us to determine the total time consumed by running these target operators, thereby determining the total execution time of the target code. Based on this determined total execution time, the performance of the target code can be analyzed, which improves the accuracy of code performance analysis to some extent.
[0035] Please see Figure 2 , Figure 2 This is a flowchart illustrating another embodiment of the code performance analysis method provided in this application. Figure 2 As shown, the method includes the following steps:
[0036] S201: Determine the data types of the operands of each target operator in the target code.
[0037] For details regarding the target code and target operators, please refer to step 101 above; detailed descriptions are omitted here.
[0038] Data types include byte, short, int, long, float, double, char, and boolean. The operands of each target operator correspond to one of the aforementioned eight data types.
[0039] In one implementation, the data type of the operands can be determined based on the variable definitions corresponding to the operands of the target operator. For example, in y = a + b, there are two operand variables a and b corresponding to the addition operator. The definitions of variables a and b are "int a = 1" and "float b = 1.0" respectively. From the definitions of variables a and b, it can be seen that the data type of variable a is integer and the data type of variable b is single-precision floating-point.
[0040] S202: Count the number of operations performed by each target operator on each data type.
[0041] Considering that for the same operator, the operation time may differ depending on the data type of its operands. For example, if variable 'a' is an integer and variable 'b' is a long integer, the operation time of 'a++' and 'b++' will be different. Therefore, in this embodiment, the data types of the operands of each target operator are first determined, and then the number of operations performed by each target operator on each data type is counted.
[0042] Furthermore, in this embodiment, when counting the number of operations performed by each target operator on each data type, the statistics are performed separately for unary operators and multi-ary operators.
[0043] In one implementation, each target operator in the target code includes a unary operator. Since a unary operator has only one operand, the number of operations performed by the unary operator on each data type can be calculated based on the number of times the operand corresponding to each data type appears.
[0044] For example, the target code includes multiple bitwise NOT operators, and the data types of the operands of these multiple bitwise NOT operators include integer and character types. The occurrence count of integer operands corresponding to the bitwise NOT operators in the target code is counted, and this count is taken as the number of bitwise NOT operations performed on integer types. Similarly, the occurrence count of character operands corresponding to the bitwise NOT operators in the target code is counted, and this count is taken as the number of bitwise NOT operations performed on character types.
[0045] In one implementation, the target operators in the target code include multi-ary operators. When the operands of a multi-ary operator have different data types, the computer device first converts the data types of the operands to the same data type according to their priority, and then executes the operation corresponding to that multi-ary operator. The converted data type is the one with the highest priority among the operands. The priority order of data types from low to high is: byte, short integer, character, integer, long integer, single-precision floating-point, and double-precision floating-point. Byte, short integer, and character types have the same priority.
[0046] Therefore, in this embodiment, to improve the accuracy of the count of the target operator's operations, the reference operands corresponding to the multi-ary operator are first determined; then, based on the occurrence frequency of the reference operands for each data type corresponding to the multi-ary operator, the count of operations performed by the multi-ary operator on each data type is calculated. The reference operand is the operand with the highest data type priority among the multiple operands of the multi-ary operator.
[0047] The following example illustrates how to count the number of addition operations performed on each data type in the target code.
[0048] First, determine the reference operand for each addition operator based on the data type precedence of its two operands. For example, in y = a + b, the two operands of the addition operator are a and b, where a is an integer and b is a single-precision floating-point number. Since single-precision floating-point numbers have higher precedence than integers, the reference operand for the addition operator in y = a + b can be determined as b.
[0049] Then, based on the frequency of occurrences of the reference operands for each data type corresponding to the addition operator, the number of operations performed by the addition operator on each data type in the target code is counted. For example, if the data types of the reference operands for the addition operator in the target code are determined to be integers and single-precision floating-point numbers using the above method, then the frequency of occurrences of the integer reference operands for the addition operator in the target code is counted, and this count is taken as the number of operations performed by the addition operator on integers. Similarly, the frequency of occurrences of the single-precision floating-point reference operands for the addition operator in the target code is counted, and this count is taken as the number of operations performed by the addition operator on single-precision floating-point numbers.
[0050] Furthermore, to facilitate the counting of the number of operations performed by each target operator on each data type, statistical variables can be set for the number of operations performed by each target operator on each data type, and the statistical variables for each target operator on each data type can be stored in the same statistical stack.
[0051] Furthermore, to facilitate the counting of operations performed by each target operator on each data type, a statistical function for the number of operations on each data type can be defined. The statistical function for each data type includes a statistical variable for the number of operations performed on that data type and a statistical algorithm.
[0052] Considering that the bit width of different data types may vary on platforms with different bit widths, in order to facilitate the portability of the target code to different platforms, we use generic data type names to define the data types in the target code. For example, we define the character type as S08, the short integer type as S16, and the unsigned integer type as U32.
[0053] In steps S201 and S202 above, the data types corresponding to the operands of each target operator in the target code are first determined, and then the number of operations performed by each target operator on each data type is counted. Since the time consumed in executing the operation corresponding to the same operator may differ depending on the data type of the operands, using this method to count the number of operations of the target operators makes the final total execution time of the target code more accurate.
[0054] Optionally, steps S201 and S202 can be replaced by directly counting the number of operations of each target operator in the target code, without classifying the data types of the operands of the target operators. For example, if the target code includes addition and multiplication operators, the number of operations of the addition operator and the number of operations of the multiplication operator can be counted.
[0055] Optionally, to improve the efficiency of computer device code execution, the processor of the computer device is usually equipped with a hardware circuit unit (multiplier accumulator) that implements multiply-accumulate (MAC) operations. Therefore, in this embodiment, the multiply-accumulate operations in the target code can also be counted separately. After executing step S202, the method may further include: responding to the fact that the calculation statement in the target code includes multiply-accumulate operations, counting the number of multiply-accumulate operations in the target code, subtracting the number of multiply-accumulate operations from the number of multiply operations to obtain the adjusted number of multiply operations, and subtracting the number of multiply-accumulate operations from the number of addition operations to obtain the adjusted number of addition operations.
[0056] Since the counts of all multiplication operators and all addition operators in the target code were already counted in step S202, and a multiplication-addition operation (e.g., y = a*x + b or y = b + a*x) includes one multiplication operation and one addition operation, to improve the accuracy of the counts of the target operators, it is necessary to subtract the counts of multiplication-addition operators from the counts of multiplication operators, and subtract the counts of multiplication-addition operators from the counts of addition operators.
[0057] Specifically, determining whether a calculation statement in the target code includes a multiplication-addition operation involves: storing the address of the result of the multiplication operation in the calculation statement, and the addresses of the two operands of the addition operation in the calculation statement; if one of the addresses of the two operands of the addition operation is the same as the address of the result of the multiplication operation, the calculation statement is determined to include a multiplication-addition operation; or if the addresses of the two operands of the addition operation are not the same as the address of the result of the multiplication operation, the calculation statement is determined not to include a multiplication-addition operation.
[0058] The following example illustrates how to determine whether a calculation statement includes a multiplication-addition operation. The multiplication-addition operation includes two forms: y = a*x + b or y = b + a*x. Variable A stores the address of the result of the multiplication operation a*x, and variables B and C store the addresses of the left and right operands of the addition operation, respectively.
[0059] In one example, if the calculation statement includes a*x+b, then the address of the result of the multiplication operation a*x stored in variable A is the same as the address of the left operand a*x stored in variable B, indicating that the calculation statement includes a multiplication-addition operation. If the left operand of the addition operation in the calculation statement includes other additional operations besides the multiplication operation a*x, such as (a*x<<1)+b, then the address of the result of the multiplication operation a*x stored in variable A is different from the address of the left operand stored in variable B (the address of the result of a*x<<1), indicating that the calculation statement does not include a multiplication-addition operation.
[0060] In one example, if the calculation statement includes `b + a * x`, then the address of the result of the multiplication operation `a * x` stored in variable A is the same as the address of the right operand `a * x` stored in variable C, indicating that the calculation statement includes a multiplication-addition operation. If the right operand of the addition operation in the calculation statement includes other additional operations besides the multiplication operation `a * x`, such as `b + (a * x << 1)`, then the address of the result of the multiplication operation `a * x` stored in variable A is different from the address of the right operand stored in variable C (the address of the result of `a * x << 1`), indicating that the calculation statement does not include a multiplication-addition operation.
[0061] Specifically, the counting method for the number of operations in the multiplication-addition operation is the same as that for the addition operation, i.e., first determine the reference operand corresponding to the multiplication-addition operation; based on the occurrence frequency of the reference operand for each data type in the multiplication-addition operation, the number of operations for each data type in the multiplication-addition operation is counted. The reference operand corresponding to the multiplication-addition operation is the operand with the highest data type priority among the multiple operands in the addition operation. Related details can be found in step S202 regarding the counting method for multi-ary operators, and are omitted here in detail.
[0062] Furthermore, to facilitate the counting of multiplication-addition operations for each data type, separate statistical variables can be set for each data type, and these variables can be stored in the same statistical stack. When one of the addresses of the two operands in the addition operation is the same as the address of the result of the multiplication operation, it is determined that the calculation statement includes multiplication-addition. The corresponding statistical variable for multiplication-addition is incremented by 1, and the statistical variables for both addition and multiplication are decremented by 1. Alternatively, when neither of the addresses of the two operands in the addition operation is the same as the address of the result of the multiplication operation, it is determined that the calculation statement does not include multiplication-addition. The statistical stack for multiplication-addition is then cleared, preparing for the counting of the next target operator.
[0063] S203: Calculate the total execution time of the target code based on the number of operations performed by each target operator.
[0064] In this embodiment, the operation time of each target operator for each data type can be determined based on the number of operations performed by each target operator for each data type; the sum of the operation times of each target operator for each data type is taken as the total time consumption of the target code.
[0065] In one embodiment, the computer device's storage unit stores the operation time of each target operator for each type of operand. For example, the operation time for the reference operand of the addition operator when the data type is integer, single-precision floating-point, or long integer, etc. The computer device can directly obtain the operation time of each target operator for each type of operand from the storage unit, and then multiply the obtained operation time of each target operator for each type of operand by the number of operations performed by each target operator for each type of data to obtain the operation time of each target operator for each type of data. For example, multiplying the operation time of the addition operator for the reference operand of integer by the number of operations performed by the addition operator for integer yields the operation time of the addition operator for integer; multiplying the operation time of the addition operator for the reference operand of single-precision floating-point by the number of operations performed by the addition operator for single-precision floating-point yields the operation time of the addition operator for single-precision floating-point. Exemplarily, the operation time of each target operator for each type of operand stored in the computer device is an empirical value.
[0066] In one implementation, the operation time of each target operator for each data type is determined based on the number of operations performed by each target operator for each data type and the peak computing power of the device running the target code.
[0067] In a specific application, formula (1) is used to determine the operation time of each target operator for each data type. Formula (1) is as follows:
[0068]
[0069] In formula (1), y represents the operation time of each target operator for each data type. N represents the number of operations of each target operator for each data type, m represents the peak computing power of the device running the target code, and the coefficient 1000 is used to convert the unit of operation time of each target operator for each data type to milliseconds. The computer device's storage unit stores the peak computing power of the device running the target code, and the computer device can directly obtain the peak computing power of the device running the target code from the storage unit. Alternatively, the peak computing power of the device running the target code can be calculated using formula (2), as follows:
[0070] Y = N * f * M (2)
[0071] In formula (2), Y represents the peak computing power of the device running the target code, N represents the number of processors in the device running the target code, f represents the processor frequency of the device running the target code, and M represents the maximum number of floating-point operations (FMAD, multiply-accumulate instructions) that the device running the target code can execute per clock cycle. N, f, and M are set values, and the values of N, f, and M are stored in the memory units of the computer device. It should be noted that since the peak computing power of different devices may be the same or different, in this embodiment, the operation time of each target operator for each data type needs to be determined based on the peak computing power of the specific device running the target code.
[0072] Optionally, when the number of multiplication-addition operations is also counted in this embodiment, step S203 can be replaced by determining the total time consumption of the target code based on the adjusted number of operations of each target operator and the number of multiplication-addition operations. The adjusted operation time of each target operator for each data type, and the operation time of multiplication-addition operations for each data type, can be determined using the above-described implementation method, and will not be repeated here.
[0073] S204: Analyze the performance of the target code based on the total execution time of the target code.
[0074] In this embodiment, the total execution time of the target code is compared with a set time threshold to analyze the performance of the target code. If the total execution time of the target code is less than the set time threshold, the performance of the target code is determined to meet the requirements; conversely, if the total execution time of the target code is greater than or equal to the set time threshold, the performance of the target code is determined to fail to meet the requirements. The set time threshold is determined based on the actual performance requirements of the target code. For example, if the total execution time of the target code is less than the set time threshold, the performance of the target code is determined to meet the requirements and it can be ported to other platforms; if the total execution time of the target code is greater than or equal to the set time threshold, the performance of the target code is determined to fail to meet the requirements and it cannot be ported to other platforms, requiring further optimization.
[0075] Furthermore, to facilitate optimization of the target code by developers, this embodiment can output statistical results after counting the number of operations of each target operator in the target code, or after determining that the performance of the target code does not meet the requirements. The statistical results include the number of operations of each target operator, or the number of operations of each target operator for each data type. For example, the output format of the statistical results can be a table or other formats; this embodiment does not impose specific limitations.
[0076] In this embodiment, the total execution time of the target code can be determined by counting the number of operations performed by each target operator on each data type; alternatively, the total execution time of the target code can be determined by adjusting the number of operations performed by each target operator and the number of multiplication-addition operations. Since the time required to execute the corresponding operation may differ depending on the data type of the operands of the same operator, this method allows for a more accurate determination of the total execution time of the target code. This, in turn, enables more accurate performance analysis of the target code based on the precise total execution time, thus improving the accuracy of the target code analysis.
[0077] Please see Figure 3 , Figure 3 This is a schematic diagram of a framework of an embodiment of the code performance analysis device provided in this application. In this embodiment, the code performance analysis device 30 includes: a statistics module 31, a determination module 32, and an analysis module 33.
[0078] The statistics module 31 is used to count the number of operations of each target operator in the target code. The determination module 32 is used to determine the total execution time of the target code based on the number of operations of each target operator. The analysis module 33 is used to analyze the performance of the target code based on the total execution time of the target code.
[0079] Optionally, the statistics module 31 is used to determine the data type corresponding to the operands of each target operator; and to count the number of operations performed by each target operator on each data type.
[0080] Optionally, each target operator includes a unary operator. For each unary operator, the statistics module 31 is used to calculate the number of operations performed by the unary operator on each type of data based on the number of times the operands of the unary operator appear for each type of data.
[0081] Optionally, each target operator includes a multi-ary operator, and the statistics module 31 is used to determine the reference operand corresponding to the multi-ary operator. The reference operand is the operand with the highest data type priority among the multiple operands of the multi-ary operator. For the multi-ary operator, the number of operations performed by the multi-ary operator on each data type is calculated based on the number of occurrences of the reference operand corresponding to each data type of the multi-ary operator.
[0082] Optionally, the determining module 32 is used to determine the operation time of each target operator for each data type based on the number of operations of each target operator for each data type; and to use the sum of the operation times of the target operators for each data type as the total time consumption of the target code.
[0083] Optionally, the target operators include multiplication operators and addition operators. After counting the number of operations of each target operator in the target code, the statistics module 31 is further configured to, in response to determining that the calculation statement of the target code includes multiplication and addition operations, count the number of operations of the multiplication and addition operations in the target code, subtract the number of operations of the multiplication and addition operations from the number of operations of the multiplication and addition operations to obtain the adjusted number of operations of the addition operations; the determination module 32 is configured to determine the total time consumption of the target code based on the adjusted number of operations of each target operator and the number of operations of the multiplication and addition operations.
[0084] Optionally, the statistics module 31 is used to store the address of the multiplication result in the calculation statement and the addresses of the two operands of the addition operation in the calculation statement; in response to the fact that one of the addresses of the two operands of the addition operation is the same as the address of the multiplication result, it is determined that the calculation statement includes the multiplication-addition operation.
[0085] Optionally, the analysis module 33 is configured to determine that the performance of the target code meets the requirements in response to the total time consumption of the target code being less than a set time threshold; or, in response to the total time consumption of the target code being greater than or equal to the set time threshold, determine that the performance of the target code does not meet the requirements.
[0086] Optionally, the target operator includes all operators in the target code; and / or, the statistics module 31 is further configured to output statistical results after counting the number of operations of each target operator in the target code, the statistical results including the number of operations of each target operator.
[0087] It should be noted that the apparatus of this embodiment can perform the steps in the above method. For a detailed description of the relevant content, please refer to the method section above, which will not be repeated here.
[0088] Please see Figure 4 , Figure 4 This is a schematic diagram of a framework of an embodiment of the processing device provided in this application. In this embodiment, the processing device 40 includes a memory 41 and a processor 42.
[0089] Processor 42 can also be referred to as CPU (Central Processing Unit). Processor 42 may be an integrated circuit chip with signal processing capabilities. Processor 42 can also be a general-purpose processor, digital signal processor (DSP), application-specific integrated circuit (ASIC), field-programmable gate array (FPGA), or other programmable logic device, discrete gate or transistor logic device, or discrete hardware component. A general-purpose processor can be a microprocessor, or processor 42 can be any conventional processor 42, etc.
[0090] The memory 41 in the processing device 40 is used to store the program instructions required for the processor 42 to run.
[0091] The processor 42 is used to execute program instructions to implement the code performance analysis method in this application.
[0092] Please see Figure 5 , Figure 5 This is a schematic diagram of a framework of an embodiment of the computer-readable storage medium provided in this application. The computer-readable storage medium 50 of this embodiment stores program instructions 51, which, when executed, implement the code performance analysis method provided in this application. The program instructions 51 can form a program file and be stored in the aforementioned computer-readable storage medium 50 in the form of a software product, so that a computer device (which may be a personal computer, server, or network device, etc.) can execute all or part of the steps of the methods of various embodiments of this application. The aforementioned computer-readable storage medium 50 includes various media capable of storing program code, such as a USB flash drive, portable hard drive, read-only memory (ROM), random access memory (RAM), magnetic disk, or optical disk, or terminal devices such as computers, servers, mobile phones, and tablets.
[0093] The above approach determines the total execution time of the target code by counting the number of operations performed by each target operator. Since code typically includes various calculation statements, which in turn contain various operators and their corresponding operands, counting the number of operations performed by each target operator allows us to determine the total time consumed by running these operators, thus determining the total execution time of the target code. Based on this total execution time, we can then analyze the performance of the target code, improving the accuracy of code performance analysis to some extent.
[0094] In some embodiments, the functions or modules of the apparatus provided in this disclosure can be used to perform the methods described in the above method embodiments. The specific implementation can be referred to the description of the above method embodiments, and for the sake of brevity, it will not be repeated here.
[0095] The description of the various embodiments above tends to emphasize the differences between the various embodiments. The similarities or similarities between them can be referred to, and for the sake of brevity, they will not be repeated here.
[0096] In the several embodiments provided in this application, it should be understood that the disclosed methods, apparatuses, and systems can be implemented in other ways. For example, the apparatus implementations described above are merely illustrative. For instance, the division of modules or units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection of apparatuses or units may be electrical, mechanical, or other forms.
[0097] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment, depending on actual needs.
[0098] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0099] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute all or part of the steps of the methods of various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0100] The above description is merely an embodiment of this application and does not limit the patent scope of this application. Any equivalent structural or procedural transformations made using the content of this application's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the patent protection scope of this application.
Claims
1. A method for analyzing code performance, characterized in that, The method includes: Count the number of operations performed by each target operator in the target code; The total time consumption of the target code is determined based on the number of operations performed by each target operator. Analyze the performance of the target code based on its total execution time; The number of operations performed by each target operator in the statistical target code includes: Determine the data types corresponding to the operands of each target operator; Count the number of operations performed by each of the target operators on each of the data types; The target operators include the multiplication operator and the addition operator. After counting the number of operations of each target operator in the target code, the method further includes: In response to determining that the calculation statement of the target code includes a multiplication-addition operation, the number of operations of the multiplication-addition operation in the target code is counted, and the number of operations of the multiplication operator is subtracted from the number of operations of the multiplication-addition operation to obtain the adjusted number of operations of the multiplication operator, and the number of operations of the addition operator is subtracted from the number of operations of the multiplication-addition operation to obtain the adjusted number of operations of the addition operator.
2. The method according to claim 1, characterized in that, Each target operator includes unary operators, and the counting of the number of operations performed by each target operator for each data type includes: For each unary operator, the number of operations performed by the unary operator on each data type is calculated based on the number of times the operand of the unary operator appears for each data type.
3. The method according to claim 1, characterized in that, The target operators include multi-ary operators, and the counting of the number of operations performed by each target operator on each data type includes: Determine the reference operand corresponding to the multi-ary operator, wherein the reference operand is the operand with the highest data type priority among the multiple operands of the multi-ary operator; For the multi-ary operator, the number of operations performed by the multi-ary operator on each data type is calculated based on the number of times the reference operands for each data type appear.
4. The method according to claim 1, characterized in that, The step of determining the total time consumption of the target code based on the number of operations of each target operator includes: The operation time of each target operator for each data type is determined based on the number of operations performed by each target operator for each data type. The total time consumption of the target code is the sum of the operation times of the target operators for each of the data types.
5. The method according to claim 1, characterized in that, The step of determining the total time consumption of the target code based on the number of operations of each target operator includes: The total time consumption of the target code is determined based on the adjusted number of operations of each target operator and the number of operations of the multiplication-addition operation.
6. The method according to claim 5, characterized in that, The calculation statement used to determine the target code includes multiplication and addition operations, including: Store the address of the result of the multiplication operation in the calculation statement, and the addresses of the two operands of the addition operation in the calculation statement; In response to the fact that one of the addresses of the two operands of the addition operation is the same as the address of the result of the multiplication operation, it is determined that the calculation statement includes the multiply-add operation.
7. The method according to claim 1, characterized in that, The step of analyzing the performance of the target code based on its total execution time includes: If the total execution time of the target code is less than a set time threshold, the performance of the target code is determined to meet the requirements; or... If the total execution time of the target code is greater than or equal to a set time threshold, it is determined that the performance of the target code does not meet the requirements.
8. The method according to claim 1, characterized in that, The target operator includes all operators in the target code; And / or, after counting the number of operations of each target operator in the target code, the method further includes: Output statistical results, which include the number of operations performed by each target operator.
9. A processing device, characterized in that, Including interconnected memory and processor, The memory stores program instructions; The processor is used to execute program instructions stored in the memory to implement the method according to any one of claims 1-8.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium is used to store program instructions that can be executed to implement the method of any one of claims 1-8.
