Skip to content

Instantly share code, notes, and snippets.

@markd2
Created March 26, 2012 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markd2/2201940 to your computer and use it in GitHub Desktop.
Save markd2/2201940 to your computer and use it in GitHub Desktop.
Random comparisons of isEqual:, isEqualToString:, and compare:
#import <Foundation/Foundation.h>
#import <mach/mach_time.h> // for mach_absolute_time() and friends
// clang -g -Wall -framework Foundation -o equaltime equaltime.m
// clang -g -arch i386 -Wall -framework Foundation -o equaltime equaltime.m
CGFloat BNRTimeBlock (void (^block)(void)) {
mach_timebase_info_data_t info;
if (mach_timebase_info(&info) != KERN_SUCCESS) return -1.0;
uint64_t start = mach_absolute_time ();
block ();
uint64_t end = mach_absolute_time ();
uint64_t elapsed = end - start;
uint64_t nanos = elapsed * info.numer / info.denom;
return (CGFloat)nanos / NSEC_PER_SEC;
} // BNRTimeBlock
#define LOOPAGE 10000000
void CompareStrings (NSString *thing1, NSString *thing2) {
CGFloat equalTime = BNRTimeBlock (^{
for (NSInteger i = 0; i < LOOPAGE; i++) {
(void) [thing1 isEqual: thing2];
}
});
CGFloat equalToStringTime = BNRTimeBlock (^{
for (NSInteger i = 0; i < LOOPAGE; i++) {
(void) [thing1 isEqualToString: thing2];
}
});
CGFloat compareTime = BNRTimeBlock (^{
for (NSInteger i = 0; i < LOOPAGE; i++) {
(void) [thing1 compare: thing2];
}
});
NSLog (@"Time for isEqual: %f", equalTime);
NSLog (@"Time for isEqualToString: %f", equalToStringTime);
NSLog (@"Time for compare: %f", compareTime);
} // CompareStrings
// Replace the zero with a one for the chunk fo test code you want to run
#define RUN_TIMINGS 0
#define STATIC_ERROR_CHECKING 0
#define RUNTIME_TYPE_CHECKING 0
#define LITERAL_COMPARISON 0
#define NILS_ARE_ALIVE 0
int main (void) {
@autoreleasepool {
#if RUN_TIMINGS
// Different strings, so literal NSString
NSString *thing1 = @"Hello there, how are you doing today?";
NSString *thing2 = @"Hello there, how are you doing today!";
CompareStrings (thing1, thing2);
#endif
#if STATIC_TYPE_CHECKING
NSString *string = @"I seem to be a verb.";
NSNumber *number = [NSNumber numberWithInt: 42];
[number isEqualToString: string];
[string isEqualToString: number];
#endif
#if RUNTIME_TYPE_CHECKING
NSString *string = @"i can haz cheezburger?";
NSArray *stringArray = [NSArray arrayWithObject: string];
NSNumber *number = [NSNumber numberWithInt: 42];
NSArray *numberArray = [NSArray arrayWithObject: number];
[[stringArray lastObject] isEqualToString: [numberArray lastObject]];
// [[numberArray lastObject] isEqualToString: [stringArray lastObject]];
#endif
#if LITERAL_COMPARISON
NSString *oUmlaut = @"\u00f6"; // o-umlaut
NSString *oPlusUmlaut = @"o\u0308"; // o + combining diaeresis
NSLog (@"%@ and %@: equal %d vs compare: %d",
oUmlaut, oPlusUmlaut,
[oUmlaut isEqualToString: oPlusUmlaut],
[oUmlaut compare: oPlusUmlaut] == NSOrderedSame);
#endif
#if NILS_ARE_ALIVE
NSString *nil1 = nil;
NSString *nil2 = nil;
NSLog (@"nil and nil: equal %d vs compare: %d",
[nil1 isEqualToString: nil2],
[nil1 compare: nil2] == NSOrderedSame);
NSString *notNil = @"greeble bork";
NSLog (@"nil and non-nil: equal %d vs compare: %d",
[nil1 isEqualToString: notNil],
[nil1 compare: notNil] == NSOrderedSame);
NSLog (@"non-nil and nil: equal %d vs compare: %d",
[notNil isEqualToString: nil1],
[notNil compare: nil1] == NSOrderedSame);
#endif
}
return 0;
} // main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment