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
python python-2.7 python-multithreading
add a comment |
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
python python-2.7 python-multithreading
add a comment |
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
python python-2.7 python-multithreading
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
python python-2.7 python-multithreading
edited Nov 11 at 7:22
DYZ
24.6k61948
24.6k61948
asked Nov 11 at 7:17
hulipa
1
1
add a comment |
add a comment |
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.
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
add a comment |
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()
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
add a comment |
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.
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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()
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
add a comment |
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()
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
add a comment |
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()
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()
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Nov 11 at 8:21
quant
1,58411526
1,58411526
answered Nov 11 at 8:07
Calvin-Ruiz
286
286
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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