Re: How is @selector resolved at compile time ?
Re: How is @selector resolved at compile time ?
- Subject: Re: How is @selector resolved at compile time ?
- From: "Erik M. Buck" <email@hidden>
- Date: Wed, 25 Jul 2001 22:17:25 -0500
I benchmarked message send vs. calling an IMP function with @selector vs.
calling an IMP function without @selector
Here are the times. They are consistent over many runs.
>
Running 'TestMsgSend.exe'...
0.771109 0.310446 0.310446
>
Finished running 'TestMsgSend.exe'.
The first number is for [@"" class]
it is 2 times the cost of the IMP function call.
Using @selector has or not NO impact on performance. How is that possible ?
Here is the code:
#import <Foundation/Foundation.h>
int main (int argc, const char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSTimeInterval time4;
NSTimeInterval time5;
NSTimeInterval time3;
NSTimeInterval time1;
NSTimeInterval time2;
NSTimeInterval time0 = [NSDate timeIntervalSinceReferenceDate];
{
int i;
for(i = 0; i < 10000000; i++) {
[@"" class];
}
time1 = [NSDate timeIntervalSinceReferenceDate];
}
{
IMP function = [@"" methodForSelector:@selector(class)];
int i;
time2 = [NSDate timeIntervalSinceReferenceDate];
for(i = 0; i < 10000000; i++) {
(*function)(@"", @selector(class));
}
time3 = [NSDate timeIntervalSinceReferenceDate];
}
{
SEL selector = @selector(class);
IMP function = [@"" methodForSelector:selector];
int i;
time4 = [NSDate timeIntervalSinceReferenceDate];
for(i = 0; i < 10000000; i++) {
(*function)(@"", selector);
}
time5 = [NSDate timeIntervalSinceReferenceDate];
}
printf("%f %f %f\n", time1 - time0, time3 - time2, time5 - time4);
[pool release];
exit(0); // insure the process exit status is 0
return 0; // ...and make main fit the ANSI spec.
}