4 ms·
CFAbsoluteTimeGetCurrent is a long deprecated API, so I'm not sure where that's coming from. A current and more readable way of expressing this would be let
by deminature 7y ago
CFAbsoluteTimeGetCurrent is a long deprecated API, so I'm not sure where that's coming from.
A current and more readable way of expressing this would be
let start = Date().timeIntervalSinceReferenceDate
If you don't need exact seconds right away, you can simplify further to just:
let start = Date()
which is easily as simple as the Python example.
- darknoon 7y agoThis is not the same thing. CACurrentMediaTime() / CFAbsoluteTimeGetCurrent() are first of all not deprecated (just check CFDate.h / CABase.h) but return a time interval since system boot so they are guaranteed to be increasing. It's just a fp64 representation of mach_absolute_time() without needing to worry about the time base vs seconds. Date() / NSDate returns a wall clock time, which is less accurate and not guaranteed to increase uniformly (ie adjusting to time server, user changes time etc)
- Someone 7y ago”CACurrentMediaTime() / CFAbsoluteTimeGetCurrent() […] are guaranteed to be increasing.” That is true for CACurrentMediaTime, but that time stops when the system sleeps (https://developer.apple.com/documentation/quartzcore/1395996-cacurrentmediatime https://developer.apple.com/documentation/quartzcore/1395996... says it calls mach_absolute_time, and https://developer.apple.co/documentation/driverkit/3438076-mach_absolute_time https://developer.apple.co/documentation/driverkit/3438076-m... says ” Returns current value of a clock that increments monotonically in tick units (starting at an arbitrary point), this clock does not increment while the system is asleep.”) Also (https://developer.apple.com/documentation/corefoundation/1543542-cfabsolutetimegetcurrent https://developer.apple.com/documentation/corefoundation/154...): Repeated calls to this function do not guarantee monotonically increasing results. The system time may decrease due to synchronization with external time references or due to an explicit user change of the clock.
- deminature 7y agoOops, you're right on the deprecation point. CFAbsoluteTimeGetCurrent is not itself deprecated but every method associated with it is [1]. Also CFAbsoluteTimeGetCurrent explicitly calls out that it isn't guaranteed to only increase. CACurrentMediaTime is monotonic though. CFAbsoluteTimeGetCurrent also returns seconds since 2001 and is not monotonic, so there's really no reason to use it instead of Date().timeIntervalSinceReferenceDate. The most idiomatic equivalent to the Python time method is definitely some usage of Date(), as time in Python doesn't have monotonic guarantees either. [1] https://developer.apple.com/documentation/corefoundation/1543542-cfabsolutetimegetcurrent https://developer.apple.com/documentation/corefoundation/154...
- saagarjha 7y ago> CFAbsoluteTimeGetCurrent is not itself deprecated but every method associated with it is. Because they deal with calendar-related stuff that is better accessed through Date.
- mpweiher 7y agoCFAbsoluteTimeGetCurrent() returns wall clock time, as far as I can tell the exact same thing as -[NSDate timeIntervalSinceReferenceDate]. https://developer.apple.com/documentation/corefoundation/1543542-cfabsolutetimegetcurrent?language=objc https://developer.apple.com/documentation/corefoundation/154... https://developer.apple.com/documentation/foundation/nsdate/1417376-timeintervalsincereferencedate?language=objc https://developer.apple.com/documentation/foundation/nsdate/...
- novok 7y agoPython's time() call is returning the unix epoch wall clock time. Newbies (and most engineers TBH) are not going to know the subtleties and reasons why you'd use a monotonic clock or to even think of using one or another. So for this comparison, it is better to use Date().
- bilboa 7y agoPython's time.time() call is also going to be affected by system time changes and thus not guaranteed to increase uniformly. So Date() in Swift and time.time() in Python are the same in that regard.
- nomel 7y agoCorrect, the appropriate function is time.monotonic().
- NobodyNada 7y ago"long deprecated" as in 20 years long; the CF APIs exist mostly for compatibility with Mac OS 9. The only time you really would need to use those functions nowdays is for interfacing with a few system services on Apple platforms like low-level graphics APIs and whatnot.
- saagarjha 7y agoCoreFoundation is in no way deprecated.
- NobodyNada 7y agoYou're right; I quoted the parent comment even though "deprecated" was not an accurate word choice here, sorry. CF is not deprecated because it is needed on occasion when programming for Apple platforms, but the APIs are largely obsolete.
- deleted 7y ago[deleted]
- mpweiher 7y agoWhat's amusing is that -[NSDate timeIntervalSinceReferenceDate] is actually the older of the two, going back to NeXTStep's Foundation introduced with EOF 1994 and later made standard with OPENSTEP 4.0.