Skip to content

Instantly share code, notes, and snippets.

@donpark
Created August 22, 2012 00:08
Show Gist options
  • Save donpark/3420684 to your computer and use it in GitHub Desktop.
Save donpark/3420684 to your computer and use it in GitHub Desktop.
Simple high-resolution timing wrapper module in CoffeeScript
if process.hrtime
console.log "using process.hrtime for timing"
clock = process.hrtime
else
console.log "using microtime for timing"
Microtime = require('microtime')
clock = (s) ->
t = Microtime.nowStruct()
t[1] *= 1000 # microseconds to nanoseconds
if s
t[0] -= s[0]
t[1] -= s[1]
if t[1] < 0
t[0] -= 1
t[1] += 1000000000
t
namedTimers = {}
exports.time = (name) ->
delete namedTimers[name]
namedTimers[name] = clock()
exports.timeEnd = (name, logger = console.log) ->
t = clock(namedTimers[name])
delete namedTimers[name]
if t[0]
logger "#{name} #{t[0]} seconds #{t[1]} nanoseconds"
else
logger "#{name} #{t[1]} nanoseconds"
t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment