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 + +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::microseconds MillionMicros(1000000); +/// errs() << floatDuration(MillionMicros) << "\n" +/// /* Print '1000.0' on the output */ +template +static double floatDuration(DurIn Dur) { + return std::chrono::duration_cast< + std::chrono::duration>(Dur) + .count(); +} + +} // namespace llvm + +#endif // LLVM_SUPPORT_CHRONO_H 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: 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(MillionMicros)); + ASSERT_EQ(1., floatDuration(MillionMicros)); + + std::chrono::seconds TenSeconds(10); + ASSERT_EQ(10000., floatDuration(TenSeconds)); + ASSERT_EQ(10000000., floatDuration(TenSeconds)); + + // Check that floatDuration accepts a non-float based duration + std::chrono::duration> TenSecondsInt(10); + ASSERT_EQ(10000., floatDuration(TenSecondsInt)); + ASSERT_EQ(10000000., floatDuration(TenSecondsInt)); +} + +} // anonymous namespace