discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: objective-c: how slow ?


From: Erik M. Buck
Subject: Re: objective-c: how slow ?
Date: Fri, 31 Aug 2001 15:00:48 -0500

Objective-C message dispatch is 2-3 times slower than a C function call.
Since not that much time is spent in function calls themselves as opposed to
the bodies of the functions, it seldom has a huge impact.  For message
sending within a loop, the message can be hand optimized into a simple
function call and have exactly the same performance as C.

NeXT used Objective-C for writing kernel level drivers.  There is no real
problem using Objective-C in a performance sensitive embedded system.
However, it would be very difficult if not impossible to prove the
determinism of the Objective-C message dispatch (since it uses hash tables),
so if you need determinism (i.e guaranteed execution time measured in CPU
clock ticks) then you don't want Objective-C.

I personally have never seen Java perform even half as fast as Objective-C.
Every Java program I have used on the desktop suffered from slow buggy
graphics and was prone to occasional halts/freezes presumably while garbage
was collected.  Every Java implementation that I have seen as suffered from
a large memory footprint for the VM and then problems resulting from the
heap size not being large enough (Java heap sizes are limited for security
so that a Java program can not crash a machine by allocating too much
memory: (This is a job of the operating system and not a language, but what
can you do ?)

C++ virtual member functions are a tiny bit faster than Objective-C
messages.  If you use a pointer to a virtual member function (as is common
with call-backs) then the performance is identical in most cases.  Using the
pointer forces the compiler to do an additional pointer dereference similar
to what Objective-C does.  I have had performance problems with C++ not
because of virtual methods but because you have to write more code which
takes longer to execute and is not as likely to be in a cache etc.  When
profiling C++, it is not uncommon to see 40% or more time spent in
destructors and constructors.  This happens because many programmers use
simple assignment which calls copy constructors and function argument
passing by value which causes a new object to be constructed in the stack
and destructed upon return.  Here is a common slow pattern in C++;

/***** Begin example *****/

// swap objects in a collection
MyClass objectA = collection[i];
MyClass objectB = collection[i+1];
collection[i] = objectB;
collection[i+1] = objectA;

/***** End example *****/


Given that the [] operators have probably been overridden, collection[i]
turns into collection.GetObject(i) and collection[i] = objectB turns into
collection.SetObject(i, objectB).

So, how many times is the constructor and destructor for MyClass called per
line?

MyClass objectA = collection[i];  // 3 times: create objectA with default
constructor.
                                                       // copy object from
collection onto stack for return
                                                       // use copy
constructor for assignment
                                                       // destructor: 2
times original value is lost due to assignment
                                                       // stack value gives
away after assignment
MyClass objectB = collection[i+1];  // 3 times: same as above
                                                      // destructor 2 times
collection[i] = objectB;                // at least 2 times: once onto stack
for function call and agian for
                                                      // assignment
                                                     // destructor at least
once
collection[i+1] = objectA;            // same as above

Total: at least 10 constructor calls
          at least 8 destructor calls

Each constructor or constructor call has the minimum overhead of a function
call
Constructors and destructors foo all of MyClass's super classes are also
called.
It is easy to get 20 or 30 function calls out of the code above.


Now, before C++ programmers say the example is not fair because references
should be used rather than pass by value.  First, the code above looks
perfectly reasonable and harmless to an non-C++ high priest.  The same code
might be written in C or Java (except there people would know what the []
and = operators do for sure).
An expert C++ programmer might write MyClass objectA(collection[i]); to
avoid the useless default constructor call.  Second, tell that to the
creators of the CString class in MFC that requires pass by value semantics.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]