42

Currently I just add the following lines around my code:

import time
start_time = time.time()

# my code here

print "time elapsed: {:.2f}s".format(time.time() - start_time)

Is it possible to achieve the same without adding code to every script I want to time? Either adding something in run configuration or using a plugin?

2
  • 1
    When I run my code in PyCharm I always get how long it took to run by default. Commented Feb 26, 2016 at 16:07
  • On *nix systems you can simply run time python3 code.py Commented Aug 10, 2023 at 3:17

5 Answers 5

53

You can profile your script by hitting the 'profile' button (it is to the right of the 'run', 'debug', and 'run with coverage' buttons):

Profile button

Among the output, you will find the name of the script itself, and the time needed to run it.

Note: the function is available in PyCharm PROFESSIONAL 2017.1 for the Linux platform; other installations might not provide the profiler button.

Sign up to request clarification or add additional context in comments.

6 Comments

it is not there for the recent 2017 release of the Pycharm community Edition is there any alternative to the ide sort that I can exploit from the terminal.
@AloyASen you may take a look at this question: stackoverflow.com/questions/6786990/…
If you have a valid .edu email, get a free 1-year license of the PyCharm Professional here: jetbrains.com/student
This will add additional execution time to your run because of overhead from the cProfile tool
seems overkill, all I want is the how long it took to execute
|
17

Since not everyone has PyCharm Pro which can measure script's running time, here is a simple solution that uses decorator. We only need to add a single line of code to measure the running time of any function as follows:

import time

def timeit(func):
    """
    Decorator for measuring function's running time.
    """
    def measure_time(*args, **kw):
        start_time = time.time()
        result = func(*args, **kw)
        print("Processing time of %s(): %.2f seconds."
              % (func.__qualname__, time.time() - start_time))
        return result

    return measure_time

@timeit
def func():
    for _ in range(3):
        time.sleep(1)

if __name__ == "__main__":
    func()

Output:

Processing time of func(): 3.00 seconds.

Comments

7

I know it is late but I wanted the same thing and here is what I did:

Create another python file in the directory of your codes:

import time
st=time.time()
import test
print("----%.2f----"%(time.time()-st))

where test is your program name. So if you want to run any program just run it from here by just changing test.

Keep in mind that import runs the code normally if you haven't used:

if __name__=="__main__":

Comments

2

Just write corresponding unit test (works with community edition).

from unittest import TestCase

from yourscript import yourcode

class TestSol(TestCase):
    def benchmark(self):
        res = yourcode('banana')
        self.assertEqual(res, 77)

PyCharm neatly displays time taken for each test.

Another solution would be to wrap the interpreter with time.
But since it will help in other ways, I recommend taking the unit-tests road.

Comments

-2

You can use this command while using IPython / Jupyter notebook

Command can be added to first line in the cell to get CPU and wall time required to execute the cell

%%time
# code

An alternative to this is (timeit - it will run cell multiple times to get average and standard deviation of computational time)

%%timeit
# code

2 Comments

That looks like IPython / Jupyter magic. Nothing in the question mentions IPython or Jupyter, and this is invalid in regular Python.
I just tried this, it does not work, it's a total failure. File "<input>", line 1 %%time ^ SyntaxError: invalid syntax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.