Contenuto principale

Calculate Execution Time and Memory Use of C/C++ Code

The metric Execution time measures how much time the process spends in executing the various callable entities in your code. See Execution Time.

The metric Memory usage measures how the callable entities use the available stack memory. See Memory Use.

The steps for calculating these metrics depend on your workflow:

  • If you use a Polyspace Platform project (*.psprjx), you can:

    • Calculate these metrics in the Polyspace Platform user interface.

    • Automate the calculation by using the Polyspace® Test™ command line interface.

  • If you use the Polyspace Test xUnit API, you can calculate these metrics by using your own toolchain and the polyspace-code-profiler command.

This example shows how to calculate the execution time and memory use metrics of your code by using the command polyspace-code-profiler command. In this workflow, you do not require a Polyspace Test project.

Prerequisites

Before you calculate the execution time or memory use metrics, perform these steps:

  1. Copy this code in a writable location, say in a file source/src.c.:

    double power(double x, int n)
    {
        double BN = x;
        for(int i = 1; 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))));
    }
    

  2. Define some environment variables to point to Polyspace Test files for use as shorthands in compilation commands. For more information, see Set Up C/C++ Testing and Code Profiling Using Self-Managed Builds.

  3. To compute the execution time or memory use, you need an entry point to your code. The source code in src.c contains two functions but no entry point. Include a main() function in the source file. The main() function must call the functions you want to profile. For example, include this main function:

     int main() {
          AppxIndex(1.5, 1.1);
          return 0;
    }

  4. Compile the source file to check for compilation errors. This example uses the GCC compiler (with compilation command gcc). In Windows®, the compilation command is:

    gcc source/src.c

Instrument Source Files

Once the code is prepared with an entry point, Instrument the source files for either execution profile or stack memory profile calculation. Polyspace Test does not support simultaneously calculating more than one among code coverage, execution profile, and stack profile. To calculate both stack profile and execution profile, you must perform two separate analyses.

To instrument the source files for execution time, at the command line, enter:

polyspace-code-profiler -instrument -limit-instrumentation-to source/ -instrum-dir instrums/ -exec-metric-level detailed -prof-counter-size 64 -- gcc -c source/src.c
Alternatively, to calculate the memory use, at the command line, enter:
polyspace-code-profiler -instrument -limit-instrumentation-to source/ -instrum-dir instrums/ -stack-metric-level detailed -prof-counter-size 64 -- gcc -c source/src.c

This command:

  • Instruments the sources file in the folder source for execution profiling data collection and scopes the instrumentation to the source folder only. When instrumenting, Polyspace Test does not modify your original source files. Instead, Polyspace Test copies your source files and injects instrumenting macros in the copy.

  • Sets the value detailed for the option -exec-metric-level or -stack-metric-level. This value enables the calculation of all supported profiling metrics. The default value is none.

  • Sets the value of -prof-counter-size as 64. This option represents:

    • The size in bits of the timestamp used to compute execution profiling results.

    • The size in bits of the stack counter.

    This option is required if you want to calculate execution profiling or stack profiling. On 64-bit systems, specify 64 as the value.

  • Saves the instrumented copy of the source files and traceability databases in the folder instrums.

  • Compiles the instrumented source file src.c into an object file src.o. Because you specify the -c flag for gcc, the command does not link the objects into an executable.

Generate Profiling Report

After instrumenting your source file, generate the profiling report.

  1. Generate an executable by linking the object files generated in the previous step with the Polyspace Test precompiled library. The precompiled library is provided with the Polyspace Test installation. In Windows, use this command to link the objects with the library:

    gcc src.o <PSPROFILELIB>
    For more information on the variable <PSPROFILELIB>, see Set Up C/C++ Testing and Code Profiling Using Self-Managed Builds.

    You run the generated executable to collect coverage data.

  2. Collect code profiling data by running the executable generated in the previous step. Specify the folder instrums, which contains the instrumented source files, as the input to -instrum-dir:

    polyspace-code-profiler -run -instrum-dir instrums/ -results-dir results/ -- a.exe
    
    Polyspace Test stores the collected data in the results folder.

  3. Generate a readable HTML report from the collected profiling data using this command:

    polyspace-code-profiler -report -html -report-dir reports/ results/
    This command generates an HTML report in the reports folder.

  4. Review the report to identify execution speed and memory bottlenecks in your code. For details, see:

See Also

Topics