Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is this make sence? Dynamic assembler for python

4 views
Skip to first unread message

DivX

unread,
Jun 19, 2010, 6:53:38 PM6/19/10
to
I found on the forum some discussion about crypting text and one guy
did make assembly implementation of crypting algorithm. He dynamically
generates mashine code and call that from python. Here are impressive
results http://www.daniweb.com/code/snippet216632-5.html

Is this better approach then writing extensions in c?

geremy condra

unread,
Jun 19, 2010, 7:18:53 PM6/19/10
to DivX, pytho...@python.org

No, xor cipher is not suitable for general purpose encryption, and what do you
need the speed for? xor is almost certainly not going to be the bottleneck in
your application.

Geremy Condra

DivX

unread,
Jun 19, 2010, 8:36:57 PM6/19/10
to
On 19 lip, 21:18, geremy condra <debat...@gmail.com> wrote:

> On Sat, Jun 19, 2010 at 11:53 AM, DivX <sem.r...@gmail.com> wrote:
> > I found on the forum some discussion about crypting text and one guy
> > did make assembly implementation of crypting algorithm. He dynamically
> > generates mashine code and call that from python. Here are impressive
> > resultshttp://www.daniweb.com/code/snippet216632-5.html

>
> > Is this better approach then writing extensions in c?
>
> No, xor cipher is not suitable for general purpose encryption, and what do you
> need the speed for? xor is almost certainly not going to be the bottleneck in
> your application.
>
> Geremy Condra

Just asking if this approach is good for example quicksort algoriths
or some kind of sorting algorithms, or simulations but the point is of
mixing python and assembler?

Steven D'Aprano

unread,
Jun 20, 2010, 12:52:15 AM6/20/10
to


Ask yourself, why aren't programs written in assembly if it's so good?

(1) It's platform dependent. Do you really need a separate program for
every single hardware platform you want to run Quicksort on?

(2) Writing assembler is hard, really hard. And even harder to debug.

(3) Modern C compilers can produce better (faster, more efficient)
machine code than the best assembly code written by hand.


Honestly, this question has been resolved twenty years ago -- thirty
years ago, maybe there was still a good point in writing general purpose
code in assembly, but now? It's just showing off. Unless you're writing
hardware specific code (e.g. device drivers) it is pointless, in my
opinion.

I think that mixing assembly and python is a gimmick of very little
practical significance. If you really need the extra performance, check
out PyPy, Cython, Pyrex and Psyco.

--
Steven

Terry Reedy

unread,
Jun 20, 2010, 1:08:16 AM6/20/10
to pytho...@python.org

You have to define 'better'. This approach requires someone to write
template assembler code, which will be machine specific. To be faster
than compiled C on a particular machine, one must be pretty good at
assemblee also.

Message has been deleted

DivX

unread,
Jun 20, 2010, 10:19:48 AM6/20/10
to
On 20 lip, 02:52, Steven D'Aprano <st...@REMOVE-THIS-

I can agree with you about most of the arguments, but why he continues
to developing it. What he sees and we do not see?
If you're interested I found a link http://www.tahir007.com/

Steven D'Aprano

unread,
Jun 20, 2010, 10:46:37 AM6/20/10
to
On Sun, 20 Jun 2010 03:19:48 -0700, DivX wrote:

> On 20 lip, 02:52, Steven D'Aprano <st...@REMOVE-THIS-
> cybersource.com.au> wrote:

[...]


>> I think that mixing assembly and python is a gimmick of very little
>> practical significance. If you really need the extra performance, check
>> out PyPy, Cython, Pyrex and Psyco.
>>
>> --
>> Steven
>
> I can agree with you about most of the arguments, but why he continues
> to developing it. What he sees and we do not see?

Why ask us? You should ask him.


--
Steven

DivX

unread,
Jun 20, 2010, 10:57:27 AM6/20/10
to
On 20 lip, 12:46, Steven D'Aprano <st...@REMOVE-THIS-

cybersource.com.au> wrote:
> On Sun, 20 Jun 2010 03:19:48 -0700, DivX wrote:
> > On 20 lip, 02:52, Steven D'Aprano <st...@REMOVE-THIS-
> > cybersource.com.au> wrote:
> [...]
> >> I think that mixing assembly and python is a gimmick of very little
> >> practical significance. If you really need the extra performance, check
> >> out PyPy, Cython, Pyrex and Psyco.
>
> >> --
> >> Steven
>
> > I can agree with you about most of the arguments, but why he continues
> > to developing it. What he sees and we do not see?
>
> Why ask us? You should ask him.
>
> --
> Steven

Be sure I will ask him, but before, I wanted to know your opinions
about it. Hear arguments on both sides...

Lie Ryan

unread,
Jun 20, 2010, 4:41:18 PM6/20/10
to

Ever heard of JIT?

Terry Reedy

unread,
Jun 20, 2010, 5:36:13 PM6/20/10
to pytho...@python.org
On 6/20/2010 12:41 PM, Lie Ryan wrote:
> On 06/20/10 20:57, DivX wrote:
>> On 20 lip, 12:46, Steven D'Aprano<st...@REMOVE-THIS-
>> cybersource.com.au> wrote:
>>> On Sun, 20 Jun 2010 03:19:48 -0700, DivX wrote:
>>>> On 20 lip, 02:52, Steven D'Aprano<st...@REMOVE-THIS-
>>>> cybersource.com.au> wrote:
>>> [...]
>>>>> I think that mixing assembly and python is a gimmick of very little
>>>>> practical significance. If you really need the extra performance, check
>>>>> out PyPy, Cython, Pyrex and Psyco.

Of course, Psyco mixes assembly and python, which is why it was
originally (not sure now) limited to x86 machines. It does do
generically, in small chunks, behind the scenes, so the rest of us can
get the benefit without expert assembly knowledge.

>>>> I can agree with you about most of the arguments, but why he continues
>>>> to developing it.

I presume because he can and enjoys it. That sort of tinkering is what
led to Psyco.

> Ever heard of JIT?

Psyco is one form of JIT. You might enjoy reading about how it works.

Terry Jan Reedy

Rhodri James

unread,
Jun 20, 2010, 9:45:14 PM6/20/10
to
Mixing Python and assembler is a bizarre thing to want to do in general,
but...

On Sun, 20 Jun 2010 01:52:15 +0100, Steven D'Aprano
<st...@remove-this-cybersource.com.au> wrote:

> (3) Modern C compilers can produce better (faster, more efficient)
> machine code than the best assembly code written by hand.

No. Modern C compilers often produce very good machine code, but the best
hand-written assembly code will be better. I can usually write *very*
marginally better code than GCC achieves at work, though 99% of the time I
don't because it would be a maintenance nightmare. I wrote more at a
previous job, but then the processor had instructions that could not be
expressed usefully in C, and having an FFT routine that ran like greased
lightning was important.

I would agree, though, that the only reason I can think of for wanting to
mix Python and assembler is if you are grobbling about with hardware, and
if you are doing that, Python wasn't the best place to start from.

--
Rhodri James *-* Wildebeeste Herder to the Masses

Dave Angel

unread,
Jun 20, 2010, 10:21:43 PM6/20/10
to DivX, pytho...@python.org
DivX wrote:
> On 20 lip, 12:46, Steven D'Aprano <st...@REMOVE-THIS-
> cybersource.com.au> wrote:
>
>> On Sun, 20 Jun 2010 03:19:48 -0700, DivX wrote:
>>
>>> On 20 lip, 02:52, Steven D'Aprano <st...@REMOVE-THIS-
>>> cybersource.com.au> wrote:
>>>
>> [...]
>>
>>>> I think that mixing assembly and python is a gimmick of very little
>>>> practical significance. If you really need the extra performance, check
>>>> out PyPy, Cython, Pyrex and Psyco.
>>>>
>>>> --
>>>> Steven
>>>>
>>> I can agree with you about most of the arguments, but why he continues
>>> to developing it. What he sees and we do not see?
>>>
>> Why ask us? You should ask him.
>>
>> --
>> Steven
>>
>
> Be sure I will ask him, but before, I wanted to know your opinions
> about it. Hear arguments on both sides...
>
>
Something's intrinsically wrong with the argument made in this thread
against generating assembly code. That's exactly what happens every
time you write code in C. The real question is whether the code
generator your friend is writing is better than the ones written by
dozens of C gurus over the years, and better tuned to the requirements
of his particular processor. Naturally, better can be measured in
several ways.


For example, I have a processor for which no C compiler is available.
So if I were to want optimized assembler, I might need to write one
myself, or use the language for which such a code generator has been
written.


DaveA

Steven D'Aprano

unread,
Jun 21, 2010, 3:28:43 AM6/21/10
to
On Sun, 20 Jun 2010 18:21:43 -0400, Dave Angel wrote:

> Something's intrinsically wrong with the argument made in this thread
> against generating assembly code. That's exactly what happens every
> time you write code in C.

I don't know whether C compilers generate assembly mnemonics or direct
machine code, but the distinction for this argument is irrelevant.

The argument in this thread is that it's not worth the *human coder*
writing assembly, not that no assembly code is involved in the process
anywhere.

--
Steven

Steven D'Aprano

unread,
Jun 21, 2010, 3:34:40 AM6/21/10
to
On Sun, 20 Jun 2010 22:45:14 +0100, Rhodri James wrote:

> Mixing Python and assembler is a bizarre thing to want to do in general,
> but...
>
> On Sun, 20 Jun 2010 01:52:15 +0100, Steven D'Aprano
> <st...@remove-this-cybersource.com.au> wrote:
>
>> (3) Modern C compilers can produce better (faster, more efficient)
>> machine code than the best assembly code written by hand.
>
> No. Modern C compilers often produce very good machine code, but the
> best hand-written assembly code will be better. I can usually write
> *very* marginally better code than GCC achieves at work, though 99% of
> the time I don't because it would be a maintenance nightmare.


Not that I don't believe you, but that is an extraordinary claim that
would require more evidence than just "Hey, some guy on the Internet
reckons his assembly code can regularly out-perform optimizing C
compilers" before I will change my opinion *wink*

Of course, there almost certainly are special cases where the state of
the art for optimizing C compilers isn't as good as a clever human, e.g.
if you're writing for hardware where no optimizing compiler exists at
all, or some tiny CPU without all the pipelines and predictive branching
crap^W stuff they do these days.

--
Steven

Paul Rubin

unread,
Jun 21, 2010, 4:37:15 AM6/21/10
to
Steven D'Aprano <steve-RE...@cybersource.com.au> writes:
> Not that I don't believe you, but that is an extraordinary claim that
> would require more evidence than just "Hey, some guy on the Internet
> reckons his assembly code can regularly out-perform optimizing C
> compilers" before I will change my opinion *wink*

It is really true, and it's the opposite claim that would be
extraordinary. That's why compilers like gcc include pragmas for
assembler intrinsics for when you want include some inline assembly code
in your C program. gzip uses it for example, as does Gnu MP and
OpenSSL. That's why gmpy is several times faster at multi-precision
arithmetic than Python's built-in longs, that are implemented in C.

Another reason for wanting dynamic assembly code is so you can embed
staged programming in your python code. Think of how slow python
regexps are. Now imagine a regexp package that compiles your regexp
directly to assembly code instead of to some silly interpreted state
machine. Parser generators like pyparsing could similarly generate asm
code directly.

Dave Angel

unread,
Jun 21, 2010, 7:00:12 AM6/21/10
to Steven D'Aprano, pytho...@python.org
But the OP said of his friend:

"He dynamically generates mashine code and call that from python."

I took that to mean he dynamically generated machine code, not that he hired some human to do it.

DaveA


Carl Banks

unread,
Jun 21, 2010, 9:40:42 AM6/21/10
to

Well we know what you meant, but he did post a snippet of the code
showing handwritten assembly, from which the machine code is
dynamically generated.

Inline assembly not too useful for general purpose Python programming,
but I'm sure there's a time and place for it.

I wonder how easy it'd be to bundle a small C compiler.


Carl Banks

Steven D'Aprano

unread,
Jun 21, 2010, 10:13:23 AM6/21/10
to
On Mon, 21 Jun 2010 03:00:12 -0400, Dave Angel wrote:


> But the OP said of his friend:
>
> "He dynamically generates mashine code and call that from python."
>
> I took that to mean he dynamically generated machine code, not that he
> hired some human to do it.


Well, I suppose if his friend is a robot or alien intelligence, you could
very well be right. *wink*

Otherwise, whether his friends writes the assembly, or he hires someone
to do it, what's the difference?

(If you follow the OP's link to the code, it seems fairly clear to me
that it uses hand-written assembly code.)

--
Steven

DivX

unread,
Jun 21, 2010, 10:53:07 AM6/21/10
to

Where I wrote that he was my friend? But that is not the point, I send
him a mail and here is answer:

"I know that writing assembly code is hard but when you want some
simple algorithm like algorithms in image processing or in digital
signal processing you must write functions in C and then write wrapper
to call that from python because it’s slow to do all work from python.
And then if you want to support windows and Linux you will spend more
time about compiling and see if everything works correctly than on
algorithms.

Another thing is that when you have assembler now you can write some
small C compiler so that you don’t have to write assembly language.
It’s doesn’t matter if gcc will produce better code it’s enough to
achieve speed that you are satisfied and you don’t have to worry
about how to write extensions for Python.

Also many people use intrinsic functions to achieve more speed when
they need. I think programming using intrinsic functions is like using
sse instructions directly.

Best regards,

Tahir"

So, thanks people for your opinions and arguments...

Dave Angel

unread,
Jun 21, 2010, 1:15:15 PM6/21/10
to pytho...@python.org
My fault. I didn't follow the link. I assumed the words in all those
messages were clear enough. So his acquaintance is writing an
assembler, not a code generator.

DaveA

David Cournapeau

unread,
Jun 21, 2010, 4:00:54 PM6/21/10
to Steven D'Aprano, pytho...@python.org
On Mon, Jun 21, 2010 at 12:34 PM, Steven D'Aprano
<steve-RE...@cybersource.com.au> wrote:
> On Sun, 20 Jun 2010 22:45:14 +0100, Rhodri James wrote:
>
>> Mixing Python and assembler is a bizarre thing to want to do in general,
>> but...
>>
>> On Sun, 20 Jun 2010 01:52:15 +0100, Steven D'Aprano
>> <st...@remove-this-cybersource.com.au> wrote:
>>
>>> (3) Modern C compilers can produce better (faster, more efficient)
>>> machine code than the best assembly code written by hand.
>>
>> No.  Modern C compilers often produce very good machine code, but the
>> best hand-written assembly code will be better.  I can usually write
>> *very* marginally better code than GCC achieves at work, though 99% of
>> the time I don't because it would be a maintenance nightmare.
>
>
> Not that I don't believe you, but that is an extraordinary claim that
> would require more evidence than just "Hey, some guy on the Internet
> reckons his assembly code can regularly out-perform optimizing C
> compilers" before I will change my opinion *wink*

No, it is not extraordinary claim, I would actually think it is common
knowledge. For performance sensitive, extremely well constraints
algorithm, ASM easily beats general C compilers. Most optimized
numerical libraries rely quite heavily on ASM to get significant speed
up (Atlas, FFTW, MKL). C has actually a few majors aspects which makes
some optimizations very hard to do - things like pointer aliasing, for
example, where the compiler has to be quite pessimistic in general.

Also, the fact that current compilers can generate code which is
significantly faster than they used to a few years ago *on the exact
same C code* show that it is not that hard to beat C compilers. They
manage to do it by themselves :)

David

MRAB

unread,
Jun 21, 2010, 5:12:48 PM6/21/10
to pytho...@python.org
David Cournapeau wrote:
> On Mon, Jun 21, 2010 at 12:34 PM, Steven D'Aprano
> <steve-RE...@cybersource.com.au> wrote:
>> On Sun, 20 Jun 2010 22:45:14 +0100, Rhodri James wrote:
>>
>>> Mixing Python and assembler is a bizarre thing to want to do in general,
>>> but...
>>>
>>> On Sun, 20 Jun 2010 01:52:15 +0100, Steven D'Aprano
>>> <st...@remove-this-cybersource.com.au> wrote:
>>>
>>>> (3) Modern C compilers can produce better (faster, more efficient)
>>>> machine code than the best assembly code written by hand.
>>> No. Modern C compilers often produce very good machine code, but the
>>> best hand-written assembly code will be better. I can usually write
>>> *very* marginally better code than GCC achieves at work, though 99% of
>>> the time I don't because it would be a maintenance nightmare.
>>
>> Not that I don't believe you, but that is an extraordinary claim that
>> would require more evidence than just "Hey, some guy on the Internet
>> reckons his assembly code can regularly out-perform optimizing C
>> compilers" before I will change my opinion *wink*
>
> No, it is not extraordinary claim, I would actually think it is common
> knowledge. For performance sensitive, extremely well constraints
> algorithm, ASM easily beats general C compilers. Most optimized
> numerical libraries rely quite heavily on ASM to get significant speed
> up (Atlas, FFTW, MKL). C has actually a few majors aspects which makes
> some optimizations very hard to do - things like pointer aliasing, for
> example, where the compiler has to be quite pessimistic in general.
>
> Also, the fact that current compilers can generate code which is
> significantly faster than they used to a few years ago *on the exact
> same C code* show that it is not that hard to beat C compilers. They
> manage to do it by themselves :)
>
A human can write better assembly code than a compiler, but would take a
much longer, and usually for not much gain, so it's usually a waste of
time (premature optimisation, and all that).

Stephen Hansen

unread,
Jun 21, 2010, 5:26:21 PM6/21/10
to pytho...@python.org
On 6/21/10 10:12 AM, MRAB wrote:
> A human can write better assembly code than a compiler, but would take a
> much longer, and usually for not much gain, so it's usually a waste of
> time (premature optimisation, and all that).

When you get to the point where you're considering writing something in
assembly, its no longer "premature" -- you are clearly in a domain which
needs to process a lot of data and crunch it very efficiently.

Once you're at that point, there can be quite significant gain.

Fortunately, the vast majority of people, and situations, and programs,
don't ever get there.

And even more fortunately: very, very often when those people who *do*
get to that place, they actually can use a library which has already
done it and get those major performance gains without writing the
assembly themselves.

--

Stephen Hansen
... Also: Ixokai
... Mail: me+list/python (AT) ixokai (DOT) io
... Blog: http://meh.ixokai.io/

signature.asc

Rhodri James

unread,
Jun 21, 2010, 9:39:56 PM6/21/10
to
On Mon, 21 Jun 2010 04:34:40 +0100, Steven D'Aprano
<steve-RE...@cybersource.com.au> wrote:

> On Sun, 20 Jun 2010 22:45:14 +0100, Rhodri James wrote:
>
>> No. Modern C compilers often produce very good machine code, but the
>> best hand-written assembly code will be better. I can usually write
>> *very* marginally better code than GCC achieves at work, though 99% of
>> the time I don't because it would be a maintenance nightmare.
>
> Not that I don't believe you, but that is an extraordinary claim that
> would require more evidence than just "Hey, some guy on the Internet
> reckons his assembly code can regularly out-perform optimizing C
> compilers" before I will change my opinion *wink*

Fairy 'nuff. In this case it's a matter of having a simple processor and
knowing a trick or two that our in-house compiler apparently doesn't. Or
didn't, since it has improved recently. Oh, and having done this for a
decade or two :-)

But like we've been agreeing, most of the time knocking an instruction or
two out of a function isn't worth the effort.

alex23

unread,
Jun 22, 2010, 2:10:17 AM6/22/10
to
DivX <sem.r...@gmail.com> wrote:
> Another thing is that when you have assembler now you can write some
> small C compiler so that you don’t have to write assembly language.

That has to be the most paradoxical argument I've ever heard: "when
you use assembler you have the ability to not use assembler" :)

0 new messages