最佳方式获取当前系统时间(毫秒)是什么?
如果你想用这个来进行相对时间计算(例如用于游戏或动画),我更愿意使用CACurrentMediaTime()
double CurrentTime = CACurrentMediaTime();
什么是推荐方式? NSDate
从联网的同步时钟获取时间,并在重新与网络同步时偶尔会出现问题。它返回以秒为单位的当前绝对时间。let ct = CACurrentMediaTime().truncatingRemainder(dividingBy: 1)
[[NSDate date] timeIntervalSince1970];
它返回自纪元以来的秒数,类型为双精度浮点数。我几乎确定您可以从小数部分中访问毫秒。
[NSDate timeIntervalSinceReferenceDate]
更便宜。虽然它引用了不同的 "纪元" -- 如果您想要 1970 年,请添加常量 NSTimeIntervalSince1970
。 - Hot LickstimeIntervalSince1970
返回的TimeInterval实际上是一个带有“亚毫秒精度”的Double。 - Incinerator我在iPhone 4S和iPad 3上对所有其他答案进行了基准测试(发布版本)。CACurrentMediaTime
的开销略低于其他方法。 timeIntervalSince1970
比其他方法慢得多,可能是由于NSDate
实例化开销导致的,尽管对许多用例来说这可能并不重要。
如果您希望最小化开销并且不介意添加Quartz Framework依赖,则建议使用CACurrentMediaTime
。如果可移植性是您的重点,则使用gettimeofday
。
iPhone 4S
CACurrentMediaTime: 1.33 µs/call
gettimeofday: 1.38 µs/call
[NSDate timeIntervalSinceReferenceDate]: 1.45 µs/call
CFAbsoluteTimeGetCurrent: 1.48 µs/call
[[NSDate date] timeIntervalSince1970]: 4.93 µs/call
iPad 3
CACurrentMediaTime: 1.25 µs/call
gettimeofday: 1.33 µs/call
CFAbsoluteTimeGetCurrent: 1.34 µs/call
[NSDate timeIntervalSinceReferenceDate]: 1.37 µs/call
[[NSDate date] timeIntervalSince1970]: 3.47 µs/call
NSTimeIntervalSince1970
的宏,它是最快的。 - Ozgur VatanseverNSTimeIntervalSince1970
是一个常量,描述了从1970年01月01日到“参考日期”(即2001年01月01日)之间的秒数,因此它始终等于978307200。 - YourMJK在Swift中,我们可以创建一个函数并按照以下方式执行:
func getCurrentMillis()->Int64{
return Int64(NSDate().timeIntervalSince1970 * 1000)
}
var currentTime = getCurrentMillis()
尽管在 Swift 3.0 中它工作得很好,但我们可以改用Date
类而不是NSDate
类在3.0中使用。
Swift 3.0
func getCurrentMillis()->Int64 {
return Int64(Date().timeIntervalSince1970 * 1000)
}
var currentTime = getCurrentMillis()
func currentTimeInMilliSeconds()-> Int
{
let currentDate = Date()
let since1970 = currentDate.timeIntervalSince1970
return Int(since1970 * 1000)
}
到目前为止,我发现在 iOS (iPad) 上使用 gettimeofday
是一个不错的解决方案,当你想要执行一些间隔性评估(比如帧率、渲染帧的时间...)时:
#include <sys/time.h>
struct timeval time;
gettimeofday(&time, NULL);
long millis = (time.tv_sec * 1000) + (time.tv_usec / 1000);
Swift 2
let seconds = NSDate().timeIntervalSince1970
let milliseconds = seconds * 1000.0
Swift 3
let currentTimeInMiliseconds = Date().timeIntervalSince1970.milliseconds
TimeInterval
(也称为Double
)没有毫秒属性。Date().timeIntervalSince1970 * 1000
表示将当前时间与1970年1月1日的时间差转换为毫秒数。 - Leo Dabus了解CodeTimestamps可能会很有用,它提供了对基于mach的计时函数的封装。这使您可以获得纳秒级分辨率的计时数据-比毫秒高出1000000倍。是的,精度高出一百万倍。(前缀为毫、微、纳,每个前缀比上一个前缀高出1000倍)即使您不需要CodeTimestamps,也可以查看代码(它是开源的),以了解他们如何使用mach获取计时数据。当您需要更高的精度并且希望方法调用速度更快时,这将非常有用,而不是使用NSDate方法。
http://eng.pulse.me/line-by-line-speed-analysis-for-ios-apps/
-fno-objc-arc
:) - Dan Rosenstark// Timestamp after converting to milliseconds.
NSString * timeInMS = [NSString stringWithFormat:@"%lld", [@(floor([date timeIntervalSince1970] * 1000)) longLongValue]];
我需要一个包含[[NSDate date] timeIntervalSince1970]
准确结果的NSNumber
对象。由于该函数被多次调用,而且我并不需要创建NSDate
对象,因此性能不是很好。
如果要得到原始函数给我的格式,请尝试这样做:
#include <sys/time.h>
struct timeval tv;
gettimeofday(&tv,NULL);
double perciseTimeStamp = tv.tv_sec + tv.tv_usec * 0.000001;
这将给您完全相同的结果,就像[[NSDate date] timeIntervalSince1970]
一样