Re: Objective-C question
Re: Objective-C question
- Subject: Re: Objective-C question
- From: Nicko van Someren <email@hidden>
- Date: Wed, 5 May 2004 17:11:24 +0100
On 5 May 2004, at 15:30, Ondra Cada wrote:
On 5.5.2004, at 16:19, Scott Thompson wrote:
But playing the Devil's advocate, it is also the case, however, that
Encapsulation is "just" a design tool that can be applied to the
class.
My opinion is that if the ivars are to be public by design, then you
actually want a struct, not a class.
I presume you can rig up an example when a struct would be impractical
and yet you need public ivars, but IMHO it would make 0.01 per cent of
0.01 per cent, so in practice we can skip that ;)
Classes give you much more than just encapsulation. They give you all
sorts of useful memory management features, convenient inheritance of
methods and a way to make your code more readable. There are many good
reasons for using these features and the other benefits of classes even
if you care nothing for encapsulation.
On the other hand it is worth noting that Objective-C classes are not
without cost. Consider the code for blah.m attached to the end of this
mail. It makes an object and then uses @defs() to make a C structure
with the same layout. Here's the output:
Direct took 0.68s, methods took 9.10s for 100000000 loops
Reading and writing the instance variable many times using direct
access records 6.8ns per loop on a 1.25GHz G4 whereas using methods to
get and set the same instance variable takes 91ns per loop, thirteen
times slower. There is clearly a large cost associated with making the
method calls themselves, and worse the disruption of processor flow by
having to make calls to objc_msgSend all the time means that the
compiler's optimisation phase is much less effective; in this test I
deliberately forced the compiler not to optimise around these calls and
removing the 'volatile' directive drops the direct access loop to
1.8ns, 50 time faster than method access. In many cases where dealing
with large bodies of data the overhead of using method access to
instance variables is unacceptable.
Nicko
// blah.m
// Test compiled with: gcc -O3 -o blah -framework Foundation blah.m
#import <Foundation/Foundation.h>
#include <time.h>
@interface MyClass : NSObject { @public int x; }
- (int) getX;
- (void) setX: (int) anInt;
@end
@implementation MyClass
- (int) getX {return x;}
- (void) setX: (int) anInt { x = anInt; }
@end
typedef struct { @defs(MyClass); } ClassStr;
int main(int argc, char **argv) {
int i, count=100000000;
clock_t t1, t2, t3;
MyClass *obj = [[MyClass alloc] init];
volatile ClassStr *cs = (ClassStr *) obj; // The 'volatile' modifier
is to stop the compiler optimising out the reads
t1 = clock();
for(i=0; i<count; i++) { cs->x = i; i = cs->x; }
t2 = clock();
for(i=0; i<count; i++) { [obj setX: i]; i = [obj getX]; }
t3 = clock();
printf("Direct took %.2fs, methods took %.2fs for %d loops\n",
(t2 - t1)/((double) CLOCKS_PER_SEC),
(t3 - t2)/((double) CLOCKS_PER_SEC), count);
return 0;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.