How should i load a file in python efficiently using all my cpu power?











up vote
0
down vote

favorite












I decided to create a python code to encrypt my data a while back, when i am half way through, i decided to do a performance test, and the result is horrible, like only 430kB/s encrypting speed. Opening System monitor showed that only 1 thread of my 8C16T processor is being used by my program.



After I try temporary deleting the encrypting part in my code, it was only like 10kB/s faster. So i analyzed the code and found out this part of code



with open("10MB.test", 'rb') as f:  
byte = f.read(1)
if(i == 0):
test = (str("".join([ch.encode("hex") for line in byte for ch in line])))
i = 1
while byte != "":
g = g + 1
byte = f.read(1)
a = str("".join([ch.encode("hex") for line in byte for ch in line]))
test = test + a


runs very slow, and system monitor shows that it only uses one thread of my cpu . Is there a way to utilize all thread of my CPU to make this run faster?



import time
from datetime import datetime
realsr = ""
test = ""
i = 0
m = 0
g = 2
j = 0
ti1 = 0
ti2 = 0
ea = raw_input("Please Input Your First Password: ")
print "You Entered:",ea
eb = raw_input("Please Input Your Second Password: ")
print "You Entered:",eb
ec = raw_input("Please Input Your Third Password: ")
print "You Entered:",ec
ed = raw_input("Please Input Your Forth Password: ")
print "You Entered:",ed
ee = raw_input("Please Input Your Fifth Password: ")
print "You Entered:",ee
ef = raw_input("Please Input Your Sixth Password: ")
print "You Entered:",ef
eg = raw_input("Please Input Your Seventh Password: ")
print "You Entered:",eg
eh = raw_input("Please Input Your Eighth Password: ")
print "You Entered:",eh
ti1 = int(round(time.time() * 1000))
with open("10MB.test", 'rb') as d:
j = len(str("".join([ch.encode("hex") for line in d for ch in line])))
with open("10MB.test", 'rb') as f:
byte = f.read(1)
if(i == 0):
test = (str("".join([ch.encode("hex") for line in byte for ch in line])))
i = 1
while byte != "":
g = g + 1
byte = f.read(1)
a = str("".join([ch.encode("hex") for line in byte for ch in line]))
test = test + a
if(j != len(test)):
temp1 = int(a,16)
if(m == 0):
temp1 * ea
if(m == 1):
temp1 * eb
if(m == 2):
temp1 * ec
if(m == 3):
temp1 * ed
if(m == 4):
temp1 * ee
if(m == 5):
temp1 * ef
if(m == 6):
temp1 * eg
if(m == 7):
temp1 * eh
while len(test) >= j:
with open("clone.test", 'wb') as k:
k.write(test.decode("hex"))
ti2 = int(round(time.time() * 1000))
print "done!"
print test
print (ti2-ti1)
print ti1
print (ti2)
break









share|improve this question




























    up vote
    0
    down vote

    favorite












    I decided to create a python code to encrypt my data a while back, when i am half way through, i decided to do a performance test, and the result is horrible, like only 430kB/s encrypting speed. Opening System monitor showed that only 1 thread of my 8C16T processor is being used by my program.



    After I try temporary deleting the encrypting part in my code, it was only like 10kB/s faster. So i analyzed the code and found out this part of code



    with open("10MB.test", 'rb') as f:  
    byte = f.read(1)
    if(i == 0):
    test = (str("".join([ch.encode("hex") for line in byte for ch in line])))
    i = 1
    while byte != "":
    g = g + 1
    byte = f.read(1)
    a = str("".join([ch.encode("hex") for line in byte for ch in line]))
    test = test + a


    runs very slow, and system monitor shows that it only uses one thread of my cpu . Is there a way to utilize all thread of my CPU to make this run faster?



    import time
    from datetime import datetime
    realsr = ""
    test = ""
    i = 0
    m = 0
    g = 2
    j = 0
    ti1 = 0
    ti2 = 0
    ea = raw_input("Please Input Your First Password: ")
    print "You Entered:",ea
    eb = raw_input("Please Input Your Second Password: ")
    print "You Entered:",eb
    ec = raw_input("Please Input Your Third Password: ")
    print "You Entered:",ec
    ed = raw_input("Please Input Your Forth Password: ")
    print "You Entered:",ed
    ee = raw_input("Please Input Your Fifth Password: ")
    print "You Entered:",ee
    ef = raw_input("Please Input Your Sixth Password: ")
    print "You Entered:",ef
    eg = raw_input("Please Input Your Seventh Password: ")
    print "You Entered:",eg
    eh = raw_input("Please Input Your Eighth Password: ")
    print "You Entered:",eh
    ti1 = int(round(time.time() * 1000))
    with open("10MB.test", 'rb') as d:
    j = len(str("".join([ch.encode("hex") for line in d for ch in line])))
    with open("10MB.test", 'rb') as f:
    byte = f.read(1)
    if(i == 0):
    test = (str("".join([ch.encode("hex") for line in byte for ch in line])))
    i = 1
    while byte != "":
    g = g + 1
    byte = f.read(1)
    a = str("".join([ch.encode("hex") for line in byte for ch in line]))
    test = test + a
    if(j != len(test)):
    temp1 = int(a,16)
    if(m == 0):
    temp1 * ea
    if(m == 1):
    temp1 * eb
    if(m == 2):
    temp1 * ec
    if(m == 3):
    temp1 * ed
    if(m == 4):
    temp1 * ee
    if(m == 5):
    temp1 * ef
    if(m == 6):
    temp1 * eg
    if(m == 7):
    temp1 * eh
    while len(test) >= j:
    with open("clone.test", 'wb') as k:
    k.write(test.decode("hex"))
    ti2 = int(round(time.time() * 1000))
    print "done!"
    print test
    print (ti2-ti1)
    print ti1
    print (ti2)
    break









    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I decided to create a python code to encrypt my data a while back, when i am half way through, i decided to do a performance test, and the result is horrible, like only 430kB/s encrypting speed. Opening System monitor showed that only 1 thread of my 8C16T processor is being used by my program.



      After I try temporary deleting the encrypting part in my code, it was only like 10kB/s faster. So i analyzed the code and found out this part of code



      with open("10MB.test", 'rb') as f:  
      byte = f.read(1)
      if(i == 0):
      test = (str("".join([ch.encode("hex") for line in byte for ch in line])))
      i = 1
      while byte != "":
      g = g + 1
      byte = f.read(1)
      a = str("".join([ch.encode("hex") for line in byte for ch in line]))
      test = test + a


      runs very slow, and system monitor shows that it only uses one thread of my cpu . Is there a way to utilize all thread of my CPU to make this run faster?



      import time
      from datetime import datetime
      realsr = ""
      test = ""
      i = 0
      m = 0
      g = 2
      j = 0
      ti1 = 0
      ti2 = 0
      ea = raw_input("Please Input Your First Password: ")
      print "You Entered:",ea
      eb = raw_input("Please Input Your Second Password: ")
      print "You Entered:",eb
      ec = raw_input("Please Input Your Third Password: ")
      print "You Entered:",ec
      ed = raw_input("Please Input Your Forth Password: ")
      print "You Entered:",ed
      ee = raw_input("Please Input Your Fifth Password: ")
      print "You Entered:",ee
      ef = raw_input("Please Input Your Sixth Password: ")
      print "You Entered:",ef
      eg = raw_input("Please Input Your Seventh Password: ")
      print "You Entered:",eg
      eh = raw_input("Please Input Your Eighth Password: ")
      print "You Entered:",eh
      ti1 = int(round(time.time() * 1000))
      with open("10MB.test", 'rb') as d:
      j = len(str("".join([ch.encode("hex") for line in d for ch in line])))
      with open("10MB.test", 'rb') as f:
      byte = f.read(1)
      if(i == 0):
      test = (str("".join([ch.encode("hex") for line in byte for ch in line])))
      i = 1
      while byte != "":
      g = g + 1
      byte = f.read(1)
      a = str("".join([ch.encode("hex") for line in byte for ch in line]))
      test = test + a
      if(j != len(test)):
      temp1 = int(a,16)
      if(m == 0):
      temp1 * ea
      if(m == 1):
      temp1 * eb
      if(m == 2):
      temp1 * ec
      if(m == 3):
      temp1 * ed
      if(m == 4):
      temp1 * ee
      if(m == 5):
      temp1 * ef
      if(m == 6):
      temp1 * eg
      if(m == 7):
      temp1 * eh
      while len(test) >= j:
      with open("clone.test", 'wb') as k:
      k.write(test.decode("hex"))
      ti2 = int(round(time.time() * 1000))
      print "done!"
      print test
      print (ti2-ti1)
      print ti1
      print (ti2)
      break









      share|improve this question















      I decided to create a python code to encrypt my data a while back, when i am half way through, i decided to do a performance test, and the result is horrible, like only 430kB/s encrypting speed. Opening System monitor showed that only 1 thread of my 8C16T processor is being used by my program.



      After I try temporary deleting the encrypting part in my code, it was only like 10kB/s faster. So i analyzed the code and found out this part of code



      with open("10MB.test", 'rb') as f:  
      byte = f.read(1)
      if(i == 0):
      test = (str("".join([ch.encode("hex") for line in byte for ch in line])))
      i = 1
      while byte != "":
      g = g + 1
      byte = f.read(1)
      a = str("".join([ch.encode("hex") for line in byte for ch in line]))
      test = test + a


      runs very slow, and system monitor shows that it only uses one thread of my cpu . Is there a way to utilize all thread of my CPU to make this run faster?



      import time
      from datetime import datetime
      realsr = ""
      test = ""
      i = 0
      m = 0
      g = 2
      j = 0
      ti1 = 0
      ti2 = 0
      ea = raw_input("Please Input Your First Password: ")
      print "You Entered:",ea
      eb = raw_input("Please Input Your Second Password: ")
      print "You Entered:",eb
      ec = raw_input("Please Input Your Third Password: ")
      print "You Entered:",ec
      ed = raw_input("Please Input Your Forth Password: ")
      print "You Entered:",ed
      ee = raw_input("Please Input Your Fifth Password: ")
      print "You Entered:",ee
      ef = raw_input("Please Input Your Sixth Password: ")
      print "You Entered:",ef
      eg = raw_input("Please Input Your Seventh Password: ")
      print "You Entered:",eg
      eh = raw_input("Please Input Your Eighth Password: ")
      print "You Entered:",eh
      ti1 = int(round(time.time() * 1000))
      with open("10MB.test", 'rb') as d:
      j = len(str("".join([ch.encode("hex") for line in d for ch in line])))
      with open("10MB.test", 'rb') as f:
      byte = f.read(1)
      if(i == 0):
      test = (str("".join([ch.encode("hex") for line in byte for ch in line])))
      i = 1
      while byte != "":
      g = g + 1
      byte = f.read(1)
      a = str("".join([ch.encode("hex") for line in byte for ch in line]))
      test = test + a
      if(j != len(test)):
      temp1 = int(a,16)
      if(m == 0):
      temp1 * ea
      if(m == 1):
      temp1 * eb
      if(m == 2):
      temp1 * ec
      if(m == 3):
      temp1 * ed
      if(m == 4):
      temp1 * ee
      if(m == 5):
      temp1 * ef
      if(m == 6):
      temp1 * eg
      if(m == 7):
      temp1 * eh
      while len(test) >= j:
      with open("clone.test", 'wb') as k:
      k.write(test.decode("hex"))
      ti2 = int(round(time.time() * 1000))
      print "done!"
      print test
      print (ti2-ti1)
      print ti1
      print (ti2)
      break






      python python-2.7 python-multithreading






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 7:22









      DYZ

      24.6k61948




      24.6k61948










      asked Nov 11 at 7:17









      hulipa

      1




      1
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote













          You read your file one byte at a time: f.read(1). This is a horrible idea. You should read as much as you can safely fit in the memory (perhaps the whole file, if the memory size permits) and then do encryption.






          share|improve this answer





















          • i originally intended to encrypt the file byte by byte, but i think i should have a try at encrypting whole file
            – hulipa
            Nov 11 at 7:39










          • You can still encrypt it byte by byte. Just don't read it byte by byte.
            – DYZ
            Nov 11 at 16:52


















          up vote
          0
          down vote













          If you need to access all your CPU cores you will need use Pythons Multiprocessing module.



          And from the docs;




          In multiprocessing, processes are spawned by creating a Process object
          and then calling its start() method. Process follows the API of
          threading.Thread. A trivial example of a multiprocess program is




          from multiprocessing import Process

          def f(name):
          print('hello', name)

          if __name__ == '__main__':
          p = Process(target=f, args=('bob',))
          p.start()
          p.join()





          share|improve this answer





















          • Threading module is also an option.
            – Milan Velebit
            Nov 11 at 8:09










          • That is true but with the OP having an i9 processor I think he will get more speed with multiprocessing than multi threading
            – Jack Herer
            Nov 11 at 8:25


















          up vote
          0
          down vote













          Instead of using "byte = f.read(1)" in order to load 1 byte from the file, use this :



          content = f.read()
          for byte in content:
          # your code


          With this method, you could won in speed execution because you don't stop reading between all bytes. You only access the file once.






          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%2f53246630%2fhow-should-i-load-a-file-in-python-efficiently-using-all-my-cpu-power%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
            1
            down vote













            You read your file one byte at a time: f.read(1). This is a horrible idea. You should read as much as you can safely fit in the memory (perhaps the whole file, if the memory size permits) and then do encryption.






            share|improve this answer





















            • i originally intended to encrypt the file byte by byte, but i think i should have a try at encrypting whole file
              – hulipa
              Nov 11 at 7:39










            • You can still encrypt it byte by byte. Just don't read it byte by byte.
              – DYZ
              Nov 11 at 16:52















            up vote
            1
            down vote













            You read your file one byte at a time: f.read(1). This is a horrible idea. You should read as much as you can safely fit in the memory (perhaps the whole file, if the memory size permits) and then do encryption.






            share|improve this answer





















            • i originally intended to encrypt the file byte by byte, but i think i should have a try at encrypting whole file
              – hulipa
              Nov 11 at 7:39










            • You can still encrypt it byte by byte. Just don't read it byte by byte.
              – DYZ
              Nov 11 at 16:52













            up vote
            1
            down vote










            up vote
            1
            down vote









            You read your file one byte at a time: f.read(1). This is a horrible idea. You should read as much as you can safely fit in the memory (perhaps the whole file, if the memory size permits) and then do encryption.






            share|improve this answer












            You read your file one byte at a time: f.read(1). This is a horrible idea. You should read as much as you can safely fit in the memory (perhaps the whole file, if the memory size permits) and then do encryption.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 7:21









            DYZ

            24.6k61948




            24.6k61948












            • i originally intended to encrypt the file byte by byte, but i think i should have a try at encrypting whole file
              – hulipa
              Nov 11 at 7:39










            • You can still encrypt it byte by byte. Just don't read it byte by byte.
              – DYZ
              Nov 11 at 16:52


















            • i originally intended to encrypt the file byte by byte, but i think i should have a try at encrypting whole file
              – hulipa
              Nov 11 at 7:39










            • You can still encrypt it byte by byte. Just don't read it byte by byte.
              – DYZ
              Nov 11 at 16:52
















            i originally intended to encrypt the file byte by byte, but i think i should have a try at encrypting whole file
            – hulipa
            Nov 11 at 7:39




            i originally intended to encrypt the file byte by byte, but i think i should have a try at encrypting whole file
            – hulipa
            Nov 11 at 7:39












            You can still encrypt it byte by byte. Just don't read it byte by byte.
            – DYZ
            Nov 11 at 16:52




            You can still encrypt it byte by byte. Just don't read it byte by byte.
            – DYZ
            Nov 11 at 16:52












            up vote
            0
            down vote













            If you need to access all your CPU cores you will need use Pythons Multiprocessing module.



            And from the docs;




            In multiprocessing, processes are spawned by creating a Process object
            and then calling its start() method. Process follows the API of
            threading.Thread. A trivial example of a multiprocess program is




            from multiprocessing import Process

            def f(name):
            print('hello', name)

            if __name__ == '__main__':
            p = Process(target=f, args=('bob',))
            p.start()
            p.join()





            share|improve this answer





















            • Threading module is also an option.
              – Milan Velebit
              Nov 11 at 8:09










            • That is true but with the OP having an i9 processor I think he will get more speed with multiprocessing than multi threading
              – Jack Herer
              Nov 11 at 8:25















            up vote
            0
            down vote













            If you need to access all your CPU cores you will need use Pythons Multiprocessing module.



            And from the docs;




            In multiprocessing, processes are spawned by creating a Process object
            and then calling its start() method. Process follows the API of
            threading.Thread. A trivial example of a multiprocess program is




            from multiprocessing import Process

            def f(name):
            print('hello', name)

            if __name__ == '__main__':
            p = Process(target=f, args=('bob',))
            p.start()
            p.join()





            share|improve this answer





















            • Threading module is also an option.
              – Milan Velebit
              Nov 11 at 8:09










            • That is true but with the OP having an i9 processor I think he will get more speed with multiprocessing than multi threading
              – Jack Herer
              Nov 11 at 8:25













            up vote
            0
            down vote










            up vote
            0
            down vote









            If you need to access all your CPU cores you will need use Pythons Multiprocessing module.



            And from the docs;




            In multiprocessing, processes are spawned by creating a Process object
            and then calling its start() method. Process follows the API of
            threading.Thread. A trivial example of a multiprocess program is




            from multiprocessing import Process

            def f(name):
            print('hello', name)

            if __name__ == '__main__':
            p = Process(target=f, args=('bob',))
            p.start()
            p.join()





            share|improve this answer












            If you need to access all your CPU cores you will need use Pythons Multiprocessing module.



            And from the docs;




            In multiprocessing, processes are spawned by creating a Process object
            and then calling its start() method. Process follows the API of
            threading.Thread. A trivial example of a multiprocess program is




            from multiprocessing import Process

            def f(name):
            print('hello', name)

            if __name__ == '__main__':
            p = Process(target=f, args=('bob',))
            p.start()
            p.join()






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 11 at 7:59









            Jack Herer

            321112




            321112












            • Threading module is also an option.
              – Milan Velebit
              Nov 11 at 8:09










            • That is true but with the OP having an i9 processor I think he will get more speed with multiprocessing than multi threading
              – Jack Herer
              Nov 11 at 8:25


















            • Threading module is also an option.
              – Milan Velebit
              Nov 11 at 8:09










            • That is true but with the OP having an i9 processor I think he will get more speed with multiprocessing than multi threading
              – Jack Herer
              Nov 11 at 8:25
















            Threading module is also an option.
            – Milan Velebit
            Nov 11 at 8:09




            Threading module is also an option.
            – Milan Velebit
            Nov 11 at 8:09












            That is true but with the OP having an i9 processor I think he will get more speed with multiprocessing than multi threading
            – Jack Herer
            Nov 11 at 8:25




            That is true but with the OP having an i9 processor I think he will get more speed with multiprocessing than multi threading
            – Jack Herer
            Nov 11 at 8:25










            up vote
            0
            down vote













            Instead of using "byte = f.read(1)" in order to load 1 byte from the file, use this :



            content = f.read()
            for byte in content:
            # your code


            With this method, you could won in speed execution because you don't stop reading between all bytes. You only access the file once.






            share|improve this answer



























              up vote
              0
              down vote













              Instead of using "byte = f.read(1)" in order to load 1 byte from the file, use this :



              content = f.read()
              for byte in content:
              # your code


              With this method, you could won in speed execution because you don't stop reading between all bytes. You only access the file once.






              share|improve this answer

























                up vote
                0
                down vote










                up vote
                0
                down vote









                Instead of using "byte = f.read(1)" in order to load 1 byte from the file, use this :



                content = f.read()
                for byte in content:
                # your code


                With this method, you could won in speed execution because you don't stop reading between all bytes. You only access the file once.






                share|improve this answer














                Instead of using "byte = f.read(1)" in order to load 1 byte from the file, use this :



                content = f.read()
                for byte in content:
                # your code


                With this method, you could won in speed execution because you don't stop reading between all bytes. You only access the file once.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 11 at 8:21









                quant

                1,58411526




                1,58411526










                answered Nov 11 at 8:07









                Calvin-Ruiz

                286




                286






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246630%2fhow-should-i-load-a-file-in-python-efficiently-using-all-my-cpu-power%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

                    Florida Star v. B. J. F.

                    Danny Elfman

                    Lugert, Oklahoma