[comp.sys.next] speed of access methods vs. object_getInstanceVariable

simsong@daily-bugle.media.mit.edu (Simson L. Garfinkel) (01/08/91)

I was interested as to whether it is faster to access the instance variables of an object via accessor methods or via the object_getInstanceVariable() function, so I did a test.

Here's the program:

/*
 * Time test; see if it is faster to read instance variables with
 * accessor methods or with object_getInstanceVariable...
 */

#include <objc/Object.h>

@interface MyObject:Object
{
        int     x;
}

-setX:(int)x;
-(int)getX;
@end

@implementation MyObject
-setX:(int)aX
{
        x       = aX;
        return self;
}

-(int)getX;
{
        return(x);
}

@end

int     main(int argc,char **argv)
{
        int     method;
        int     count;
        id      anX;

        if(!strcmp(argv[1],"-m")){
                method  = 1;
                argv++;
                argc--;
        }

        count   = atoi(argv[1]);

        anX     = [[MyObject alloc] init];
        [anX setX:3];

        if(method){
                while(count-->0){
                        [anX getX];
                }
        }
        else{
                while(count-->0){
                        int     x;

                        object_getInstanceVariable(anX,"x",(void **)&x);
                }
        }
        return(0);
}

Here are the results of the time testing:

daily-bugle.media.mit.edu> time ti -m 100000
1.9u 0.2s 0:02 98% 0+0k 0+0io 0pf+0w
daily-bugle.media.mit.edu> time ti 100000   
5.7u 0.2s 0:06 97% 0+0k 0+0io 0pf+0w
daily-bugle.media.mit.edu> 

Two things of interest:
1. Accessing via the method is about 3x faster than accessing via object_getInstanceVariable.
2. I can get about 20,000 accesses/second (on a 60830 NeXT).  That seems fairly good.


Anybody else have any experience with this sort of thing?

			----Simson

lerman@stpstn.UUCP (Ken Lerman) (01/09/91)

In article <4755@media-lab.MEDIA.MIT.EDU> simsong@daily-bugle.media.mit.edu (Simson L. Garfinkel) writes:
|I was interested as to whether it is faster to access the instance variables of an object via accessor methods or via the object_getInstanceVariable() function, so I did a test.
|
|Here's the program:
[...program deleted...]
|Here are the results of the time testing:
|
|daily-bugle.media.mit.edu> time ti -m 100000
|1.9u 0.2s 0:02 98% 0+0k 0+0io 0pf+0w
|daily-bugle.media.mit.edu> time ti 100000   
|5.7u 0.2s 0:06 97% 0+0k 0+0io 0pf+0w
|daily-bugle.media.mit.edu> 
|
|Two things of interest:
|1. Accessing via the method is about 3x faster than accessing via object_getInstanceVariable.
|2. I can get about 20,000 accesses/second (on a 60830 NeXT).  That seems fairly good.
|
|
|Anybody else have any experience with this sort of thing?
|
|			----Simson

I ran the same test with Stepstone's Objective-C compiler and runtime
on a Sun3 (an mc68020 processor according to the mach command).

Since Stepstone doesn't support access of instance variables via the
name as a string, I removed that code and therefore can only run with
the -m option.

The results are:
	2.5u 0.0s 0:02 99% 8+24k 0+1io 0pf+0w

That seems to be too fast compared to the Next.  Does the Next have
inordinate startup time?  I did a million loop test and got:
	25.2u 0.1s 0:25 99% 8+24k 1+2io 0pf+0w

Shouldn't the Next be much faster than the Sun 3.  Am I missing something.

Ken