]> git.saurik.com Git - apple/system_cmds.git/blobdiff - CPPUtil/UtilTimer.cpp
system_cmds-643.30.1.tar.gz
[apple/system_cmds.git] / CPPUtil / UtilTimer.cpp
diff --git a/CPPUtil/UtilTimer.cpp b/CPPUtil/UtilTimer.cpp
new file mode 100644 (file)
index 0000000..b285565
--- /dev/null
@@ -0,0 +1,31 @@
+//
+//  UtilTimer.cpp
+//  CPPUtil
+//
+//  Created by James McIlree on 10/9/13.
+//  Copyright (c) 2013 Apple. All rights reserved.
+//
+
+#include "CPPUtil.h"
+
+BEGIN_UTIL_NAMESPACE
+
+static mach_timebase_info_data_t timebase_info;
+
+Timer::Timer(const char* message) : _message(message) {
+       // C++ guarantees that static variable initialization is thread safe.
+       // We don't actually care what the returned value is, we just want to init timebase_info
+       // The pragma prevents spurious warnings.
+       static kern_return_t blah = mach_timebase_info(&timebase_info);
+#pragma unused(blah)
+
+       _start = AbsTime::now(); // Do this after the initialization check.
+}
+
+Timer::~Timer()
+{
+       _end = AbsTime::now();
+       printf("%s: %5.5f seconds\n", _message.c_str(), (double)(_end - _start).nano_time().value() / (double)NANOSECONDS_PER_SECOND);
+}
+
+END_UTIL_NAMESPACE