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-profilercommand.
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:
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)))); }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.
To compute the execution time or memory use, you need an entry point to your code. The source code in
src.ccontains two functions but no entry point. Include amain()function in the source file. Themain()function must call the functions you want to profile. For example, include this main function:int main() { AppxIndex(1.5, 1.1); return 0; }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.cpolyspace-code-profiler -instrument -limit-instrumentation-to source/ -instrum-dir instrums/ -stack-metric-level detailed -prof-counter-size 64 -- gcc -c source/src.cThis command:
Instruments the sources file in the folder
sourcefor execution profiling data collection and scopes the instrumentation to thesourcefolder 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
detailedfor the option-exec-metric-levelor-stack-metric-level. This value enables the calculation of all supported profiling metrics. The default value isnone.Sets the value of
-prof-counter-sizeas64. 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
64as the value.Saves the instrumented copy of the source files and traceability databases in the folder
instrums.Compiles the instrumented source file
src.cinto an object filesrc.o. Because you specify the-cflag forgcc, the command does not link the objects into an executable.
Generate Profiling Report
After instrumenting your source file, generate the profiling report.
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:
For more information on the variablegcc src.o <PSPROFILELIB><PSPROFILELIB>, see Set Up C/C++ Testing and Code Profiling Using Self-Managed Builds.You run the generated executable to collect coverage data.
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 Test stores the collected data in thepolyspace-code-profiler -run -instrum-dir instrums/ -results-dir results/ -- a.exeresultsfolder.Generate a readable HTML report from the collected profiling data using this command:
This command generates an HTML report in thepolyspace-code-profiler -report -html -report-dir reports/ results/reportsfolder.Review the report to identify execution speed and memory bottlenecks in your code. For details, see: