Python cProfile Command Line: Scripts & Examples

You can use the cProfile module to profile your code and analyze its performance. You can use cProfile from the command line by invoking Python with the -m cProfile option followed by the name of the script you want to profile.

Here’s the basic command-line syntax for profiling a Python script using cProfile:

python -m cProfile your_script.pyCode language: Python (python)

Replace your_script.py with the name of the Python script you want to profile. When you run this command, Python will execute your script while collecting profiling information, and it will display the results to the console after your script has finished running.

Here’s an example of how to use cProfile from the command line:

python -m cProfile my_script.pyCode language: Python (python)

After running this command, you will see profiling information, including function calls, execution time, and more, printed to the console.

You can also save the profiling results to a file for further analysis using the -o option like this:

python -m cProfile -o profile_results.prof your_script.pyCode language: Python (python)

This command will save the profiling results to a file named profile_results.prof. You can then analyze the results using tools like pstats to get more insights into your script’s performance.

To view and analyze the profiling results, you can use the pstats module or tools like snakeviz. Here’s an example of how to use pstats to view the results:

python -m pstats profile_results.profCode language: Python (python)

This will start an interactive session where you can explore the profiling data.

Remember to replace profile_results.prof with the actual filename if you used a different name when saving the profiling results.

Python Cprofile Command Line Arguments

In addition to the basic usage of cProfile from the command line, you can pass command-line arguments to your Python script when profiling it with cProfile. To do this, you simply include the script’s arguments after the script’s name. Here’s the general syntax:

python -m cProfile your_script.py arg1 arg2 ...Code language: Python (python)

For example, if you have a Python script named my_script.py that accepts two command-line arguments, you can profile it like this:

python -m cProfile my_script.py arg1 arg2Code language: Python (python)

In this example, arg1 and arg2 are placeholders for the actual command-line arguments you want to pass to your script. You should replace them with the appropriate values for your use case.

When you run your script with cProfile and provide command-line arguments, cProfile will profile the execution of your script with those arguments, allowing you to analyze the performance of your script under different scenarios.

Here’s a simple example of how you might use command-line arguments in your Python script:

# my_script.py

import sys

def main():
    if len(sys.argv) != 3:
        print("Usage: my_script.py arg1 arg2")
        sys.exit(1)

    arg1 = sys.argv[1]
    arg2 = sys.argv[2]

    # Your script logic here

if __name__ == "__main__":
    main()Code language: Python (python)

In this example, my_script.py expects two command-line arguments. You can profile it with cProfile and provide these arguments as follows:

python -m cProfile my_script.py argument1 argument2Code language: Python (python)

Replace argument1 and argument2 with the actual values you want to pass as arguments to your script.

Python Cprofile Command Line Options

Python’s cProfile module itself doesn’t have a lot of command-line options when run directly from the command line. However, you can control its behavior using a few options. Here’s how you can use them:

  1. -m cProfile: As mentioned earlier, this is the basic command to profile a Python script using cProfile.
python -m cProfile your_script.pyCode language: Python (python)

You can replace your_script.py with the name of your Python script.

  1. -o (output file): You can use the -o option to specify an output file to which the profiling results will be saved. This is useful for later analysis. For example:
python -m cProfile -o profile_results.prof your_script.pyCode language: Python (python)

This will save the profiling results to a file named profile_results.prof.

  1. -s (sort order): The -s option allows you to specify the sorting order for the profiling results. You can sort by various criteria, such as cumulative time (cumtime), time per call (tottime), and more. For example, to sort by cumulative time:
python -m cProfile -s cumtime your_script.pyCode language: Python (python)

You can replace cumtime with the sorting criterion of your choice.

  1. -q (quiet mode): The -q option suppresses the output of the profile statistics to the console. This can be useful when you only want to save the results to a file and not display them on the console:
python -m cProfile -q -o profile_results.prof your_script.pyCode language: Python (python)
  1. -r (profile multiple times): You can use the -r option to specify the number of times you want to profile the script. This is helpful for obtaining more accurate profiling results, especially when your script’s execution time is very short:

This will profile the script three times and provide statistics based on multiple runs.

Please note that these options are specific to running cProfile from the command line, and there are more advanced options and functions available when using cProfile programmatically in your Python code.

Read More;

  • Abdullah Walied Allama

    Abdullah Walied Allama is a driven programmer who earned his Bachelor's degree in Computer Science from Alexandria University's Faculty of Computer and Data Science. He is passionate about constructing problem-solving models and excels in various technical skills, including Python, data science, data analysis, Java, SQL, HTML, CSS, and JavaScript.

    View all posts

Leave a Comment