Skip to content

Instantly share code, notes, and snippets.

@Inferis
Created November 23, 2011 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Inferis/1389870 to your computer and use it in GitHub Desktop.
Save Inferis/1389870 to your computer and use it in GitHub Desktop.
//
// Speciaal voor @junkiesxl
// ######## YES nil N, 10000000 iterations
// 0 0.376909: if
// 0 0.456258: if-equivalent statement
// 0 0.312767: statement
// 0 0.368508: statement-equivalent ifs
// 1 0.309485: if
// 1 0.324720: if-equivalent statement
// 1 0.306790: statement
// 1 0.337566: statement-equivalent ifs
// 2 0.310687: if
// 2 0.343567: if-equivalent statement
// 2 0.361835: statement
// 2 0.323326: statement-equivalent ifs
// total 0.332360: if
// total 0.374848: if-equivalent statement
// total 0.327131: statement
// total 0.343133: statement-equivalent ifs
//
// ######## NO nil N, 10000000 iterations
// 0 0.259425: if
// 0 0.285579: if-equivalent statement
// 0 0.390533: statement
// 0 0.330332: statement-equivalent ifs
// 1 0.278179: if
// 1 0.313811: if-equivalent statement
// 1 0.381250: statement
// 1 0.321606: statement-equivalent ifs
// 2 0.278277: if
// 2 0.298398: if-equivalent statement
// 2 0.378585: statement
// 2 0.327610: statement-equivalent ifs
// total 0.271960: if
// total 0.299263: if-equivalent statement
// total 0.383456: statement
// total 0.326516: statement-equivalent ifs
//
// ######## YES nil Bla, 10000000 iterations
// 0 2.547005: if
// 0 2.437984: if-equivalent statement
// 0 2.504259: statement
// 0 2.575665: statement-equivalent ifs
// 1 2.445735: if
// 1 2.458748: if-equivalent statement
// 1 2.388922: statement
// 1 2.458811: statement-equivalent ifs
// 2 2.472405: if
// 2 2.446070: if-equivalent statement
// 2 2.440226: statement
// 2 2.491306: statement-equivalent ifs
// total 2.488382: if
// total 2.447601: if-equivalent statement
// total 2.444469: statement
// total 2.508594: statement-equivalent ifs
//
// ######## NO nil Bla, 10000000 iterations
// 0 0.290399: if
// 0 0.291128: if-equivalent statement
// 0 2.574967: statement
// 0 2.483595: statement-equivalent ifs
// 1 0.278779: if
// 1 0.286530: if-equivalent statement
// 1 2.472457: statement
// 1 2.483466: statement-equivalent ifs
// 2 0.264189: if
// 2 0.320351: if-equivalent statement
// 2 2.466572: statement
// 2 2.495031: statement-equivalent ifs
// total 0.277789: if
// total 0.299336: if-equivalent statement
// total 2.504665: statement
// total 2.487364: statement-equivalent ifs
//
// ######## YES iets N, 10000000 iterations
// 0 0.368698: if
// 0 0.354141: if-equivalent statement
// 0 0.305537: statement
// 0 0.348360: statement-equivalent ifs
// 1 0.306307: if
// 1 0.363797: if-equivalent statement
// 1 0.334056: statement
// 1 0.327442: statement-equivalent ifs
// 2 0.331045: if
// 2 0.348089: if-equivalent statement
// 2 0.310539: statement
// 2 0.339411: statement-equivalent ifs
// total 0.335350: if
// total 0.355342: if-equivalent statement
// total 0.316711: statement
// total 0.338404: statement-equivalent ifs
//
// ######## NO iets N, 10000000 iterations
// 0 0.313580: if
// 0 0.334387: if-equivalent statement
// 0 0.351610: statement
// 0 0.345700: statement-equivalent ifs
// 1 0.382005: if
// 1 0.349941: if-equivalent statement
// 1 0.323475: statement
// 1 0.340168: statement-equivalent ifs
// 2 0.330120: if
// 2 0.323384: if-equivalent statement
// 2 0.346801: statement
// 2 0.409126: statement-equivalent ifs
// total 0.341902: if
// total 0.335904: if-equivalent statement
// total 0.340629: statement
// total 0.364998: statement-equivalent ifs
//
// ######## YES iets Bla, 10000000 iterations
// 0 2.564558: if
// 0 2.440682: if-equivalent statement
// 0 2.429113: statement
// 0 2.515968: statement-equivalent ifs
// 1 2.459344: if
// 1 2.516665: if-equivalent statement
// 1 2.460577: statement
// 1 2.471582: statement-equivalent ifs
// 2 2.446444: if
// 2 2.498884: if-equivalent statement
// 2 2.480448: statement
// 2 2.555204: statement-equivalent ifs
// total 2.490115: if
// total 2.485410: if-equivalent statement
// total 2.456713: statement
// total 2.514251: statement-equivalent ifs
//
// ######## NO iets Bla, 10000000 iterations
// 0 3.579190: if
// 0 3.388666: if-equivalent statement
// 0 2.435902: statement
// 0 3.342518: statement-equivalent ifs
// 1 3.405341: if
// 1 3.402358: if-equivalent statement
// 1 2.460596: statement
// 1 3.379473: statement-equivalent ifs
// 2 3.404787: if
// 2 3.431247: if-equivalent statement
// 2 2.438172: statement
// 2 3.380439: statement-equivalent ifs
// total 3.463106: if
// total 3.407424: if-equivalent statement
// total 2.444890: statement
// total 3.367477: statement-equivalent ifs
#import "AppDelegate.h"
static inline BOOL IsEmpty(id thing) {
if (thing == nil) return YES;
if ([thing isEqual:[NSNull null]]) return YES;
if ([thing respondsToSelector:@selector(count)]) return [thing performSelector:@selector(count)] == 0;
if ([thing respondsToSelector:@selector(length)]) return [thing performSelector:@selector(length)] == 0;
return NO;
}
@interface Inferis : NSObject {
}
- (BOOL)multipleIfs:(BOOL)isTextValue values:(NSArray *)values flag:(NSString *)flag;
- (BOOL)equivalentIfs:(BOOL)isTextValue values:(NSArray *)values flag:(NSString *)flag;
- (BOOL)statement:(BOOL)isTextValue values:(NSArray *)values flag:(NSString *)flag;
- (BOOL)equivalentStatement:(BOOL)isTextValue values:(NSArray *)values flag:(NSString *)flag;
@end
@implementation Inferis : NSObject
- (BOOL)multipleIfs:(BOOL)isTextValue values:(NSArray *)values flag:(NSString *)flag {
if (!isTextValue && IsEmpty(values)) return YES;
if ([flag isEqualToString:@"N"]) return NO;
return YES;
}
- (BOOL)equivalentIfs:(BOOL)isTextValue values:(NSArray *)values flag:(NSString *)flag {
if ([flag isEqualToString:@"N"]) return NO;
if (!isTextValue && IsEmpty(values)) return YES;
return YES;
}
- (BOOL)statement:(BOOL)isTextValue values:(NSArray *)values flag:(NSString *)flag {
return ![flag isEqualToString:@"N"] || (!isTextValue && IsEmpty(values));
}
- (BOOL)equivalentStatement:(BOOL)isTextValue values:(NSArray *)values flag:(NSString *)flag {
return (!isTextValue && IsEmpty(values)) || ![flag isEqualToString:@"N"];
}
@end
@interface AppDelegate () {
Inferis* inf;
}
@end
@implementation AppDelegate
@synthesize window = _window;
#define ITERATIONS 10000000
#define TIMES 3
static inline NSTimeInterval TOTAL(NSTimeInterval deltas[]) {
NSTimeInterval result = 0;
for (int t=0; t<TIMES; t++) {
result += deltas[t];
}
return result/(NSTimeInterval)TIMES;
}
- (void)run:(NSString*)what with:(BOOL)isTextValue values:(NSArray *)values flag:(NSString *)flag {
NSLog(@"######## %@, %d iterations", what, ITERATIONS);
NSTimeInterval delta1[TIMES];
NSTimeInterval delta2[TIMES];
NSTimeInterval delta3[TIMES];
NSTimeInterval delta4[TIMES];
NSDate *startingDate;
for (int t=0; t<TIMES; t++) {
startingDate = [NSDate date];
for (int i = 0; i < ITERATIONS; i++) {
[inf multipleIfs:isTextValue values:values flag:flag];
}
delta1[t] = [[NSDate date] timeIntervalSinceDate:startingDate];
NSLog(@"%d %f: if", t, delta1[t]);
startingDate = [NSDate date];
for (int i = 0; i < ITERATIONS; i++) {
[inf equivalentStatement:isTextValue values:values flag:flag];
}
delta3[t] = [[NSDate date] timeIntervalSinceDate:startingDate];
NSLog(@"%d %f: if-equivalent statement", t, delta3[t]);
startingDate = [NSDate date];
for (int i = 0; i < ITERATIONS; i++) {
[inf statement:isTextValue values:values flag:flag];
}
delta2[t] = [[NSDate date] timeIntervalSinceDate:startingDate];
NSLog(@"%d %f: statement", t, delta2[t]);
startingDate = [NSDate date];
for (int i = 0; i < ITERATIONS; i++) {
[inf equivalentIfs:isTextValue values:values flag:flag];
}
delta4[t] = [[NSDate date] timeIntervalSinceDate:startingDate];
NSLog(@"%d %f: statement-equivalent ifs", t, delta4[t]);
}
NSLog(@"total %f: if", TOTAL(delta1));
NSLog(@"total %f: if-equivalent statement", TOTAL(delta3));
NSLog(@"total %f: statement", TOTAL(delta2));
NSLog(@"total %f: statement-equivalent ifs", TOTAL(delta4));
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
inf = [[Inferis alloc] init];
NSArray *iets = [NSArray arrayWithObject:@"iets"];
[self run:@"YES nil N" with:YES values:nil flag:@"N"];
[self run:@"NO nil N" with:NO values:nil flag:@"N"];
[self run:@"YES nil Bla" with:YES values:nil flag:@"Bla"];
[self run:@"NO nil Bla" with:NO values:nil flag:@"Bla"];
[self run:@"YES iets N" with:YES values:iets flag:@"N"];
[self run:@"NO iets N" with:YES values:iets flag:@"N"];
[self run:@"YES iets Bla" with:YES values:iets flag:@"Bla"];
[self run:@"NO iets Bla" with:NO values:iets flag:@"Bla"];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment