[PATCH] D25394: Add a Chrono.h header to libSupport

Mehdi AMINI via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 7 22:18:11 PDT 2016


mehdi_amini created this revision.
mehdi_amini added reviewers: labath, zturner.
mehdi_amini added a subscriber: llvm-commits.
Herald added subscribers: modocache, mgorny, beanz.

The intent is to add helpers (utility functions or wrapper)
to manipulate std::chrono types.


https://reviews.llvm.org/D25394

Files:
  include/llvm/Support/Chrono.h
  unittests/Support/CMakeLists.txt
  unittests/Support/Chrono.cpp


Index: unittests/Support/Chrono.cpp
===================================================================
--- /dev/null
+++ unittests/Support/Chrono.cpp
@@ -0,0 +1,32 @@
+//===- llvm/unittest/Support/Chrono.cpp - Time utilities tests ------------===//
+//
+//		       The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/Chrono.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+namespace {
+
+TEST(Chrono, FloatConversion) {
+  std::chrono::microseconds MillionMicros(1000000);
+  ASSERT_EQ(1000., floatDuration<std::chrono::milliseconds>(MillionMicros));
+  ASSERT_EQ(1., floatDuration<std::chrono::seconds>(MillionMicros));
+
+  std::chrono::seconds TenSeconds(10);
+  ASSERT_EQ(10000., floatDuration<std::chrono::milliseconds>(TenSeconds));
+  ASSERT_EQ(10000000., floatDuration<std::chrono::microseconds>(TenSeconds));
+
+  // Check that floatDuration accepts a non-float based duration
+  std::chrono::duration<int, std::ratio<1, 1>> TenSecondsInt(10);
+  ASSERT_EQ(10000., floatDuration<std::chrono::milliseconds>(TenSecondsInt));
+  ASSERT_EQ(10000000., floatDuration<std::chrono::microseconds>(TenSecondsInt));
+}
+
+} // anonymous namespace
Index: unittests/Support/CMakeLists.txt
===================================================================
--- unittests/Support/CMakeLists.txt
+++ unittests/Support/CMakeLists.txt
@@ -12,6 +12,7 @@
   CommandLineTest.cpp
   CompressionTest.cpp
   ConvertUTFTest.cpp
+  Chrono.cpp
   DataExtractorTest.cpp
   DwarfTest.cpp
   EndianStreamTest.cpp
Index: include/llvm/Support/Chrono.h
===================================================================
--- /dev/null
+++ include/llvm/Support/Chrono.h
@@ -0,0 +1,32 @@
+//===- llvm/Support/Chrono.h - Utilities for Timing Manipulation-*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_CHRONO_H
+#define LLVM_SUPPORT_CHRONO_H
+
+#include <chrono>
+
+namespace llvm {
+
+/// Return duration \p Dur as a floating point value, after conversion in the
+/// unit passed in as a template parameter. For example:
+///
+/// std::chrono::duration<std::chrono::microsecond> MillionMicros(1000000);
+/// errs() << floatDuration<std::chrono::milliseconds>(MillionMicros) << "\n"
+/// /* Print '1000.0' on the output */
+template <typename DurOut, typename DurIn>
+static double floatDuration(DurIn Dur) {
+  return std::chrono::duration_cast<
+             std::chrono::duration<double, typename DurOut::period>>(Dur)
+      .count();
+}
+
+} // namespace llvm
+
+#endif // LLVM_SUPPORT_CHRONO_H


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25394.74016.patch
Type: text/x-patch
Size: 2956 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161008/21a76431/attachment.bin>


More information about the llvm-commits mailing list