ios iPhone:如何获得当前的毫秒数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/358207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone: How to get current milliseconds?
提问by codelogic
What is the best way to get the current system time milliseconds?
获取当前系统时间毫秒的最佳方法是什么?
回答by Allan Simonsen
If you're looking at using this for relative timing (for example for games or animation) I'd rather use CACurrentMediaTime()
如果您正在考虑将其用于相对时间(例如用于游戏或动画),我宁愿使用CACurrentMediaTime()
double CurrentTime = CACurrentMediaTime();
Which is the recommended way; NSDate
draws from the networked synch-clock and will occasionally hiccup when re-synching it against the network.
这是推荐的方式;NSDate
从网络同步时钟中提取,并且在与网络重新同步时偶尔会打嗝。
It returns the current absolute time, in seconds.
它返回当前的绝对时间,以秒为单位。
If you want onlythe decimal part (often used when syncing animations),
如果你只想要小数部分(通常在同步动画时使用),
let ct = CACurrentMediaTime().truncatingRemainder(dividingBy: 1)
回答by codelogic
[[NSDate date] timeIntervalSince1970];
It returns the number of seconds since epoch as a double. I'm almost sure you can access the milliseconds from the fractional part.
它以双精度形式返回自纪元以来的秒数。我几乎可以肯定您可以从小数部分访问毫秒。
回答by darrinm
I benchmarked all the other answers on an iPhone 4S and iPad 3 (release builds). CACurrentMediaTime
has the least overhead by a small margin. timeIntervalSince1970
is far slower than the others, probably due to NSDate
instantiation overhead, though it may not matter for many use cases.
我在 iPhone 4S 和 iPad 3(发布版本)上对所有其他答案进行了基准测试。CACurrentMediaTime
具有最小的开销。timeIntervalSince1970
比其他的慢得多,可能是由于NSDate
实例化开销,尽管对于许多用例可能无关紧要。
I'd recommend CACurrentMediaTime
if you want the least overhead and don't mind adding the Quartz Framework dependency. Or gettimeofday
if portability is a priority for you.
CACurrentMediaTime
如果您想要最少的开销并且不介意添加 Quartz Framework 依赖项,我会建议您。或者,gettimeofday
如果便携性对您来说是一个优先事项。
iPhone 4S
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
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
回答by Rajan Maheshwari
In Swift we can make a function and do as follows
在 Swift 中,我们可以创建一个函数并执行如下操作
func getCurrentMillis()->Int64{
return Int64(NSDate().timeIntervalSince1970 * 1000)
}
var currentTime = getCurrentMillis()
Though its working fine in Swift 3.0but we can modify and use the Date
class instead of NSDate
in 3.0
虽然它的做工精细的雨燕3.0,但我们可以通过修改和使用Date
类,而不是NSDate
在3.0
Swift 3.0
斯威夫特 3.0
func getCurrentMillis()->Int64 {
return Int64(Date().timeIntervalSince1970 * 1000)
}
var currentTime = getCurrentMillis()
回答by Tristan Lorach
So far I found gettimeofday
a good solution on iOS (iPad), when you want to perform some interval evaluation (say, framerate, timing of a rendering frame...) :
到目前为止,我gettimeofday
在 iOS (iPad) 上找到了一个很好的解决方案,当您想要执行一些间隔评估(例如,帧速率、渲染帧的时间......)时:
#include <sys/time.h>
struct timeval time;
gettimeofday(&time, NULL);
long millis = (time.tv_sec * 1000) + (time.tv_usec / 1000);
回答by Rajesh Loganathan
To get milliseconds for current date.
获取当前日期的毫秒数。
Swift 4+:
斯威夫特 4+:
func currentTimeInMilliSeconds()-> Int
{
let currentDate = Date()
let since1970 = currentDate.timeIntervalSince1970
return Int(since1970 * 1000)
}
回答by quemeful
Swift 2
斯威夫特 2
let seconds = NSDate().timeIntervalSince1970
let milliseconds = seconds * 1000.0
Swift 3
斯威夫特 3
let currentTimeInMiliseconds = Date().timeIntervalSince1970.milliseconds
回答by Tyler
It may be useful to know about CodeTimestamps, which provide a wrapper around mach-based timing functions. This gives you nanosecond-resolution timing data - 1000000x more precise than milliseconds. Yes, a million times more precise. (The prefixes are milli, micro, nano, each 1000x more precise than the last.) Even if you don't need CodeTimestamps, check out the code (it's open source) to see how they use mach to get the timing data. This would be useful when you need more precision and want a faster method call than the NSDate approach.
了解 CodeTimestamps 可能很有用,它提供了基于 mach 的计时函数的包装器。这为您提供了纳秒分辨率的计时数据 - 比毫秒精确 1000000 倍。是的,精确一百万倍。(前缀是milli、micro、nano,每一个都比上一个精确1000 倍。)即使您不需要CodeTimestamps,也请查看代码(它是开源的)以了解它们如何使用mach 来获取计时数据。当您需要更高的精度并想要比 NSDate 方法更快的方法调用时,这将很有用。
http://eng.pulse.me/line-by-line-speed-analysis-for-ios-apps/
http://eng.pulse.me/line-by-line-speed-analysis-for-ios-apps/
回答by Fatima Arshad
// Timestamp after converting to milliseconds.
NSString * timeInMS = [NSString stringWithFormat:@"%lld", [@(floor([date timeIntervalSince1970] * 1000)) longLongValue]];
回答by mmackh
I needed a NSNumber
object containing the exact result of [[NSDate date] timeIntervalSince1970]
. Since this function was called many times and I didn't really need to create an NSDate
object, performance was not great.
我需要一个NSNumber
包含[[NSDate date] timeIntervalSince1970]
. 由于这个函数被调用了很多次而且我真的不需要创建一个NSDate
对象,所以性能不是很好。
So to get the format that the original function was giving me, try this:
所以要获得原始函数给我的格式,试试这个:
#include <sys/time.h>
struct timeval tv;
gettimeofday(&tv,NULL);
double perciseTimeStamp = tv.tv_sec + tv.tv_usec * 0.000001;
Which should give you the exact same result as [[NSDate date] timeIntervalSince1970]
这应该给你完全相同的结果 [[NSDate date] timeIntervalSince1970]