현재 시스템 시간 밀리초입니다 얻는 가장 좋은 방법은 무엇입니까?
double CurrentTime = CACurrentMediaTime();
이는 권장됨 운행에서어떠한. '무' 에서 산드라테 네트워킹된 synch 시계 것으로, 이 때 가끔 딸꾹질 re synching 아니했으니 네트워크.
현재 절대 되돌려줍니다 이 시간 (초).
스케쳐내 소수점 이하 전용) , 부품 (자주 사용되는 동기화할 때 애니메이션)
let ct = CACurrentMediaTime().truncatingRemainder(dividingBy: 1)
내가 다른 모든 답을 벤치 마크 협력하였습니다 아이폰 4S 와 아이패드 3 (릴리스에는 빌드). '여백' 에 의해 가장 작은 카스해류메디아티메 오버헤드에. '훨씬' timeIntervalSince1970 다른 인스턴스 오버헤드에 산드라테 인해 ',' 보다 느린 것으로 보이지만 대부분의 활용 사례를 중요하지 않다.
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
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
우리는 다음과 같이 나타낼 수 있는 기능 및 movd 스위프트
func getCurrentMillis()->Int64{
return Int64(NSDate().timeIntervalSince1970 * 1000)
}
var currentTime = getCurrentMillis()
수정 및 사용 할 수 없다 '의 조속한 3.0과 해당 apc® 표시되어도 벌금 대신 날짜' 클래스 '에서' 산드라테 3.0*
스위프트 3.0*과
func getCurrentMillis()->Int64 {
return Int64(Date().timeIntervalSince1970 * 1000)
}
var currentTime = getCurrentMillis()
현재 afaq 밀리초입니다 바뀌었다.
func currentTimeInMilliSeconds()-> Int
{
let currentDate = Date()
let since1970 = currentDate.timeIntervalSince1970
return Int(since1970 * 1000)
}
주위에서 래퍼 제공하십시오 고데타임스탬프스 대해 알고 있는 것이 좋을 수 있습니다 세제곱마일 기반 타이밍 총괄하였습니다. 이 시기 보다 더 정확한 데이터를 얻을 수 있습니다 - 1000000x 나노 초 해결안에서는 밀리초입니다. 예, 백만 배 더 정확합니다. (이 접두사에는 더 이상 밀리, 마이크로, 나노기술 (nt) · 각 1000x 정확히까지 마지막.) # 39, t need 고데타임스탬프스 don& 경우에도, 체크아웃합니다 코드 (it& # 39 의 오픈 소스) 를 얻기 위해 타이밍 데이터를 어떻게 사용하는지 세제곱마일. 이 보다 더 자세히 알고 있으며 빠른 메서드 호출 할 때 유용하게 사용할 수 있는 산드라테 외곽진입.
http://eng.pulse.me/line-by-line-speed-analysis-for-ios-apps/
내가 필요한 네스 움베르 정확히 포함된 'a' 결과 '객체에는 [[산드라테 날짜] timeIntervalSince1970]'. # 39, 이후 여러 차례 이 함수를 호출한 쉐퍼드도 didn& 만들어야 한다 '' t really 산드라테 객체에는, 성능 그리 좋지 않았다.
그래서 이 형식을 가져올 수 있는 날, 원래 취지대로 시도하시겠습니까 주고 있었어요.
#include <sys/time.h>
struct timeval tv;
gettimeofday(&tv,NULL);
double perciseTimeStamp = tv.tv_sec + tv.tv_usec * 0.000001;
정확한 제공하십시오 하는 것과 같은 결과가 ' [[산드라테 날짜] timeIntervalSince1970]'
이거 드세요.
NSDate * timestamp = [NSDate dateWithTimeIntervalSince1970:[[NSDate date] timeIntervalSince1970]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss.SSS"];
NSString *newDateString = [dateFormatter stringFromDate:timestamp];
timestamp = (NSDate*)newDateString;
이 예에서, 년-월-일 HH:mm:ss.SSS" 포맷터 @" dateWithTimeIntervalSince1970 함께 사용된다. 함께 있는 시간을 함께 반환되므로 날짜 년, 월, 일, 시, 분, 초, 밀리초입니다. 예 참조. 02-12-2015 04:43:15.008" ";). 나는 이전에 기록된 형식은 산슬링 을 사용한 있는지 확인할 수 있다.
> 절대 시간, 날짜 1일 1월 2001년 00:00:00 (그리니치 표준시 기준으로 상대경로를 은 초 단위로 참조. 이후 참조입니다 제이보드 데이트였어 양수 값을 날짜, 음수 값을 제이보드 날짜 전에. 예를 들어, 16일 12월 1999년 17 54 34 emc. 절대 시간 - 32940326 같습니다. 이 같은 결과를 단조 증가 함수 호출을 보장되지 않습니다. 시스템 시간을 줄일 수 있는 사용자 또는 변경 등으로 인해 외부 시간 참조에서는 통기화를 충족되었으며 명시성 편리합니다. [1]: https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFTimeUtils/ # //apple_ref/c/func/CFAbsoluteTimeGetCurrent
이것은 기본적으로 같은 대답을 올린 것으로, 그냥 코딩 @TristanLorach 대한 조속한 3:
/// Method to get Unix-style time (Java variant), i.e., time since 1970 in milliseconds. This
/// copied from here: http://stackoverflow.com/a/24655601/253938 and here:
/// http://stackoverflow.com/a/7885923/253938
/// (This should give good performance according to this:
/// http://stackoverflow.com/a/12020300/253938 )
///
/// Note that it is possible that multiple calls to this method and computing the difference may
/// occasionally give problematic results, like an apparently negative interval or a major jump
/// forward in time. This is because system time occasionally gets updated due to synchronization
/// with a time source on the network (maybe "leap second"), or user setting the clock.
public static func currentTimeMillis() -> Int64 {
var darwinTime : timeval = timeval(tv_sec: 0, tv_usec: 0)
gettimeofday(&darwinTime, nil)
return (Int64(darwinTime.tv_sec) * 1000) + Int64(darwinTime.tv_usec / 1000)
}
NSTimeInterval time = ([[NSDate date] timeIntervalSince1970]); //double
long digits = (long)time; //first 10 digits
int decimalDigits = (int)(fmod(time, 1) * 1000); //3 missing digits
/*** long ***/
long timestamp = (digits * 1000) + decimalDigits;
/*** string ***/
NSString *timestampString = [NSString stringWithFormat:@"%ld%03d",digits ,decimalDigits];