Code segment time consumption calculation method and device, computer equipment and storage medium
By distinguishing the execution type of code segments and selecting appropriate time-consuming calculation strategies, the existing tool has solved the problems of single functions, inaccurate statistics and cumbersome operations, and the accurate time-consuming analysis and code optimization of the panoramic image performance of smart travel is achieved.
Patent Information
- Application Number
- CN202411043853.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-31
- Publication Date
- 2024-12-03
AI Technical Summary
The existing time-consuming statistics tool has single functions, inaccurate statistics, long initialization time, and cumbersome operations when statistics are performed at multiple exit points, making it difficult to meet the complex needs of smart travel panoramic image performance analysis.
By obtaining the execution type of the target code segment, based on the correspondence between the preset execution type and the time-consuming calculation strategy, the execution time period is determined, and different strategies are used for time-consuming calculation, including single execution and multiple execution processing, simplifying the user Operation to improve the convenience and efficiency of the tool.
It realizes accurate statistics on the time-consuming code segments, improves the versatility and practicality of the tool, supports performance analysis and code optimization, simplifies user operations, and enhances the quality and stability of the code.
Smart Images

Figure CN119065964A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and device for calculating the time consumption of a code segment, a computer device and a storage medium. Background Art
[0002] As the trend of automobile intelligence becomes increasingly prominent, the panoramic imaging function of smart travel is undergoing rapid iteration. In this process, the performance requirements of panoramic imaging are increasing day by day, and the analysis requirements involving receiving camera images, rendering time, code execution time, and abnormal time detection are also increasing. However, the existing time statistics tools seem to be unable to cope with these complex requirements and have many shortcomings.
[0003] The deficiencies of the existing technology are mainly reflected in the following aspects: first, there is a lack of a comprehensive tool that can simultaneously count the image frame rate, code block time consumption, and provide timeout detection prompts; second, the accuracy of statistical time consumption needs to be improved, and the initialization time of the statistical tool itself is too long, which affects the efficiency of use; finally, when there are multiple exit points in the code to be counted, the existing statistical method requires adding statistical code at each exit point, which is cumbersome and error-prone. Summary of the invention
[0004] In view of this, an embodiment of the present invention provides a method, apparatus, computer device and storage medium for calculating the time consumption of a code segment to solve the problems of existing time consumption statistics tools having single functions, inaccurate statistics, long initialization time and cumbersome statistics of multiple exit points.
[0005] In a first aspect, an embodiment of the present invention provides a method for calculating the time consumption of a code segment, the method comprising:
[0006] Obtaining the execution type of the target code segment currently to be time-consumingly calculated, wherein the execution type includes single execution and multiple executions;
[0007] Based on the correspondence between the preset execution type and the preset time-consuming calculation strategy, determine the time-consuming calculation strategy corresponding to the execution type;
[0008] Determine, according to the execution type, an execution time period of the scope corresponding to the target code segment;
[0009] The time consumption of the execution time period is calculated based on the time consumption calculation strategy to obtain the time consumption result of the target code segment.
[0010] In an optional implementation manner of the present application, determining the execution time period of the scope corresponding to the target code segment according to the execution type includes:
[0011] If the execution type is single execution, when the target code segment starts to execute, a start timestamp is obtained through a constructor, and when the target code segment ends execution, an end timestamp is obtained through a destructor;
[0012] The execution time period is generated based on the start timestamp and the end timestamp.
[0013] In an optional implementation manner of the present application, determining the execution time period of the scope corresponding to the target code segment according to the execution type includes:
[0014] If the execution type is multiple executions, and the execution requirement of the multiple executions is non-loop execution, then when the target code segment starts to execute, the character strings corresponding to the rendering start point and the rendering end point marks of each function in the target code segment are marked;
[0015] When the target code segment ends execution, the start timestamp and the end timestamp corresponding to each function in the target code segment are determined according to the character string to obtain the execution time period.
[0016] In an optional implementation manner of the present application, determining the execution time period of the scope corresponding to the target code segment according to the execution type includes:
[0017] If the execution type is multiple executions, and the execution requirement of the multiple executions is cyclic execution, then a preset time-consuming statistical cycle is obtained;
[0018] Generate a first timeline based on the time-consuming statistical period, and mark the character strings corresponding to the rendering start point and the rendering end point of each function in the first timeline in sequence;
[0019] If the rendering end point of the function in the first timeline exceeds the deadline of the first timeline, the rendering end point is used as the start time of the next timeline until the target code segment finishes executing;
[0020] The function corresponding to each time axis in the target code segment is determined according to the character string, and the start timestamp and the end timestamp corresponding to each function in the time axis are obtained to obtain the execution time period.
[0021] In an optional implementation of the present application, based on the correspondence between the preset execution type and the preset time-consuming calculation strategy, determining the time-consuming calculation strategy corresponding to the execution type includes:
[0022] If the execution type is a single execution, determining the time-consuming calculation strategy to be a first strategy;
[0023] Or, if the execution type is multiple executions, the time consumption statistical requirement of the target code segment is obtained. When the time consumption statistical requirement is non-cyclic execution, the time consumption calculation strategy is determined to be the second strategy. When the time consumption statistical requirement is cyclic execution, the time consumption calculation strategy is determined to be the third strategy.
[0024] In an optional implementation manner of the present application, performing time consumption calculation on the execution time period based on the time consumption calculation strategy to obtain the time consumption result of the target code segment includes:
[0025] If the time consumption calculation strategy is the first strategy or the second strategy, the execution time corresponding to each function in the target code segment is calculated according to the start timestamp and the end timestamp;
[0026] Determining whether the target code segment satisfies a first output condition;
[0027] If the first output condition is met, a first time consumption result is generated according to the execution time corresponding to each function in the target code segment.
[0028] In an optional implementation manner of the present application, performing time consumption calculation on the execution time period based on the time consumption calculation strategy to obtain the time consumption result of the target code segment includes:
[0029] If the time consumption calculation strategy is the third strategy, the execution time of the function in each time axis is calculated according to the start timestamp and the end timestamp to obtain a time consumption sequence;
[0030] Calculate the average time consumption, minimum time consumption and maximum time consumption of the function in each time axis according to the time consumption sequence;
[0031] Determine whether the current time axis meets the second output condition;
[0032] If the second output condition is met, a second time consumption result is generated according to the number of function calls, average time consumption, minimum time consumption and maximum time consumption in the current timeline.
[0033] In a second aspect, an embodiment of the present invention provides a time-consuming calculation device for a code segment, the device comprising:
[0034] An acquisition module, used to acquire the execution type of the target code segment to be currently subjected to time-consuming calculation, wherein the execution type includes single execution and multiple executions;
[0035] A first determination module, configured to determine a time-consuming calculation strategy corresponding to a preset execution type based on a correspondence between the preset execution type and the preset time-consuming calculation strategy;
[0036] A second determination module, configured to determine an execution time period of a scope corresponding to the target code segment according to the execution type;
[0037] A calculation module is used to perform time consumption calculation on the execution time period based on the time consumption calculation strategy to obtain a time consumption result of the target code segment.
[0038] In a third aspect, an embodiment of the present invention provides a computer device, comprising: a memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, and the processor executing the method of the first aspect or any corresponding embodiment thereof by executing the computer instructions.
[0039] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium having computer instructions stored thereon, the computer instructions being used to enable a computer to execute the method of the first aspect or any corresponding embodiment thereof.
[0040] The method provided in the embodiment of the present application has the following beneficial effects:
[0041] The method provided in the embodiment of the present application can distinguish between single-execution and multiple-execution code segments by obtaining the execution type of the target code segment to be time-consumingly calculated, providing a basis for the subsequent selection of an appropriate time-consuming calculation strategy. It is flexible and can adapt to the time-consuming statistical needs in different scenarios, thereby improving the versatility and practicality of the tool. Based on the correspondence between the preset execution type and the preset time-consuming calculation strategy, the time-consuming calculation strategy corresponding to the execution type can be automatically determined. This intelligent strategy selection mechanism simplifies user operations, allowing users to perform time-consuming calculations without manual configuration, thereby improving the ease of use and efficiency of the tool.
[0042] The method provided in the embodiment of the present application can accurately define the scope of time-consuming calculation by determining the execution time period of the corresponding scope of the target code segment, avoid the interference of irrelevant time periods, and ensure the accuracy and reliability of the time-consuming calculation results. At the same time, this precise scope definition also enables the present invention to better adapt to the needs of segmented time-consuming statistics. Based on the time-consuming calculation strategy, the execution time period is calculated, and the time-consuming result of the target code segment can be obtained. This calculation result not only provides strong support for performance analysis, but also can help developers to promptly discover and optimize performance bottlenecks in the code. In addition, through the time-consuming statistics tool provided by the present invention, developers can also easily perform timeout detection prompts on the code, further improving the quality and stability of the code. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] In order to more clearly illustrate the specific implementation methods of the present invention or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0044] Figure 1 is a flowchart of a method for calculating the time consumption of a code segment according to some embodiments of the present invention;
[0045] Figure 2 is a schematic diagram of a flow chart of single / multiple time-consuming calculations according to some embodiments of the present invention;
[0046] Figure 3 is a schematic diagram of a process flow of a cross-domain single time-consuming calculation according to some embodiments of the present invention;
[0047] Figure 4 is a schematic diagram of a process flow of time-consuming calculation across time periods according to some embodiments of the present invention;
[0048] Figure 5 is a structural block diagram of a time-consuming calculation device for a code segment according to an embodiment of the present invention;
[0049] Figure 6 It is a schematic diagram of the hardware structure of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION
[0050] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0051] According to an embodiment of the present invention, a time-consuming calculation method, apparatus, computer device and storage medium for a code segment are provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0052] In this embodiment, a time-consuming calculation method for a code segment is provided. Figure 1 is a flowchart of a method for calculating the time consumption of a code segment according to an embodiment of the present invention. Figure 1 As shown, the process includes the following steps:
[0053] Step S11, obtaining the execution type of the target code segment currently to be time-consumingly calculated, wherein the execution type includes single execution and multiple executions.
[0054] In an embodiment of the present application, before executing the time-consuming calculation, it is first necessary to clarify the execution mode of the target code segment, that is, whether it is a single execution or multiple executions. This is because different types of execution modes require different time-consuming calculation strategies. Specifically, a single execution refers to a code segment being executed only once, such as a single call to a specific function. In this case, the time-consuming calculation is relatively simple. It is only necessary to record the start timestamp when the code segment starts to execute, and the end timestamp when the code segment ends to record the end timestamp, and then calculate the difference between the two to obtain the time. Multiple executions refer to the code segment being executed multiple times, such as in a loop or a frequently called function. In this case, the time-consuming calculation is complex, and it is necessary to record the start and end timestamps of each execution, and it is necessary to perform further statistical analysis on these time-consuming data, such as calculating the average time consumption, the minimum time consumption, the maximum time consumption, etc.
[0055] Step S12: determining the time-consuming calculation strategy corresponding to the execution type based on the corresponding relationship between the preset execution type and the preset time-consuming calculation strategy.
[0056] In an embodiment of the present application, the time-consuming calculation strategy is determined according to the execution type (single or multiple times) of the target code segment and the preset corresponding relationship. The first strategy is adopted for single execution, that is, the start and end timestamps are obtained and the difference is calculated. When the execution is repeated multiple times and non-looping, the second strategy is adopted to record the start and end timestamps of each execution and perform statistical analysis. When the execution is repeated multiple times and looping, the third strategy is adopted to record the start and end timestamps of each loop iteration, and calculate the number of function calls, average time consumption, minimum time consumption and maximum time consumption.
[0057] In the embodiment of the present application, step S12 includes the following steps:
[0058] Step A1: If the execution type is single execution, determine that the time-consuming calculation strategy is the first strategy.
[0059] In the embodiment of the present application, single execution means that the code segment is executed only once, so the time calculation will simply involve obtaining the start and end timestamps and calculating the difference between the two to obtain the execution time of the code segment. The first strategy is applicable to scenarios where repeated execution is not required, or even if repeated execution is performed, only the first execution time needs to be calculated. For example, an initialization function only needs to be executed once when the program starts, and its time is calculated to ensure that the performance meets expectations.
[0060] Step A2, if the execution type is multiple executions, obtain the time consumption statistics requirement of the target code segment. When the time consumption statistics requirement is non-loop execution, determine the time consumption calculation strategy as the second strategy. When the time consumption statistics requirement is loop execution, determine the time consumption calculation strategy as the third strategy.
[0061] In an embodiment of the present application, for a code segment that is executed multiple times, if the time-consuming statistics requirement is non-cyclic execution, that is, there is no fixed loop mode, the second strategy is adopted. This strategy registers the starting point at the beginning of each call and the ending point at the end, and calculates and saves the time consumed for each call. Output statistical data when the conditions are met. If the time-consuming statistics requirement is cyclic execution, that is, there is a fixed loop mode, the third strategy is adopted. This strategy pre-sets the time-consuming statistics cycle (such as per second), generates a timeline, and marks the rendering start and end points of the function. Update the mark and record the time consumed when the function is executed. At the end of the cycle, the average time consumed, the minimum time consumed, the maximum time consumed, and the number of calls of the function in the timeline are calculated, and these statistical data are output when the output conditions are met.
[0062] Step S13, determining the execution time period of the scope corresponding to the target code segment according to the execution type.
[0063] In the embodiment of the present application, the execution time period refers to the execution time range of the code segment from the beginning to the end. For a code segment executed once, the start and end timestamps are obtained by the constructor and destructor respectively to form the execution time period. For a code segment that is executed multiple times but not cyclically, the start and end timestamps of each function are determined by marking the string to obtain the execution time period. For a code segment that is executed cyclically, a timeline is generated based on a preset time-consuming statistical cycle, and the start and end points of each function are marked on the timeline to track the function execution time period within each cycle.
[0064] In the embodiment of the present application, step S13 includes the following steps B1-B2:
[0065] Step B1, if the execution type is single execution, when the target code segment starts to execute, the start timestamp is obtained through the constructor, and when the target code segment ends to execute, the end timestamp is obtained through the destructor.
[0066] It should be noted that in C++ programming, constructors and destructors are two special member functions in a class. The constructor is a member function that is automatically called when an object is created, and its main task is to initialize the object. When a new object is created, the constructor is called immediately to ensure that the object has been properly set up before use. The destructor is a member function that is automatically called at the end of the object's life cycle. Its main task is to perform cleanup work, such as releasing the resources occupied by the object. When an object is deleted or the object's life cycle ends, the destructor will be called to ensure that the resources occupied by the object are properly released.
[0067] In the embodiment of the present application, this method utilizes the RAII (Resource Acquisition Is Initialization) mechanism of C++, that is, resource acquisition is initialization. By associating the life cycle of an object with the acquisition and release of resources, it is possible to ensure that resources are acquired when an object is created and released when the object is destroyed. In the time consumption statistics, this mechanism is used to ensure that the start timestamp is obtained when the code segment starts to execute, the end timestamp is obtained when the code segment is executed, and the time consumption is calculated.
[0068] Step B2: Generate an execution time period based on the start timestamp and the end timestamp.
[0069] In the embodiment of the present application, after obtaining the start and end timestamps, an execution time period is generated based on the two timestamps. The execution time period is a measure of the duration of code execution, which can be obtained by calculating the difference between the end timestamp and the start timestamp. This difference is the execution time of the target code segment, reflecting the time elapsed from the start to the end of the code execution.
[0070] In the embodiment of the present application, step S13 includes the following steps C1-C2:
[0071] Step C1, if the execution type is multiple executions, and the execution requirement of the multiple executions is non-loop execution, then when the target code segment starts to execute, the rendering start point and the rendering end point of each function in the target code segment are marked with corresponding character strings.
[0072] In the embodiment of the present application, a string marking method is used as a registration mechanism to track and identify the rendering start and end points of each function in the global data. These strings are used as unique identifiers and are associated with the time-consuming data of the function, so that the time-consuming information of the function can be obtained or updated at any time. When the function starts and ends, the start and end points marked with the same unique string are recorded respectively.
[0073] Step C2, when the target code segment ends execution, the start timestamp and the end timestamp corresponding to each function in the target code segment are determined according to the character string to obtain the execution time period.
[0074] In an embodiment of the present application, a string is used as a unique identifier and is associated with the time-consuming data of each function. During the code execution process, each time a function starts rendering, a starting point marked with a unique string is recorded, including its start timestamp. Similarly, when the function rendering is completed, an end point marked with the same string is also recorded, including its end timestamp. In this way, when the target code segment ends execution, the complete rendering time period of each function can be determined by looking up the string marker associated with each function. This time period is defined by the start timestamp and end timestamp of the function, which represents the time consumed during the execution of the function.
[0075] In the embodiment of the present application, step S13 includes the following steps D1-D4:
[0076] Step D1: If the execution type is multiple executions, and the execution requirement of the multiple executions is cyclic execution, a preset time-consuming statistical cycle is obtained.
[0077] In an embodiment of the present application, when it is determined that the execution type of the target code segment is multiple executions, and such multiple executions are cyclic executions, it is first necessary to obtain a preset time-consuming statistical cycle. The time-consuming statistical cycle defines the time range for each statistical calculation when performing time-consuming statistics. For example, if it is desired to count the number of calls and average time consumption of a function per second, the time-consuming statistical cycle is 1 second.
[0078] Step D2, generating a first time axis based on the time-consuming statistical period, and marking the character strings corresponding to the rendering start point and the rendering end point of each function in the first time axis in sequence.
[0079] In an embodiment of the present application, a timeline is constructed based on a preset time-consuming statistical cycle. The function of this timeline is to divide the time-consuming statistics of the target code segment executed in a loop into a series of continuous time periods, each of which corresponds to a complete time-consuming statistical cycle. For example, if it is desired to count the number of calls and the average time consumption of a function once per second, the time-consuming statistical cycle is set to 1 second, and each point on the timeline represents the beginning of a new second.
[0080] After the timeline is constructed, the rendering start and end points of each function are marked on it. This is achieved by assigning a unique string identifier to the rendering start and end points of each function, and associating these identifiers with specific time points on the timeline. In this way, in subsequent calculations, the execution time period of each function on the timeline can be accurately determined based on these identifiers, and the corresponding time consumption can be calculated accordingly.
[0081] Specifically, when the target code segment starts to execute, the rendering start point of each function is recorded and associated with the current time point on the timeline. When the function is executed, its rendering end point is also recorded and associated with the current time point on the timeline. In this way, by comparing the timestamps of the rendering start point and end point, the actual execution time of the function can be calculated.
[0082] Step D3: if the rendering end point of the function in the first timeline exceeds the deadline of the first timeline, the rendering end point is used as the start time of the next timeline until the target code segment finishes executing.
[0083] In an embodiment of the present application, the rendering start and end points of the function are monitored and compared with a preset time-consuming statistical period (such as per second). If the rendering end point of a function exceeds the deadline of the current timeline, the end point will be used as the start time of the next timeline, and the rendering start and end points of subsequent functions will continue to be marked on the new timeline. This process is particularly important for the target code segment that is executed in a loop, because it allows the system to continuously count and analyze the time consumed by the function during the execution of the entire code segment. In this way, the execution time consumed by each function in each time-consuming statistical period can be captured, thereby providing valuable data for performance analysis and optimization.
[0084] Step D4, determining the function corresponding to each time axis in the target code segment according to the character string, and obtaining the start timestamp and end timestamp corresponding to each function in the time axis to obtain the execution time period.
[0085] In an embodiment of the present application, the previously marked character string is matched with the data on the timeline to determine the function corresponding to each timeline. After determining the function corresponding to each timeline, the start timestamp and end timestamp of these functions in the timeline are obtained. Based on the obtained start timestamp and end timestamp, the execution time period of each function is generated. This time period represents the actual execution time of the function in the timeline and is the basis for subsequent time-consuming calculations. For functions executed across timelines, necessary state information is saved when the timeline is switched to ensure that the time-consuming calculation across timelines can be performed accurately.
[0086] Step S14, performing time consumption calculation on the execution time period based on the time consumption calculation strategy to obtain the time consumption result of the target code segment.
[0087] In the embodiment of the present application, according to the different time calculation strategies, two situations are handled: when the time calculation strategy is the first or second strategy (i.e., single execution or non-cyclic multiple executions), the execution time of each function is calculated (end timestamp minus start timestamp); determine whether the output conditions are met (such as time consumption exceeds the threshold or user request); if the conditions are met, generate a time result report containing function name, execution time and other information. When the time calculation strategy is the third strategy (i.e., multiple executions of cyclic execution), calculate the execution time of the function in each timeline to obtain a time sequence; calculate the average time, minimum time and maximum time according to the time sequence; determine whether the current timeline meets the output conditions (such as the end of the timeline or user request); if the conditions are met, generate a detailed report containing the number of function calls, average time, minimum time and maximum time.
[0088] In the embodiment of the present application, step S14 includes the following steps E1-E3:
[0089] Step E1, if the time consumption calculation strategy is the first strategy or the second strategy, the execution consumption corresponding to each function in the target code segment is calculated according to the start timestamp and the end timestamp.
[0090] In an embodiment of the present application, when the time-consuming calculation strategy is determined as the first strategy or the second strategy, based on the RAII mechanism of C++, the execution time corresponding to each function in the target code segment is calculated according to the start timestamp and the end timestamp. Specifically, the start timestamp is obtained in the constructor, the end timestamp is obtained in the destructor, and the execution time of the function is obtained by a simple subtraction operation. This design makes the time-consuming calculation simple and efficient, without the need for developers to manually add additional timing code, thereby reducing code intrusiveness and maintenance costs.
[0091] Step E2, determining whether the target code segment satisfies the first output condition.
[0092] In an embodiment of the present application, the first output condition can be set according to actual needs, for example: setting a time threshold, and outputting the time information only when the execution time of the target code segment exceeds a preset threshold; or setting a call count threshold, and outputting the time information only when the call count of the code segment exceeds this threshold; or, during the rendering process, outputting the time only when rendering the first frame or a specific frame; or, outputting the time information only when the execution time of the code segment exceeds the threshold and the call count exceeds the threshold.
[0093] Step E3: if the first output condition is met, a first time consumption result is generated according to the execution time consumption corresponding to each function in the target code segment.
[0094] In an embodiment of the present application, the first timing result may include: function name, which identifies the specific function in the code segment; execution time, which records the execution time of each function, that is, the time difference between the start and end of the function execution; execution times, for code segments that are executed multiple times, the execution times of each function will also be recorded. Other relevant information, such as the function call stack, memory usage during execution, etc. The purpose of generating the first timing result is to enable developers to clearly understand the execution performance of the code segment, so as to find performance bottlenecks and optimize them. For example, if the execution time of a function is particularly long, the developer can focus on analyzing the implementation of this function and obtain the corresponding optimization plan.
[0095] In the embodiment of the present application, step S14 includes the following steps F1-F3:
[0096] Step F1, if the time consumption calculation strategy is the third strategy, the execution time of the function in each time axis is calculated according to the start timestamp and the end timestamp to obtain the time consumption sequence.
[0097] In the embodiment of the present application, the time-consuming sequence is a data set arranged in chronological order, which records the execution time of all functions in each time axis. In actual operation, a start timestamp is recorded at the starting point of each time axis, and an end timestamp is recorded at the end point of the time axis. Subsequently, all functions in this time axis are traversed, and the execution time of each function is calculated according to their start timestamps and end timestamps, and these data are added to the time-consuming sequence.
[0098] Step F2, calculating the average time consumption, the minimum time consumption and the maximum time consumption of the function in each time axis according to the time consumption sequence.
[0099] In an embodiment of the present application, the average execution time is obtained by adding the execution time of the same function in each timeline and then dividing it by the number of calls of the function. This indicator represents the average execution time of a function in a specific time period, thereby evaluating its performance. The minimum execution time is the minimum value of the function execution time in each timeline, which represents the fastest execution of the function in the time period. By analyzing the minimum execution time, the best execution of the function and the optimization point can be found. The maximum execution time is the maximum value of the function execution time in each timeline, which represents the slowest execution of the function in the time period.
[0100] Step F3, determining whether the current time axis satisfies the second output condition.
[0101] In the embodiment of the present application, the second output condition can be that the time on the time axis reaches a certain threshold, such as outputting the time consumption result once per second; or it can be that the number of function calls on the time axis reaches a certain preset value, such as outputting the time consumption result once every 100 calls. The setting of the output condition depends on the user's demand for time consumption statistics.
[0102] Step F4: if the second output condition is met, a second time consumption result is generated according to the number of function calls, average time consumption, minimum time consumption and maximum time consumption in the current time axis.
[0103] In an embodiment of the present application, when it is determined that the current time axis satisfies the second output condition (for example, the time axis reaches a preset statistical period, such as per second), the number of function calls in the current time axis is first collected. Then, the average time consumption, minimum time consumption, and maximum time consumption of these function calls are calculated. Based on these data, a second time consumption result including the number of function calls, the average time consumption, the minimum time consumption, and the maximum time consumption is generated. This result is usually presented in a structured form. The time consumption result can clearly understand the performance of the code in a specific time period, so as to make targeted optimization decisions.
[0104] Figure 2 is a schematic diagram of a single / multiple time-consuming calculation process provided by an optional embodiment of the present invention, such as Figure 2 As shown, the process includes:
[0105] Step 201, placing a calculation tool in front of the code segment to be calculated;
[0106] Step 202, the constructor obtains the start timestamp;
[0107] Step 203, exit the scope, trigger the destructor, and obtain the end timestamp in the destructor;
[0108] Step 204, determining whether the calculation tool is a single calculation tool;
[0109] Step 205, if it is a single calculation tool, then execute step 206, if it is not a single calculation tool, then execute step 207;
[0110] Step 206, output the time consumption and end;
[0111] Step 207, determine whether the output flag is true, if so, execute step 208, if not, execute step 209;
[0112] Step 208, change the flag to false and end;
[0113] Step 209, end the process.
[0114] Figure 3is a schematic diagram of a process flow of a cross-domain single time-consuming calculation provided by an optional embodiment of the present invention, such as Figure 3 As shown, the process includes:
[0115] Step 301, a single cross-domain time-consuming calculation begins;
[0116] Step 302, registering the starting point by name at the starting place;
[0117] Step 303, registering the end point by using the same-name character string at the place where the statistics ends;
[0118] Step 304, globally search for registration information through the string;
[0119] Step 305, determine whether the output flag is true, if so, execute step 306, if not, execute step 307;
[0120] Step 306, determining whether the calculation tool is a single calculation tool, if so, executing step 307, if not, executing step 308;
[0121] Step 307, change the output flag to false;
[0122] Step 308: output the time consumption result.
[0123] Figure 4 is a schematic diagram of a process flow of time-consuming calculation across time periods provided by an optional embodiment of the present invention, such as Figure 4 As shown, the process includes:
[0124] Step 401, register the starting point by name at the starting place and count the intervals;
[0125] Step 402, determine whether the name is registered for the first time, if it is the first registration, execute step 403, if not, execute step 404;
[0126] Step 403, add the name to the global information and initialize the statistical time point to the current time;
[0127] Step 404, registering the end point with the same name string at the end of the statistics;
[0128] Step 405, obtaining the single time consumption;
[0129] Step 406, determine whether the current time and the last statistical time exceed the set time, if yes, execute step 407, if no, execute step 408;
[0130] Step 407, update the statistical time point to the current time, and clear the sequence;
[0131] Step 408, continue to monitor the current time;
[0132] Step 409, calculate the number of calls, average time consumption, minimum time consumption and maximum time consumption within this period of time through the time consumption sequence within this period of time, and output the time consumption result.
[0133] In the present embodiment, a time-consuming calculation device for a code segment is also provided, and the device is used to implement the above-mentioned embodiments and preferred implementation modes, and the descriptions that have been made are not repeated. As used below, the term "module" can implement a combination of software and / or hardware of a predetermined function. Although the device described in the following embodiments is preferably implemented in software, the implementation of hardware, or a combination of software and hardware is also possible and conceived.
[0134] This embodiment provides a time-consuming calculation device for a code segment, such as Figure 5 As shown, including:
[0135] An acquisition module 51 is used to acquire the execution type of the target code segment to be currently subjected to time-consuming calculation, wherein the execution type includes single execution and multiple executions;
[0136] A first determination module 52, configured to determine a time-consuming calculation strategy corresponding to an execution type based on a correspondence between a preset execution type and a preset time-consuming calculation strategy;
[0137] A second determination module 53, used to determine the execution time period of the scope corresponding to the target code segment according to the execution type;
[0138] The calculation module 54 is used to perform time consumption calculation on the execution time period based on the time consumption calculation strategy to obtain the time consumption result of the target code segment.
[0139] In an optional embodiment of the present application, the second determination module 53 is used to, if the execution type is a single execution, obtain a start timestamp through a constructor when the target code segment starts to execute, and obtain an end timestamp through a destructor when the target code segment ends execution; and generate an execution time period based on the start timestamp and the end timestamp.
[0140] In an optional embodiment of the present application, the second determination module 53 is used to mark the corresponding character strings of the rendering start point and the rendering end point of each function in the target code segment when the target code segment starts to execute if the execution type is multiple executions and the execution requirement of the multiple executions is non-loop execution; when the target code segment ends execution, the start timestamp and the end timestamp corresponding to each function in the target code segment are determined according to the character strings to obtain the execution time period.
[0141] In an optional embodiment of the present application, the second determination module 53 is used to obtain a preset time-consuming statistical period if the execution type is multiple executions and the execution requirement of the multiple executions is a loop execution; generate a first timeline based on the time-consuming statistical period, and mark the strings corresponding to the rendering start point and the rendering end point of each function in the first timeline in turn; if the rendering end point of the function in the first timeline exceeds the deadline of the first timeline, the rendering end point is used as the start time of the next timeline until the target code segment ends execution; determine the function corresponding to each timeline in the target code segment according to the string, and obtain the start timestamp and end timestamp corresponding to each function in the timeline to obtain the execution time period.
[0142] In an optional embodiment of the present application, the first determination module 52 is used to determine that the time calculation strategy is the first strategy if the execution type is a single execution; or, if the execution type is multiple executions, obtain the time statistics requirement of the target code segment, and when the time statistics requirement is a non-cyclic execution, determine the time calculation strategy to be the second strategy, and when the time statistics requirement is a cyclic execution, determine the time calculation strategy to be the third strategy.
[0143] In an optional embodiment of the present application, the calculation module 54 is used to calculate the execution time corresponding to each function in the target code segment according to the start timestamp and the end timestamp if the time calculation strategy is the first strategy or the second strategy; determine whether the target code segment meets the first output condition; if the first output condition is met, generate a first timing result according to the execution time corresponding to each function in the target code segment.
[0144] In an optional embodiment of the present application, the calculation module 54 is used to calculate the execution time of the function in each timeline according to the start timestamp and the end timestamp to obtain a time sequence if the time calculation strategy is the third strategy; calculate the average time, minimum time and maximum time of the function in each timeline according to the time sequence; determine whether the current timeline meets the second output condition; if the second output condition is met, generate a second time result according to the number of function calls, average time, minimum time and maximum time in the current timeline.
[0145] See also Figure 6 , Figure 6 is a schematic diagram of the structure of a computer device provided by an optional embodiment of the present invention, such as Figure 6As shown, the computer device includes: one or more processors 10, a memory 20, and interfaces for connecting various components, including high-speed interfaces and low-speed interfaces. Various components are connected to each other using different buses for communication, and can be installed on a common mainboard or installed in other ways as needed. The processor can process the instructions executed in the computer device, including instructions stored in or on the memory to display the graphical information of the GUI on an external input / output device (such as, a display device coupled to the interface). In some optional embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories and multiple memories. Similarly, multiple computer devices can be connected, and each device provides some necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system).
[0146] The processor 10 may be a central processing unit, a network processor or a combination thereof. The processor 10 may further include a hardware chip. The hardware chip may be a dedicated integrated circuit, a programmable logic device or a combination thereof. The programmable logic device may be a complex programmable logic device, a field programmable gate array, a general purpose array logic or any combination thereof.
[0147] The memory 20 stores instructions executable by at least one processor 10, so that at least one processor 10 executes the method shown in the above embodiment.
[0148] The memory 20 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created by the use of a computer device based on the presentation of a small program landing page, etc. In addition, the memory 20 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some optional embodiments, the memory 20 may optionally include a memory remotely arranged relative to the processor 10, and these remote memories may be connected to the computer device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0149] The memory 20 may include a volatile memory, such as a random access memory; the memory may also include a non-volatile memory, such as a flash memory, a hard disk or a solid state drive; the memory 20 may also include a combination of the above types of memory.
[0150] The computer device further comprises a communication interface 30 for the computer device to communicate with other devices or a communication network.
[0151] The embodiment of the present invention also provides a computer-readable storage medium. The method according to the embodiment of the present invention can be implemented in hardware, firmware, or can be implemented as a computer code that can be recorded in a storage medium, or can be implemented as a computer code that is originally stored in a remote storage medium or a non-temporary machine-readable storage medium and will be stored in a local storage medium through a network download, so that the method described herein can be stored in such software processing on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. Among them, the storage medium can be a magnetic disk, an optical disk, a read-only storage memory, a random access memory, a flash memory, a hard disk or a solid-state hard disk, etc.; further, the storage medium can also include a combination of the above types of memories. It can be understood that a computer, a processor, a microprocessor controller, or programmable hardware includes a storage component that can store or receive software or computer code. When the software or computer code is accessed and executed by a computer, a processor, or hardware, the method shown in the above embodiment is implemented.
[0152] Although the embodiments of the present invention have been described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present invention, and such modifications and variations are all within the scope defined by the appended claims.
Claims
1. A time-consuming calculation method for a code segment, It is characterized in that The method comprises: Obtaining the execution type of the target code segment currently to be time-consumingly calculated, wherein the execution type includes single execution and multiple executions; Based on the correspondence between the preset execution type and the preset time-consuming calculation strategy, determine the time-consuming calculation strategy corresponding to the execution type; Determine an execution time period of a scope corresponding to the target code segment according to the execution type; The time consumption of the execution time period is calculated based on the time consumption calculation strategy to obtain the time consumption result of the target code segment.
2. The method according to claim 1, It is characterized in that The determining, according to the execution type, the execution time period of the scope corresponding to the target code segment includes: If the execution type is single execution, when the target code segment starts to execute, a start timestamp is obtained through a constructor, and when the target code segment ends execution, an end timestamp is obtained through a destructor; The execution time period is generated based on the start timestamp and the end timestamp.
3. The method according to claim 1, It is characterized in that The determining, according to the execution type, the execution time period of the scope corresponding to the target code segment includes: If the execution type is multiple executions, and the execution requirement of the multiple executions is non-loop execution, then when the target code segment starts to execute, the character strings corresponding to the rendering start point and the rendering end point marks of each function in the target code segment are marked; When the target code segment ends execution, the start timestamp and the end timestamp corresponding to each function in the target code segment are determined according to the character string to obtain the execution time period.
4. The method according to claim 1, It is characterized in that The determining, according to the execution type, the execution time period of the scope corresponding to the target code segment includes: If the execution type is multiple executions, and the execution requirement of the multiple executions is cyclic execution, then a preset time-consuming statistical cycle is obtained; Generate a first timeline based on the time-consuming statistical period, and mark the character strings corresponding to the rendering start point and the rendering end point of each function in the first timeline in sequence; If the rendering end point of the function in the first timeline exceeds the deadline of the first timeline, the rendering end point is used as the start time of the next timeline until the target code segment finishes executing; The function corresponding to each time axis in the target code segment is determined according to the character string, and the start timestamp and the end timestamp corresponding to each function in the time axis are obtained to obtain the execution time period.
5. The method according to claim 1, It is characterized in that The determining the time-consuming calculation strategy corresponding to the execution type based on the correspondence between the preset execution type and the preset time-consuming calculation strategy includes: If the execution type is a single execution, determining the time-consuming calculation strategy to be a first strategy; Or, if the execution type is multiple executions, the time consumption statistical requirement of the target code segment is obtained. When the time consumption statistical requirement is non-cyclic execution, the time consumption calculation strategy is determined to be the second strategy. When the time consumption statistical requirement is cyclic execution, the time consumption calculation strategy is determined to be the third strategy.
6. The method according to claim 5, It is characterized in that The step of performing time consumption calculation on the execution time period based on the time consumption calculation strategy to obtain the time consumption result of the target code segment includes: If the time consumption calculation strategy is the first strategy or the second strategy, the execution time corresponding to each function in the target code segment is calculated according to the start timestamp and the end timestamp; Determining whether the target code segment satisfies a first output condition; If the first output condition is met, a first time consumption result is generated according to the execution time corresponding to each function in the target code segment.
7. The method according to claim 5, It is characterized in that The step of performing time consumption calculation on the execution time period based on the time consumption calculation strategy to obtain the time consumption result of the target code segment includes: If the time consumption calculation strategy is the third strategy, the execution time of the function in each time axis is calculated according to the start timestamp and the end timestamp to obtain a time consumption sequence; Calculate the average time, minimum time and maximum time of the function in each time axis according to the time sequence; Determine whether the current time axis meets the second output condition; If the second output condition is met, a second time consumption result is generated according to the number of function calls, average time consumption, minimum time consumption and maximum time consumption in the current timeline.
8. A device for calculating the time consumption of a code segment, It is characterized in that The device comprises: An acquisition module, used to acquire the execution type of the target code segment currently to be time-consumingly calculated, wherein the execution type includes single execution and multiple executions; A first determination module, configured to determine a time-consuming calculation strategy corresponding to a preset execution type based on a correspondence between the preset execution type and the preset time-consuming calculation strategy; A second determination module, configured to determine an execution time period of a scope corresponding to the target code segment according to the execution type; A calculation module is used to perform time consumption calculation on the execution time period based on the time consumption calculation strategy to obtain a time consumption result of the target code segment.
9. A computer device, It is characterized in that include: A memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the method according to any one of claims 1 to 7 by executing the computer instructions.
10. A computer-readable storage medium, It is characterized in that The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a computer to execute the method according to any one of claims 1 to 7.
Citation Information
Cited By
Code time consumption statistical method and system based on IDEA plug-in
CN120045431A
