Computer Chess Club Archives




Subject: Re: Why is assembly more effecient than C?

Author: Amir Ban

Date: 13:58:55 09/27/98

Go up one level in this thread

On September 27, 1998 at 15:41:42, Danniel Corbit wrote:

>On September 27, 1998 at 14:36:22, Robert Hyatt wrote:
>>that's only true if you use the C++ syntactical tricks without using the
>>major C++ functionality, namely object constructors/destructor stuff, which
>>is where the overhead grows quickly.
>Actually, constructors/destructors are no more or less efficient than
>malloc/free+initialization calls,

There's no reason for a constructor/destructor to be associated with a
malloc/free (or the equivalent new/delete), unless you want it to be. I don't
know why so many people assume it, but it's not true. Look at this example of
using a constructor for an auto variable:

class Klass
  int val;

  K() {val = 0;}
  ~K() {}

void foo()
  Klass ob;

  // ...

On each entry to foo, the constructor for ob is called, and on exit the
destructor is called. Sounds heavy, right ? In fact, the only thing that happens
is that the compiler inserts the instruction to set ob.val to 0 on entry, and
the destructor is empty, so the constructor + destructor amount to one machine
instruction. Assuming you really need ob.val to be initialized to 0, you can't
do better even in assembler.

> and you don't have to remember stuff.  There
>are real penalties in C++ though.  RTTI, exception handling and a few things of
>that nature can add overhead.  It's less than one percent though.  The algorithm
>chosen is so much more important than the language used that the language choice
>truly pales in comparison.  There are some tiny gains for assembly also.  For
>instance, you cannot do a rotate right with carry or rotate left with carry in a
>single cpu cycle in C or C++ like you can in assembly.  On the other hand,
>optimizing compilers can do some pretty clever stuff that assembly language
>programmers would be hard-put to find.
>Typically, 90% of the time spent in a program happens in 5 or 10% of the code.
>So if you optimize the hot spots, that is where the benefits are to be reaped.
>But I agree with Amir that little will be gained by rehashing the choice of
>programming language.  There are very successful assembly, C and C++ programs
>that I am aware of.  If one had a large advantage, they would all be written in
>that language.

I think the people that invested too much in assembler for x86 took a hit with
the Pentium, and a further hit with the Pentium-Pro, because the issues of what
makes code efficient changed considerably. The easiest way to adapt was to
change to a newer version of a compiler, but you had to be using C or C++ to do


This page took 0.03 seconds to execute

Last modified: Thu, 07 Jul 11 08:48:38 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.