Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!mit-eddie!media-lab!simsong From: simsong@daily-bugle.media.mit.edu (Simson L. Garfinkel) Newsgroups: comp.sys.next Subject: speed of access methods vs. object_getInstanceVariable Message-ID: <4755@media-lab.MEDIA.MIT.EDU> Date: 7 Jan 91 21:47:16 GMT Sender: news@media-lab.MEDIA.MIT.EDU Lines: 82 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 @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