Well, scrambleLine will remove line-endings, so when you're
descrambling
you'll be processing the entire file at once. This is particularly bad
because of the way your functions work, adding a character at a time
to
s.

Probably your easiest bet is to iterate over the file using read(N)
for some small N rather than doing a line at a time. Something like:

Re: I could use some help making this Python code run faster usingonly Python code.

On 9/20/07, Python Maniac <> wrote:
> I am new to Python however I would like some feedback from those who
> know more about Python than I do at this time.

Well, you could save some time by not applying the scramble one line
at a time (that is if you don't mind losing the line endings in the
scrambled version). For that to be effective though, you probably want
to open in binary mode. Also, your scramble can be written using list
comprehension.

If you use xor (^) as above, you can use the same method for scramble
as descramble (running it again with the same key will descramble) and
you can use an arbitrary key. Though, with 255 combinations, it isn't
very strong encryption.

If you want stronger encryption you can use the following AESish algorithm:

This allows you to use much larger keys, but with a similar effect.
Still not strong enough to be unbreakable, but much better than the
origional. It is strong enough that someone knowing how you scrambled
it will have trouble unscrambling it even if they don't know the key.

On Sep 20, 3:57 pm, "Matt McCredie" <> wrote:
> On 9/20/07, Python Maniac <> wrote:
>
> > I am new to Python however I would like some feedback from those who
> > know more about Python than I do at this time.
>
> Well, you could save some time by not applying the scramble one line
> at a time (that is if you don't mind losing the line endings in the
> scrambled version). For that to be effective though, you probably want
> to open in binary mode. Also, your scramble can be written using list
> comprehension.
>
>

>
> If you use xor (^) as above, you can use the same method for scramble
> as descramble (running it again with the same key will descramble) and
> you can use an arbitrary key. Though, with 255 combinations, it isn't
> very strong encryption.
>
> If you want stronger encryption you can use the following AESish algorithm:
>
>

>
> This allows you to use much larger keys, but with a similar effect.
> Still not strong enough to be unbreakable, but much better than the
> origional. It is strong enough that someone knowing how you scrambled
> it will have trouble unscrambling it even if they don't know the key.
>
> Matt

So far I like what was said in this reply however my ultimate goal is
to allow the scramble method to be more than what is shown above.

I considered using XOR however XOR only works for the case shown above
where the scramble method simply sets or removes the MSB.

What I want to be able to do is set or reset the MSB in addition to
performing a series of additional steps without negatively impacting
performance for the later cases when the MSB is not the only technique
being employed.

Hopefully this sheds more light on the goal I have in mind.

BTW - My original code is able to scramble a 20 MB file in roughly 40
secs using a Dell E6600 2.4 GHz. When I began writing the code for
this problem my best runtime was about 65 secs so I know I was heading
in the right direction.

I was about to begin the process of using the D Language and pyd to
gain better performance but then I thought I might also take this
opportunity to learn something about Python before delving into D.
Obviously I could simply code the whole process using D but that
defeats the purpose for using Python in the first place and so I would
tend to limit my low-level coding to the task of scrammbling each line
of text.

The ironic thing about this exorcise was the fact that the
optimization techniques that worked for Python caused the Ruby version
I coded to decrease performance. It seems Ruby has some definite
ideas about what it feels is optimal that it ain't got much to do with
what I would consider to be traditional optimization techniques where
Ruby is concerned. For instance, Ruby felt the putc method was less
optimal than the write method which makes no sense to me but that's
life with Ruby.

I got similar results as well. I believe the reason for join actually
performing slower is because join iterates twice over the sequence. [1]
The first time is to determine the size of the buffer to allocate and
the second is to populate the buffer.

It has to do with the input string length; try multiplying it by 10 or
100. Below is a more complete benchmark; for largish strings, the imap
version is the fastest among those using the original algorithm. Of
course using a lookup table as Diez showed is even faster. FWIW, here
are some timings (Python 2.5, WinXP):

George Sakkis <> wrote:
> It has to do with the input string length; try multiplying it by 10 or
> 100. Below is a more complete benchmark; for largish strings, the imap
> version is the fastest among those using the original algorithm. Of
> course using a lookup table as Diez showed is even faster. FWIW, here
> are some timings (Python 2.5, WinXP):
>
> scramble: 1.818
> scramble_listcomp: 1.492
> scramble_gencomp: 1.535
> scramble_map: 1.377
> scramble_imap: 1.332
> scramble_dict: 0.817
> scramble_dict_map: 0.419
> scramble_dict_imap: 0.410

I added another one:

import string
scramble_translation = string.maketrans(''.join(chr(i) for i in xrange
(256)), ''.join(chr(i|0x80) for i in xrange(256)))
def scramble_translate(line):
return string.translate(line, scramble_translation)

The difference between where I began (70.155 secs) and where we end
(2.278 secs) is a whopping 30.8x faster using some rather simple
techniques that are nothing more than variations on the theme of
hoisting function calls out of loops along with using some very
powerful iterator functions from Python.

My best runtime with Ruby using the same machine and OS was 67.797
secs which is 29.8x slower than the fastest Python runtime. This
makes Ruby almost as slow as Python was made faster. The irony with
Ruby was that the use of a hash in Ruby actually made the Ruby code
run slower than when a hash was not used.

Now I think I will code this little scrambler using nothing but the D
Language just to see whether there is any benefit in using D over
Python for this sort of problem.

On Sep 21, 1:00 pm, Python Maniac <> wrote:
> My best runtime with Ruby using the same machine and OS was 67.797
> secs which is 29.8x slower than the fastest Python runtime. This
> makes Ruby almost as slow as Python was made faster. The irony with
> Ruby was that the use of a hash in Ruby actually made the Ruby code
> run slower than when a hash was not used.

I'm not familiar with Ruby but chances are that if you post at
c.l.ruby you'll get some good suggestions on how to speed up your ruby
code too (although the difference may not be so dramatic).
> Now I think I will code this little scrambler using nothing but the D
> Language just to see whether there is any benefit in using D over
> Python for this sort of problem.

And then you'll do it in assembly to see how much do you gain compared
to D ? If this is not just for learning purposes, perhaps you should
be thinking it the other way around: how fast is fast enough ?
Optimizing just for the sake of optimization, without a specific
performance goal in mind, is not the most productive way to do things
(again, leaving learning motivations aside).

On Sep 21, 3:02 pm, "Matt McCredie" <> wrote:
> > Now I think I will code this little scrambler using nothing but the D
> > Language just to see whether there is any benefit in using D over
> > Python for this sort of problem.
>
> Isn't D compiled to machine code? I would expect it to win hands down.
> That is, unless it is horribly unoptimized.
>
> Matt

Well D code is compiled into machine code that runs via a VM.

My initial D code ran in about 6 secs as compare with the 2.278 secs
for the optimized Python code.

If I want the D code to run faster than the optimized Python I would
have to use the same Pythonic optimizations as were used in Python
when crafting the D code and then I would guess the optimized D code
might run only 2x faster than the optimized Python code.

In real terms < 3 secs to process a 20 MB file is more than reasonable
performance with no need to perform any additional optimizations.

For this particular problem Python performs as well as the D powered
machine code using far less effort, for me, than what it would take to
make the D code run faster than the Python code.

All this tells me the following:

* List Comprehensions are very powerful for Python.
* String translation is a must when processing string based data in an
iterative manner.
* Ruby has no hope of being able to beat Python for this type of
problem given the proper Python optimizations are used.
* There is no value in wasting time with lower-level languages to make
Python run faster for this type of problem.

It would be nice if Python could be made to automatically detect the
LC and string translation patterns used by the unoptimized Python code
and make them into optimized Python code on the fly at runtime. I am
more than a little amazed nobody has chosen to build a JIT (Just In-
Time compiler) or cached-compiler into Python but maybe that sort of
thing is just not needed given the fact that Python code can be easily
optimized to run 30x faster.

Re: I could use some help making this Python code run faster usingonly Python code.

> It would be nice if Python could be made to automatically detect the
> LC and string translation patterns used by the unoptimized Python code
> and make them into optimized Python code on the fly at runtime. I am
> more than a little amazed nobody has chosen to build a JIT (Just In-
> Time compiler) or cached-compiler into Python but maybe that sort of
> thing is just not needed given the fact that Python code can be easily
> optimized to run 30x faster.

See PyPy http://codespeak.net/pypy/ for a JIT comiler for python.
Although it is in the research phase, but worth taking a look at.

On Sep 21, 4:48 pm, "Matt McCredie" <> wrote:
> > It would be nice if Python could be made to automatically detect the
> > LC and string translation patterns used by the unoptimized Python code
> > and make them into optimized Python code on the fly at runtime. I am
> > more than a little amazed nobody has chosen to build a JIT (Just In-
> > Time compiler) or cached-compiler into Python but maybe that sort of
> > thing is just not needed given the fact that Python code can be easily
> > optimized to run 30x faster.
>
> See PyPyhttp://codespeak.net/pypy/for a JIT comiler for python.
> Although it is in the research phase, but worth taking a look at.
>
> Matt

You need to check-out a project called Psyco (forerunner for pypy).

I was able to get almost 2x better performance by adding 3 lines of
code for Psyco.

Share This Page

Welcome to The Coding Forums!

Welcome to the Coding Forums, the place to chat about anything related to programming and coding languages.

Please join our friendly community by clicking the button below - it only takes a few seconds and is totally free. You'll be able to ask questions about coding or chat with the community and help others.
Sign up now!