2

I'm playing with Swift for the first time and have some decent programs running under Ubuntu and on a Raspberry Pi but I cannot find a way to measure elapsed time. My first thought is to read the unix time but I cannot see it in any API and haven't figured out the FFI yet.

How should I measure elapsed time in Swift code on Ubuntu and Raspberry Pi?

7
  • 2
    What's wrong with Date? Commented Jan 2, 2018 at 22:04
  • 1
    If you need accurate elapsed time, consider mach_absolute_time as outlined in Technical Q&A 1398. Commented Jan 2, 2018 at 22:20
  • Is NSDate an option? It is mentioned in this answer: Measure elapsed time in swift Commented Jan 2, 2018 at 22:40
  • My answer was converted into a comment so it can't be selected as an answer. That is not right. Commented Jan 2, 2018 at 22:41
  • @absmiths - Effective with Swift 3, we don’t use NSDate anymore. We use Date nowadays. Commented Jan 2, 2018 at 22:42

1 Answer 1

1

C is interoperable with Swift. If you don't want to use Foundation's Date as rmaddy suggested,* you should be able to call the C stdlib's time() function. Just import GlibC first.


*The Unix time from a Date is just Date.timeIntervalSince1970.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.