Hello everybody!
I created my own DES (data encryption standard) implementation in Python. For those who don't know, it is an encryption scheme that works on the bit level. It works flawlessly! However, my implementation seems to be extremely slow! My encryption function can work on about 20,000 bits per second. The popular Python DES implementation (pyDES) encrypts about 17,000 bits per second.
However, somebody I know wrote a similar implementation in C, and it runs 1000 times faster! When I port some arbitrary code to C, it generally runs 10-40 times faster than it does in python, but not 1000! Granted, this guy is absolutely brilliant, but something still seems a little off.
Right now, I'm representing binary numbers as bit arrays. For example, 0b1001 is handled as:
a = [1, 0, 0, 1]
The reason for this is that I need to do quite a bit of bit slicing. For example, I need to be able to take the 6-bit value 0b100101, concatenate the first and last bit (to make 0b11), and group together the middle 4 (to make 0b0010).
Right now, it is done as follows:
a = [1, 0, 0, 1, 0, 1]
b = [ a[0], a[5] ]
c = a[1:5]
I feel as if I were to use actual binary numbers (0b100101) for this, it would take longer because I would need to convert the number to a string in order to splice it. My first implementation used strings of bits ('0010011'), but it ran at half the speed of my current one.
Is there any magical bit-handling feature of python that can be used for xoring AND splicing binary numbers? I would rather use a built-in feature rather than an external library because I want to release my implementation one day and don't want to make others install libraries that they don't want.
Thanks a lot!
PS: I'm running Python 2.7.
Optimizing slow python code: bitwise operations for DES implementation
Started by scorpion990, Jul 09 2010 11:21 PM
3 replies to this topic
#1
Posted 09 July 2010 - 11:21 PM
|
|
|
#2
Posted 10 July 2010 - 11:07 PM
Why not use the python bit manipulation operators?
<<
>>
&
|
^
They are really very fast
BitManipulation - PythonInfo Wiki
See at the down..there's lots of example.
<<
>>
&
|
^
They are really very fast
BitManipulation - PythonInfo Wiki
See at the down..there's lots of example.
Be a joke unto yourself!
Check out my blog at Flashcore
Check out my blog at Flashcore
#3
Posted 13 July 2010 - 05:27 AM
C is a compiled, and Python an interpreted language so a huge difference in complex algorithms is expected. As said above use the Python bitwise operators and/or possibly compile your source code to c++ with Shed Skin ( Shed Skin - An Optimizing Python-to-C++ Compiler ).
#4
Posted 13 July 2010 - 01:30 PM
You might also want to literally embed C/C++ code in Python, you can use distutils, Swig, boost,weave,etc...
That would allow you to write a C module and simply call it's function from Python as if it was a Python module.
That would allow you to write a C module and simply call it's function from Python as if it was a Python module.


Sign In
Create Account

Back to top









