Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 NILS_ARE_ALIVE 0
int main (void) {
@autoreleasepool {
// 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);
NSString *string = @"I seem to be a verb.";
NSNumber *number = [NSNumber numberWithInt: 42];
[number isEqualToString: string];
[string isEqualToString: number];
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]];
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);
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);
return 0;
} // main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment