skip to Main Content

I am wondering if it is possible for Python in VSCode (or anywhere else) that I can generate a running time report to evaluate the performance of codes, which means I can know the running time of each function or even each line with some clicks instead of measuring them phase by phase. And that is like the ‘run and time’ button in MATLAB. Thanks!

2

Answers


  1. If you are using Linux, you can write this for python code to run:

    time python yourprogram.py
    

    This will get you the time it takes for your program to run. You can use it with -v for more verbose, something like this:

    time -v python yourprogram.py
    

    Also, you can use the below code to print the execution time of your program manually:

    import time
    start_time = time.time()
    main()
    print("--- %s seconds ---" % (time.time() - start_time))
    
    Login or Signup to reply.
  2. evaluate the performance of codes

    I think what you need is not only the time at the end of the code run, but also the total time required to run part of the code.

    You can try to use jupyter notebook to split the code into cells, so that you can count the time spent in running some code.

    1. Install Jupyter extension in market.
    2. Create .ipynb file and write codes in the cell.
    3. Click Run on the left, and the running time will be displayed after running.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search