I could use some help making this Python code run faster using only Python code.

M

Marc 'BlackJack' Rintsch

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

About which D are we talking here? Not digital mars' successor to C++,
right!?

Ciao,
Marc 'BlackJack' Rintsch
 
P

Python Maniac

About which D are we talking here? Not digital mars' successor to C++,
right!?

Ciao,
Marc 'BlackJack' Rintsch

Yes, Digital Mars D is what I was referring to and yes I know D is not
as efficient as C++. If I knew of a good C++ compiler that is not
from Microsoft that works natively with Windows I would be happy to
consider using it but alas the only one I have found so far is from
Digital Mars. Digital Mars D has nice integration with Python via pyd
and this is another plus, in my mind.
 
P

Python Maniac

George Sakkis said:
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)

...
funcs = [scramble, scramble_listcomp, scramble_gencomp,
scramble_map, scramble_imap,
scramble_dict, scramble_dict_map, scramble_dict_imap,
scramble_translate
]

and I think I win:

scramble: 1.949
scramble_listcomp: 1.439
scramble_gencomp: 1.455
scramble_map: 1.470
scramble_imap: 1.546
scramble_dict: 0.914
scramble_dict_map: 0.415
scramble_dict_imap: 0.416
scramble_translate: 0.007

Some benchmarks showing the effectiveness of using Psyco:

scramble: 4.210
scramble_listcomp: 2.343
scramble_gencomp: 2.599
scramble_map: 1.960
scramble_imap: 2.231
scramble_dict: 2.387
scramble_dict_map: 0.535
scramble_dict_imap: 0.726
scramble_translate: 0.010

Now with Psyco...
psyco.bind(scramble)...
scramble: 0.121 4.088 34.670x faster
scramble_listcomp: 0.215 2.128 10.919x faster
scramble_gencomp: 2.563 0.036 1.014x faster
scramble_map: 2.002 -0.043 0.979x slower
scramble_imap: 2.175 0.056 1.026x faster
scramble_dict: 0.199 2.188 11.983x faster
scramble_dict_map: 0.505 0.029 1.058x faster
scramble_dict_imap: 0.728 -0.001 0.998x slower
scramble_translate: 0.009 0.001 1.111x faster

Overall, Psyco helped my little process of converting 20 MB worth of
ASCII into what may not look like the original at 6x faster than
without Psyco.
 
M

Marc 'BlackJack' Rintsch

Yes, Digital Mars D is what I was referring to and yes I know D is not
as efficient as C++.

But D code doesn't run in a VM unless you see hardware processors as VMs.

Ciao,
Marc 'BlackJack' Rintsch
 
M

Matt McCredie

Yes, Digital Mars D is what I was referring to and yes I know D is not
as efficient as C++. If I knew of a good C++ compiler that is not
from Microsoft that works natively with Windows I would be happy to
consider using it.

I've had good luck with MinGW (gcc compiled for windows).

http://www.mingw.org

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top