Joseph Jude

Timing code execution in Swift, Python & Typescript

2016.05.07 / code / swift / tsc / python /

How to track execution time of a code block

Time to time, you need to check the execution time of a code block. Here are the snippets for Swift, Python & Typescript.


import Foundation
var start = NSDate()

// code block to measure

var end = NSDate()
let timeInterval: Double = end.timeIntervalSinceDate(start)
print("Time to execute \(timeInterval) seconds")


import time
start_time = time.time()

# code block to measure

print("Time to execute %s seconds" % (time.time() - start_time))


var start = new Date().getTime();

// code block to measure

var end = new Date().getTime();
console.log(end - start);

Share this post on

Twitter | | |
Sign up for my weekly newsletter

I will send blog updates to this email. You can unsubscribe at any time using the link in those emails.