Skip to content
Browse files

Add `process.hrtime()` high-resolution timer function with docs.

  • Loading branch information...
1 parent 5ebc05f commit 31f0576c57ebc1736c196b74629df446a48b1bc4 @TooTallNate committed Mar 5, 2012
Showing with 33 additions and 0 deletions.
  1. +12 −0 doc/api/process.markdown
  2. +21 −0 src/node.cc
View
12 doc/api/process.markdown
@@ -359,3 +359,15 @@ given, otherwise returns the current mask.
## process.uptime()
Number of seconds Node has been running.
+
+
+## process.hrtime()
+
+Returns the current high-resolution real time in a `[seconds, nanoseconds]`
+tuple Array. It is relative to an arbitrary time in the past. It is not
+related to the time of day and therefore not subject to clock drift. The
+primary use is for measuring performance between intervals.
+
+Example output looks like:
+
+ [ 1800216, 927643717 ]
View
21 src/node.cc
@@ -1557,6 +1557,25 @@ Handle<Value> Kill(const Arguments& args) {
return Undefined();
}
+// used in Hrtime() below
+#define NANOS_PER_SEC 1000000000
+
+// Hrtime exposes libuv's uv_hrtime() high-resolution timer.
+// The value returned by uv_hrtime() is a 64-bit int representing nanoseconds,
+// so this function instead returns an Array with 2 entries representing seconds
+// and nanoseconds, to avoid any interger overflow possibility.
+Handle<Value> Hrtime(const v8::Arguments& args) {
+ HandleScope scope;
+
+ uint64_t t = uv_hrtime();
+
+ Local<Array> tuple = Array::New(2);
+ tuple->Set(Number::New(0), Integer::New(t / NANOS_PER_SEC));
+ tuple->Set(Number::New(1), Integer::New(t % NANOS_PER_SEC));
+
+ return scope.Close(tuple);
+}
+
typedef void (UV_DYNAMIC* extInit)(Handle<Object> exports);
@@ -2126,6 +2145,8 @@ Handle<Object> SetupProcessObject(int argc, char *argv[]) {
NODE_SET_METHOD(process, "_debugPause", DebugPause);
NODE_SET_METHOD(process, "_debugEnd", DebugEnd);
+ NODE_SET_METHOD(process, "hrtime", Hrtime);
+
NODE_SET_METHOD(process, "dlopen", DLOpen);
NODE_SET_METHOD(process, "uptime", Uptime);

2 comments on commit 31f0576

@tshinnic

s/interger/integer/ , or not...

@TooTallNate
Owner

Thanks @tshinnic, fixed!

Please sign in to comment.
Something went wrong with that request. Please try again.