Hi all,
I want to set up a little program to time a loop.
This is what I have come up with...your input/criticism would be most helpful.
Time.h
Time.m
main.m (example of use)
To me the timeSince: method feels really awkward...but that's what seems to work. Thanks for your help.
I want to set up a little program to time a loop.
This is what I have come up with...your input/criticism would be most helpful.
Time.h
Code:
#import <Foundation/NSDate.h>
@interface NSDate (DateOps)
+(id)dateNow;
+(NSTimeInterval) timeSince: (NSDate*) startOfLoop;
@end
Time.m
Code:
#import "Time.h"
@implementation NSDate (DateOps)
+(id) dateNow
{
NSDate *today = [NSDate date];
return today;
}
+(NSTimeInterval) timeSince: (NSDate*) startOfLoop;
{
return -( 1000 * [ startOfLoop timeIntervalSinceNow]); /* returns milli-seconds */
}
@end
main.m (example of use)
Code:
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSAutoreleasePool *tempPool;
int x;
NSDate *startOfLoop = [ NSDate dateNow];
for ( x = 0; x < 100000; x ++)
{
tempPool = [ [ NSAutoreleasePool alloc] init];
NSString *myS = [[NSString alloc]initWithString: @"test"];
if ( x % 10 )
[myS autorelease];
[tempPool drain];
}
NSTimeInterval elapsedTime = [NSDate timeSince:startOfLoop];
NSLog(@"Elapsed Time: %g milliseconds", elapsedTime);
[pool drain];
return 0;
}
To me the timeSince: method feels really awkward...but that's what seems to work. Thanks for your help.