How to run-time script in PyCharm without adding code every time?

I am currently just adding 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 all the script, I want time? Or add something to your startup configuration or using a plugin?

+22
python pycharm
source share
3 answers

You can profile your script by clicking the "Profile" button (it is located to the right of the "Run", "Debug" and "Run with Coated" buttons):

Profile button

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

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

+26
source share

I know it's late, but I wanted the same, and here is what I did:

Create another Python file in your code directory:

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

where test is the name of your program. Therefore, if you want to run a program, just run it from here by simply changing the test.

Keep in mind that import runs the code normally if you have not used:

 if __name__=="__main__": 
+3
source share

Just write the appropriate unit test (works with community editors).

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

PyCharm accurately displays the time spent on each test.

Another solution is to enclose the translator in time .
But since this will help in other ways, I recommend taking a unit test.

0
source share

All Articles