主要内容

Execution Time

Measures execution time and number of calls for various callable entities of your code

Since R2023b

Description

The execution time measures various dynamic metrics such as:

  • Time required to execute different callable entities: To calculate this result, Polyspace® Test™ initiates a timer when the test executable starts its operation. Say a function is called at time when the timer value is T_entry and the function returns when the timer value is T_return. Then the Execution Time metric of the function is calculated using these values.

  • Number of calls: Polyspace Test reports how many times a callable entity is called in your code.

Polyspace Implementation

When you calculate execution time, Polyspace Test instruments your source code to calculate the number of calls to functions and their execution time. If your code calls a function more than once, Polyspace Test reports an average execution time for the function. When calculating the execution time of the functions, Polyspace Test calculates several different metrics:

  • Total time — The total time to execute a function from its entry point to when the function returns. That is,

    Total time = T_return - T_entry

  • Self time — This metric shows the time required to execute a function from entry point to return without considering the time required to execute the callee functions. For instance, say the function foo() calls the function bar() twice. The Total time for foo() is T_foo and the Total time for bar() is T_bar. Then, Polyspace Test calculates the Self time of foo() as:

    T_self_foo = T_foo-2*T_bar

  • Count — Represents the number of times a function executes.

  • Max — The maximum calculated Self time for a function.

  • Saturated — A binary flag representing whether the Total time for a function overflows a 32 bit counter. For a particular function call, if the total time value saturates, Polyspace sets:

    1. The Saturated flag to true

    2. Max to the saturated value

    3. The Total time and Self time to the last known unsaturated value

When instrumenting your source code for execution profile calculation, Polyspace Test injects various macros. These macros can hinder some compiler optimizations, such as inlining trivial functions. If compiler optimizations are prevented, the reported execution time is an upper bound of the optimized execution time.

Polyspace Test does not support calculating more than one of the code coverage, execution time, and memory use metrics at a time.

Examples

expand all

In this example, you calculate the code execution time to identify the bottlenecks in your code. For details, see Calculate Execution Time and Memory Use of C/C++ Code.

Save this source code in a writable folder SRC.:

 long long power(double x, int n){
	 long long BN = 1;
	 for(int i = 0; i<n;++i){
		 BN*=x;
	 }
	 return BN;
 }
 
 double AppxIndex(double m, double f){
	 double U = (power(m,2) - 1)/(power(m,2)+2);
	 double V = (power(m,4) + 27*power(m,2)+38)/(2*power(m,2)+3);
	 return (1+2*f*power(U,2)*(1+power(m,2)*U*V + power(m,3)/
         power(m,3)*(U-V)))/( (1-2*f*power(U,2)*(1+power(m,2)*U*V 
        + power(m,3)/power(m,3)*(U-V))));
 }
 
 int main(){
 AppxIndex(1.5, 1.1);
 return 1;
 }

Instrument the source code for execution time measurement. At the command line, enter:

polyspace-code-profiler -instrument -limit-instrumentation-to  SRC -instrum-dir  instrumFolder -exec-metric-level detailed -prof-counter-size 64 -- gcc  SRC/source.c TEST_SOURCE  -I TEST_INCLUDE   

Here, TEST_SOURCE is polyspaceroot/polyspace/pstest/pstunit/src/pstunit.c and TEST_INCLUDE is polyspaceroot/polyspace/pstest/pstunit/include. The path is the same for both Linux® and Windows® systems.

Produce the test executable by linking the instrumented source code and the Polyspace Test run-time library:

g++ -o source.0 pstunit.o PSLIB
Here, PSLIB is the path to the run-time library appropriate for your environment.

Run the test executable to gather execution time data:

polyspace-code-profiler -run -instrum-dir $instrumFolder -results-dir runFolder -- Executable
Here, Executable is the test executable you generated in the previous step.

Create the report:

polyspace-code-profiler -report -html -report-dir execReport  runFolder/output_run.psprof

Polyspace Test stores the report in the execReport folder. The results indicate that the code spends 52% of the execution time in the function power(). Improving the efficiency of this function can improve the performance of your code.

Version History

Introduced in R2023b