From: MRAB on
David Cournapeau wrote:
> On Mon, Jun 21, 2010 at 12:34 PM, Steven D'Aprano
> <steve-REMOVE-THIS(a)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
>>> <steve(a)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).
From: Stephen Hansen on
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/

From: Rhodri James on
On Mon, 21 Jun 2010 04:34:40 +0100, Steven D'Aprano
<steve-REMOVE-THIS(a)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.

--
Rhodri James *-* Wildebeeste Herder to the Masses
From: alex23 on
DivX <sem.r...(a)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" :)