CPU usage of python script











up vote
0
down vote

favorite
1












Is it possible to check CPU usage of simple script ?



For example: How to get the CPU usage in % of printing 100 times "hello world!" ?



Currently I'm getting the execution time in the console, by:



time -p python script.py









share|improve this question
























  • so? what do you want that "time" don't let you?
    – Tamar
    Jun 19 '17 at 18:51










  • Is stackoverflow.com/questions/15176619/… what you are looking for?
    – Artyer
    Jun 19 '17 at 18:51










  • You can look at the top unix command.
    – coldspeed
    Jun 19 '17 at 19:02















up vote
0
down vote

favorite
1












Is it possible to check CPU usage of simple script ?



For example: How to get the CPU usage in % of printing 100 times "hello world!" ?



Currently I'm getting the execution time in the console, by:



time -p python script.py









share|improve this question
























  • so? what do you want that "time" don't let you?
    – Tamar
    Jun 19 '17 at 18:51










  • Is stackoverflow.com/questions/15176619/… what you are looking for?
    – Artyer
    Jun 19 '17 at 18:51










  • You can look at the top unix command.
    – coldspeed
    Jun 19 '17 at 19:02













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





Is it possible to check CPU usage of simple script ?



For example: How to get the CPU usage in % of printing 100 times "hello world!" ?



Currently I'm getting the execution time in the console, by:



time -p python script.py









share|improve this question















Is it possible to check CPU usage of simple script ?



For example: How to get the CPU usage in % of printing 100 times "hello world!" ?



Currently I'm getting the execution time in the console, by:



time -p python script.py






python linux console






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 19 '17 at 20:14









Benny

4331515




4331515










asked Jun 19 '17 at 18:49









user7375796

155




155












  • so? what do you want that "time" don't let you?
    – Tamar
    Jun 19 '17 at 18:51










  • Is stackoverflow.com/questions/15176619/… what you are looking for?
    – Artyer
    Jun 19 '17 at 18:51










  • You can look at the top unix command.
    – coldspeed
    Jun 19 '17 at 19:02


















  • so? what do you want that "time" don't let you?
    – Tamar
    Jun 19 '17 at 18:51










  • Is stackoverflow.com/questions/15176619/… what you are looking for?
    – Artyer
    Jun 19 '17 at 18:51










  • You can look at the top unix command.
    – coldspeed
    Jun 19 '17 at 19:02
















so? what do you want that "time" don't let you?
– Tamar
Jun 19 '17 at 18:51




so? what do you want that "time" don't let you?
– Tamar
Jun 19 '17 at 18:51












Is stackoverflow.com/questions/15176619/… what you are looking for?
– Artyer
Jun 19 '17 at 18:51




Is stackoverflow.com/questions/15176619/… what you are looking for?
– Artyer
Jun 19 '17 at 18:51












You can look at the top unix command.
– coldspeed
Jun 19 '17 at 19:02




You can look at the top unix command.
– coldspeed
Jun 19 '17 at 19:02












3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










You'll need the psutil module.



import psutil
print(psutil.cpu_percent())





share|improve this answer





















  • Thank you !!! :)
    – user7375796
    Jun 19 '17 at 19:05






  • 2




    psutil.cpu_percent is for the whole system, not an individual process. For just the current script, use current_process = psutil.Process(); print(current_process.cpu_percent()).
    – eryksun
    Jun 19 '17 at 19:47




















up vote
2
down vote













If you are on a unix machine, you could always open top in a new terminal and then observe the % usage while you run your python program. Alternatively, there are some 3rd party libraries you can use.



Here's one: Benchmark



Examples (taken from the py package index).



Program:



from benchmarker import Benchmarker

## specify number of loop
with Benchmarker(1000*1000, width=20) as bench:
s1, s2, s3, s4, s5 = "Haruhi", "Mikuru", "Yuki", "Itsuki", "Kyon"

@bench(None) ## empty loop
def _(bm):
for i in bm:
pass

@bench("join")
def _(bm):
for i in bm:
sos = ''.join((s1, s2, s3, s4, s5))

@bench("concat")
def _(bm):
for i in bm:
sos = s1 + s2 + s3 + s4 + s5

@bench("format")
def _(bm):
for i in bm:
sos = '%s%s%s%s%s' % (s1, s2, s3, s4, s5)


Results:



$ python example.py -h              # show help
$ python example.py -o result.json
## benchmarker: release 4.0.0 (for python)
## python version: 3.4.2
## python compiler: GCC 4.8.2
## python platform: Linux-3.13.0-36-generic-x86_64-with-debian-jessie-sid
## python executable: /opt/vs/python/3.4.2/bin/python
## cpu model: Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz # 2494.050 MHz
## parameters: loop=1000000, cycle=1, extra=0

## real (total = user + sys)
(Empty) 0.0236 0.0200 0.0200 0.0000
join 0.2779 0.2800 0.2800 0.0000
concat 0.3792 0.3800 0.3800 0.0000
format 0.4233 0.4300 0.4300 0.0000

## Ranking real
join 0.2779 (100.0) ********************
concat 0.3792 ( 73.3) ***************
format 0.4233 ( 65.6) *************

## Matrix real [01] [02] [03]
[01] join 0.2779 100.0 136.5 152.3
[02] concat 0.3792 73.3 100.0 111.6
[03] format 0.4233 65.6 89.6 100.0





share|improve this answer






























    up vote
    0
    down vote













    One thing you can do is use timeit and find the difference in the time of the execution and finish of the program. For example:



    import timeit
    start_time = timeit.default_timer()
    print("Hello World")
    print("Hello World")
    print("Hello World")
    end_time = timeit.default_timer()
    print (end_time - start_time)





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44637844%2fcpu-usage-of-python-script%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote



      accepted










      You'll need the psutil module.



      import psutil
      print(psutil.cpu_percent())





      share|improve this answer





















      • Thank you !!! :)
        – user7375796
        Jun 19 '17 at 19:05






      • 2




        psutil.cpu_percent is for the whole system, not an individual process. For just the current script, use current_process = psutil.Process(); print(current_process.cpu_percent()).
        – eryksun
        Jun 19 '17 at 19:47

















      up vote
      0
      down vote



      accepted










      You'll need the psutil module.



      import psutil
      print(psutil.cpu_percent())





      share|improve this answer





















      • Thank you !!! :)
        – user7375796
        Jun 19 '17 at 19:05






      • 2




        psutil.cpu_percent is for the whole system, not an individual process. For just the current script, use current_process = psutil.Process(); print(current_process.cpu_percent()).
        – eryksun
        Jun 19 '17 at 19:47















      up vote
      0
      down vote



      accepted







      up vote
      0
      down vote



      accepted






      You'll need the psutil module.



      import psutil
      print(psutil.cpu_percent())





      share|improve this answer












      You'll need the psutil module.



      import psutil
      print(psutil.cpu_percent())






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jun 19 '17 at 18:58









      Krohnus Melavea

      977




      977












      • Thank you !!! :)
        – user7375796
        Jun 19 '17 at 19:05






      • 2




        psutil.cpu_percent is for the whole system, not an individual process. For just the current script, use current_process = psutil.Process(); print(current_process.cpu_percent()).
        – eryksun
        Jun 19 '17 at 19:47




















      • Thank you !!! :)
        – user7375796
        Jun 19 '17 at 19:05






      • 2




        psutil.cpu_percent is for the whole system, not an individual process. For just the current script, use current_process = psutil.Process(); print(current_process.cpu_percent()).
        – eryksun
        Jun 19 '17 at 19:47


















      Thank you !!! :)
      – user7375796
      Jun 19 '17 at 19:05




      Thank you !!! :)
      – user7375796
      Jun 19 '17 at 19:05




      2




      2




      psutil.cpu_percent is for the whole system, not an individual process. For just the current script, use current_process = psutil.Process(); print(current_process.cpu_percent()).
      – eryksun
      Jun 19 '17 at 19:47






      psutil.cpu_percent is for the whole system, not an individual process. For just the current script, use current_process = psutil.Process(); print(current_process.cpu_percent()).
      – eryksun
      Jun 19 '17 at 19:47














      up vote
      2
      down vote













      If you are on a unix machine, you could always open top in a new terminal and then observe the % usage while you run your python program. Alternatively, there are some 3rd party libraries you can use.



      Here's one: Benchmark



      Examples (taken from the py package index).



      Program:



      from benchmarker import Benchmarker

      ## specify number of loop
      with Benchmarker(1000*1000, width=20) as bench:
      s1, s2, s3, s4, s5 = "Haruhi", "Mikuru", "Yuki", "Itsuki", "Kyon"

      @bench(None) ## empty loop
      def _(bm):
      for i in bm:
      pass

      @bench("join")
      def _(bm):
      for i in bm:
      sos = ''.join((s1, s2, s3, s4, s5))

      @bench("concat")
      def _(bm):
      for i in bm:
      sos = s1 + s2 + s3 + s4 + s5

      @bench("format")
      def _(bm):
      for i in bm:
      sos = '%s%s%s%s%s' % (s1, s2, s3, s4, s5)


      Results:



      $ python example.py -h              # show help
      $ python example.py -o result.json
      ## benchmarker: release 4.0.0 (for python)
      ## python version: 3.4.2
      ## python compiler: GCC 4.8.2
      ## python platform: Linux-3.13.0-36-generic-x86_64-with-debian-jessie-sid
      ## python executable: /opt/vs/python/3.4.2/bin/python
      ## cpu model: Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz # 2494.050 MHz
      ## parameters: loop=1000000, cycle=1, extra=0

      ## real (total = user + sys)
      (Empty) 0.0236 0.0200 0.0200 0.0000
      join 0.2779 0.2800 0.2800 0.0000
      concat 0.3792 0.3800 0.3800 0.0000
      format 0.4233 0.4300 0.4300 0.0000

      ## Ranking real
      join 0.2779 (100.0) ********************
      concat 0.3792 ( 73.3) ***************
      format 0.4233 ( 65.6) *************

      ## Matrix real [01] [02] [03]
      [01] join 0.2779 100.0 136.5 152.3
      [02] concat 0.3792 73.3 100.0 111.6
      [03] format 0.4233 65.6 89.6 100.0





      share|improve this answer



























        up vote
        2
        down vote













        If you are on a unix machine, you could always open top in a new terminal and then observe the % usage while you run your python program. Alternatively, there are some 3rd party libraries you can use.



        Here's one: Benchmark



        Examples (taken from the py package index).



        Program:



        from benchmarker import Benchmarker

        ## specify number of loop
        with Benchmarker(1000*1000, width=20) as bench:
        s1, s2, s3, s4, s5 = "Haruhi", "Mikuru", "Yuki", "Itsuki", "Kyon"

        @bench(None) ## empty loop
        def _(bm):
        for i in bm:
        pass

        @bench("join")
        def _(bm):
        for i in bm:
        sos = ''.join((s1, s2, s3, s4, s5))

        @bench("concat")
        def _(bm):
        for i in bm:
        sos = s1 + s2 + s3 + s4 + s5

        @bench("format")
        def _(bm):
        for i in bm:
        sos = '%s%s%s%s%s' % (s1, s2, s3, s4, s5)


        Results:



        $ python example.py -h              # show help
        $ python example.py -o result.json
        ## benchmarker: release 4.0.0 (for python)
        ## python version: 3.4.2
        ## python compiler: GCC 4.8.2
        ## python platform: Linux-3.13.0-36-generic-x86_64-with-debian-jessie-sid
        ## python executable: /opt/vs/python/3.4.2/bin/python
        ## cpu model: Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz # 2494.050 MHz
        ## parameters: loop=1000000, cycle=1, extra=0

        ## real (total = user + sys)
        (Empty) 0.0236 0.0200 0.0200 0.0000
        join 0.2779 0.2800 0.2800 0.0000
        concat 0.3792 0.3800 0.3800 0.0000
        format 0.4233 0.4300 0.4300 0.0000

        ## Ranking real
        join 0.2779 (100.0) ********************
        concat 0.3792 ( 73.3) ***************
        format 0.4233 ( 65.6) *************

        ## Matrix real [01] [02] [03]
        [01] join 0.2779 100.0 136.5 152.3
        [02] concat 0.3792 73.3 100.0 111.6
        [03] format 0.4233 65.6 89.6 100.0





        share|improve this answer

























          up vote
          2
          down vote










          up vote
          2
          down vote









          If you are on a unix machine, you could always open top in a new terminal and then observe the % usage while you run your python program. Alternatively, there are some 3rd party libraries you can use.



          Here's one: Benchmark



          Examples (taken from the py package index).



          Program:



          from benchmarker import Benchmarker

          ## specify number of loop
          with Benchmarker(1000*1000, width=20) as bench:
          s1, s2, s3, s4, s5 = "Haruhi", "Mikuru", "Yuki", "Itsuki", "Kyon"

          @bench(None) ## empty loop
          def _(bm):
          for i in bm:
          pass

          @bench("join")
          def _(bm):
          for i in bm:
          sos = ''.join((s1, s2, s3, s4, s5))

          @bench("concat")
          def _(bm):
          for i in bm:
          sos = s1 + s2 + s3 + s4 + s5

          @bench("format")
          def _(bm):
          for i in bm:
          sos = '%s%s%s%s%s' % (s1, s2, s3, s4, s5)


          Results:



          $ python example.py -h              # show help
          $ python example.py -o result.json
          ## benchmarker: release 4.0.0 (for python)
          ## python version: 3.4.2
          ## python compiler: GCC 4.8.2
          ## python platform: Linux-3.13.0-36-generic-x86_64-with-debian-jessie-sid
          ## python executable: /opt/vs/python/3.4.2/bin/python
          ## cpu model: Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz # 2494.050 MHz
          ## parameters: loop=1000000, cycle=1, extra=0

          ## real (total = user + sys)
          (Empty) 0.0236 0.0200 0.0200 0.0000
          join 0.2779 0.2800 0.2800 0.0000
          concat 0.3792 0.3800 0.3800 0.0000
          format 0.4233 0.4300 0.4300 0.0000

          ## Ranking real
          join 0.2779 (100.0) ********************
          concat 0.3792 ( 73.3) ***************
          format 0.4233 ( 65.6) *************

          ## Matrix real [01] [02] [03]
          [01] join 0.2779 100.0 136.5 152.3
          [02] concat 0.3792 73.3 100.0 111.6
          [03] format 0.4233 65.6 89.6 100.0





          share|improve this answer














          If you are on a unix machine, you could always open top in a new terminal and then observe the % usage while you run your python program. Alternatively, there are some 3rd party libraries you can use.



          Here's one: Benchmark



          Examples (taken from the py package index).



          Program:



          from benchmarker import Benchmarker

          ## specify number of loop
          with Benchmarker(1000*1000, width=20) as bench:
          s1, s2, s3, s4, s5 = "Haruhi", "Mikuru", "Yuki", "Itsuki", "Kyon"

          @bench(None) ## empty loop
          def _(bm):
          for i in bm:
          pass

          @bench("join")
          def _(bm):
          for i in bm:
          sos = ''.join((s1, s2, s3, s4, s5))

          @bench("concat")
          def _(bm):
          for i in bm:
          sos = s1 + s2 + s3 + s4 + s5

          @bench("format")
          def _(bm):
          for i in bm:
          sos = '%s%s%s%s%s' % (s1, s2, s3, s4, s5)


          Results:



          $ python example.py -h              # show help
          $ python example.py -o result.json
          ## benchmarker: release 4.0.0 (for python)
          ## python version: 3.4.2
          ## python compiler: GCC 4.8.2
          ## python platform: Linux-3.13.0-36-generic-x86_64-with-debian-jessie-sid
          ## python executable: /opt/vs/python/3.4.2/bin/python
          ## cpu model: Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz # 2494.050 MHz
          ## parameters: loop=1000000, cycle=1, extra=0

          ## real (total = user + sys)
          (Empty) 0.0236 0.0200 0.0200 0.0000
          join 0.2779 0.2800 0.2800 0.0000
          concat 0.3792 0.3800 0.3800 0.0000
          format 0.4233 0.4300 0.4300 0.0000

          ## Ranking real
          join 0.2779 (100.0) ********************
          concat 0.3792 ( 73.3) ***************
          format 0.4233 ( 65.6) *************

          ## Matrix real [01] [02] [03]
          [01] join 0.2779 100.0 136.5 152.3
          [02] concat 0.3792 73.3 100.0 111.6
          [03] format 0.4233 65.6 89.6 100.0






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 22:01









          Manuco Bianco

          1116




          1116










          answered Jun 19 '17 at 19:07









          coldspeed

          111k17101170




          111k17101170






















              up vote
              0
              down vote













              One thing you can do is use timeit and find the difference in the time of the execution and finish of the program. For example:



              import timeit
              start_time = timeit.default_timer()
              print("Hello World")
              print("Hello World")
              print("Hello World")
              end_time = timeit.default_timer()
              print (end_time - start_time)





              share|improve this answer

























                up vote
                0
                down vote













                One thing you can do is use timeit and find the difference in the time of the execution and finish of the program. For example:



                import timeit
                start_time = timeit.default_timer()
                print("Hello World")
                print("Hello World")
                print("Hello World")
                end_time = timeit.default_timer()
                print (end_time - start_time)





                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  One thing you can do is use timeit and find the difference in the time of the execution and finish of the program. For example:



                  import timeit
                  start_time = timeit.default_timer()
                  print("Hello World")
                  print("Hello World")
                  print("Hello World")
                  end_time = timeit.default_timer()
                  print (end_time - start_time)





                  share|improve this answer












                  One thing you can do is use timeit and find the difference in the time of the execution and finish of the program. For example:



                  import timeit
                  start_time = timeit.default_timer()
                  print("Hello World")
                  print("Hello World")
                  print("Hello World")
                  end_time = timeit.default_timer()
                  print (end_time - start_time)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 19 '17 at 19:11









                  dsomel21

                  1381112




                  1381112






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44637844%2fcpu-usage-of-python-script%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)