0

Refer to this question below

How to log a method's execution time exactly in milliseconds?

And I convert the code to swift like this

//OBjective-C
//#define TICK   NSDate *startTime = [NSDate date]
//#define TOCK   NSLog(@"Time: %f", -[startTime timeIntervalSinceNow])

class AppDelegate: NSObject, NSApplicationDelegate {

var TIMER = NSDate()

@IBOutlet weak var window: NSWindow!

func TICK()
{
    TIMER = NSDate()
}

func TOCK(){

    println("\(-TIMER.timeIntervalSinceNow)")
}



func applicationDidFinishLaunching(aNotification: NSNotification) {
    // Insert code here to initialize your application
    TICK()
    TOCK()
}

Timing in Swift : 3.40343e-05 while Objective-C : 0.000009 Why Swift is so slow?

Thank you.

Community
  • 1
  • 1
webmastx
  • 683
  • 1
  • 8
  • 30
  • Did you compile both Obj-C and Swift code in Release mode (with optimizations) ? – Martin R Apr 08 '15 at 09:54
  • I test that code in both applicationDidFinishLaunching in debug mode. – webmastx Apr 08 '15 at 09:56
  • What are the results in Release mode? I would also recommend to check with Xcode 6.3 beta which brings huge performance improvements with Swift 1.2. – Martin R Apr 08 '15 at 09:57
  • Edit Scheme then changed from debug to release and still get the same result! I use Xcode 6.2 and wondered why it is so much slower. When I get 6.3 I will come back to this result. Thanks – webmastx Apr 08 '15 at 10:09
  • 2
    3.40343e-05 = 0.000034 and when I did the same thing I got as little as 2.8*10^-6. 0.000002 < 0.000009, so my conclusion is, Swift is FASTER. – Arbitur Apr 08 '15 at 10:20
  • let totaltime = -(Float)(TIMER.timeIntervalSinceNow) println((String(format: "Time : %.10f",totaltime))) – webmastx Apr 08 '15 at 10:53

1 Answers1

0

If you'd like to use anywhere in your code and avoid global variables, you can do same approach with static functions inside a utility class like so (I call my class Debug):

class Debug {

private static var tickTimestamp: Date = Date()

static func tick() {
    print("TICK.")
    tickTimestamp = Date()
}

static func tock() {
    print("TOCK. Elapsed Time: \(Date().timeIntervalSince(tickTimestamp))")
}

}

Then use like:

Debug.tick()
Debug.tock()
RGood
  • 111
  • 1
  • 6