I don't want to set up a timer that "fires" (and does something) after a certain amount of time has passed.
Therefore, I'm not interested in the NSTimer
class and its methods.
All I'm interested in is counting the time that passes by while , for example, some while-loop is executing. I could use NSDate
as follows I guess :
NSDate currentDate = [[NSDate alloc] init];
while(someConditionIsTrue)
{
// executing..
}
timeElapsed = [self timeIntervalSinceDate:currentDate];
NSLog(@"time elapsed was: %i", timeElapsed);
However, if I understand correctly, timeIntervalSinceDate:
can only count seconds.
Is there a way I can count the time that is passing by in milliseconds ?
In other words, what is the smallest unit I can count passing time in and how ?
Look at CFAbsoluteTimeGetCurrent()
CFAbsoluteTime before = CFAbsoluteTimeGetCurrent();
CFAbsoluteTime after = CFAbsoluteTimeGetCurrent();
Your second approach is correct. Save the current date in an NSDate
object and use timeIntervalSinceDate:
to get the passed time since then. The result will be of type NSTimeInterval
which is a floating point number. It specifies time differences in seconds, but since it's a floating point number it can store fractions of a second as well.
NSTimeInterval is always specified in seconds; it yields sub-millisecond precision over a range of 10,000 years.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.