susumu.yata
null+****@clear*****
Wed Feb 27 18:23:36 JST 2013
susumu.yata 2013-02-27 18:23:36 +0900 (Wed, 27 Feb 2013) New Revision: 89cd2817dab9bb7539302c8c7f41fc10cfc32af4 https://github.com/groonga/grnxx/commit/89cd2817dab9bb7539302c8c7f41fc10cfc32af4 Log: Represent a time in microseconds since the Unix epoch. Remove now() and now_in_seconds() from grnxx::Time. Add grnxx::SystemClock and grnxx::SteadyClock for now(). Added files: lib/internal_clock.hpp Copied files: lib/steady_clock.hpp (from lib/duration.cpp) lib/system_clock.hpp (from lib/duration.cpp) Modified files: lib/Makefile.am lib/duration.cpp lib/duration.hpp lib/io/file-posix.cpp lib/io/file-windows.cpp lib/io/file_info.cpp lib/io/pool-impl.cpp lib/logger.cpp lib/mutex.cpp lib/recycler.cpp lib/thread.cpp lib/time.cpp lib/time.hpp test/test_db_blob_vector.cpp test/test_db_vector.cpp test/test_duration.cpp test/test_intrinsic.cpp test/test_io_pool.cpp test/test_mutex.cpp test/test_recycler.cpp test/test_string.cpp test/test_string_format.cpp test/test_thread.cpp test/test_time.cpp Modified: lib/Makefile.am (+3 -0) =================================================================== --- lib/Makefile.am 2013-02-26 16:49:04 +0900 (6cbbc67) +++ lib/Makefile.am 2013-02-27 18:23:36 +0900 (1ea106f) @@ -36,6 +36,7 @@ libgrnxx_include_HEADERS = \ features.hpp \ flags_impl.hpp \ grnxx.hpp \ + internal_clock.hpp \ intrinsic.hpp \ lock.hpp \ logger.hpp \ @@ -44,9 +45,11 @@ libgrnxx_include_HEADERS = \ os.hpp \ recycler.hpp \ slice.hpp \ + steady_clock.hpp \ string.hpp \ string_builder.hpp \ string_format.hpp \ + system_clock.hpp \ thread.hpp \ time.hpp \ version.h Modified: lib/duration.cpp (+9 -9) =================================================================== --- lib/duration.cpp 2013-02-26 16:49:04 +0900 (c1215f3) +++ lib/duration.cpp 2013-02-27 18:23:36 +0900 (c4402e1) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -28,17 +28,17 @@ StringBuilder &Duration::write_to(StringBuilder &builder) const { return builder; } - uint64_t nanoseconds; - if (nanoseconds_ >= 0) { - nanoseconds = nanoseconds_; + uint64_t count; + if (count_ >= 0) { + count = count_; } else { builder << '-'; - nanoseconds = -nanoseconds_; + count = -count_; } - builder << (nanoseconds / 1000000000); - nanoseconds %= 1000000000; - if (nanoseconds != 0) { - builder << '.' << StringFormat::align_right(nanoseconds, 9, '0'); + builder << (count / 1000000); + count %= 1000000; + if (count != 0) { + builder << '.' << StringFormat::align_right(count, 6, '0'); } return builder; } Modified: lib/duration.hpp (+64 -53) =================================================================== --- lib/duration.hpp 2013-02-26 16:49:04 +0900 (5a83c9d) +++ lib/duration.hpp 2013-02-27 18:23:36 +0900 (834b0d8) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -23,55 +23,66 @@ namespace grnxx { +// Time difference in microseconds. +// 64-bit tick count (usec) is used. class Duration { public: + // Trivial default constructor. Duration() = default; - explicit constexpr Duration(int64_t nanoseconds) - : nanoseconds_(nanoseconds) {} + // Construct a duration object whose tick count is "count". + explicit constexpr Duration(int64_t count) : count_(count) {} - static constexpr Duration nanoseconds(int64_t nanoseconds) { - return Duration(nanoseconds); + // Return the minimum tick count. + static constexpr Duration min() { + return Duration(std::numeric_limits<int64_t>::min()); } - static constexpr Duration microseconds(int64_t microseconds) { - return Duration(microseconds * 1000); + // Return the maximum tick count. + static constexpr Duration max() { + return Duration(std::numeric_limits<int64_t>::max()); } - static constexpr Duration milliseconds(int64_t milliseconds) { - return Duration(milliseconds * 1000000); + + // Return a duration of "count" microseconds. + static constexpr Duration microseconds(int64_t count) { + return Duration(count); + } + // Return a duration of "count" milliseconds. + static constexpr Duration milliseconds(int64_t count) { + return Duration(count * 1000); } - static constexpr Duration seconds(int64_t seconds) { - return Duration(seconds * 1000000000); + // Return a duration of "count" seconds. + static constexpr Duration seconds(int64_t count) { + return Duration(count * 1000000); } - static constexpr Duration minutes(int64_t minutes) { - return Duration(minutes * 1000000000 * 60); + // Return a duration of "count" minutes. + static constexpr Duration minutes(int64_t count) { + return Duration(count * 1000000 * 60); } - static constexpr Duration hours(int64_t hours) { - return Duration(hours * 1000000000 * 60 * 60); + // Return a duration of "count" hours. + static constexpr Duration hours(int64_t count) { + return Duration(count * 1000000 * 60 * 60); } - static constexpr Duration days(int64_t days) { - return Duration(days * 1000000000 * 60 * 60 * 24); + // Return a duration of "count" days. + static constexpr Duration days(int64_t count) { + return Duration(count * 1000000 * 60 * 60 * 24); } - static constexpr Duration weeks(int64_t weeks) { - return Duration(weeks * 1000000000 * 60 * 60 * 24 * 7); + // Return a duration of "count" weeks. + static constexpr Duration weeks(int64_t count) { + return Duration(count * 1000000 * 60 * 60 * 24 * 7); } - constexpr int64_t nanoseconds() { - return nanoseconds_; + // Return the tick count. + constexpr int64_t count() { + return count_; } - void set_nanoseconds(int64_t nanoseconds) { - nanoseconds_ = nanoseconds; + // Set the tick count. + void set_count(int64_t count) { + count_ = count; } StringBuilder &write_to(StringBuilder &builder) const; - static constexpr Duration max() { - return Duration(std::numeric_limits<int64_t>::max()); - } - static constexpr Duration min() { - return Duration(std::numeric_limits<int64_t>::min()); - } - private: - int64_t nanoseconds_; + int64_t count_; // Copyable. }; @@ -82,75 +93,75 @@ inline constexpr Duration operator+(Duration duration) { return duration; } inline constexpr Duration operator-(Duration duration) { - return Duration(-duration.nanoseconds()); + return Duration(-duration.count()); } inline Duration &operator+=(Duration &lhs, Duration rhs) { - lhs.set_nanoseconds(lhs.nanoseconds() + rhs.nanoseconds()); + lhs.set_count(lhs.count() + rhs.count()); return lhs; } inline Duration &operator-=(Duration &lhs, Duration rhs) { - lhs.set_nanoseconds(lhs.nanoseconds() - rhs.nanoseconds()); + lhs.set_count(lhs.count() - rhs.count()); return lhs; } inline Duration &operator*=(Duration &lhs, int64_t rhs) { - lhs.set_nanoseconds(lhs.nanoseconds() * rhs); + lhs.set_count(lhs.count() * rhs); return lhs; } inline Duration &operator/=(Duration &lhs, int64_t rhs) { if (rhs == 0) { - lhs.set_nanoseconds(0); + lhs.set_count(0); } else { - lhs.set_nanoseconds(lhs.nanoseconds() / rhs); + lhs.set_count(lhs.count() / rhs); } return lhs; } inline Duration &operator%=(Duration &lhs, Duration rhs) { - if (rhs.nanoseconds() == 0) { - lhs.set_nanoseconds(0); + if (rhs.count() == 0) { + lhs.set_count(0); } else { - lhs.set_nanoseconds(lhs.nanoseconds() % rhs.nanoseconds()); + lhs.set_count(lhs.count() % rhs.count()); } return lhs; } inline constexpr Duration operator+(Duration lhs, Duration rhs) { - return Duration(lhs.nanoseconds() + rhs.nanoseconds()); + return Duration(lhs.count() + rhs.count()); } inline constexpr Duration operator-(Duration lhs, Duration rhs) { - return Duration(lhs.nanoseconds() - rhs.nanoseconds()); + return Duration(lhs.count() - rhs.count()); } inline constexpr Duration operator*(Duration lhs, int64_t rhs) { - return Duration(lhs.nanoseconds() * rhs); + return Duration(lhs.count() * rhs); } inline constexpr Duration operator*(int64_t lhs, Duration rhs) { - return Duration(lhs * rhs.nanoseconds()); + return Duration(lhs * rhs.count()); } inline constexpr Duration operator/(Duration lhs, int64_t rhs) { - return (rhs != 0) ? Duration(lhs.nanoseconds() / rhs) : Duration(0); + return (rhs != 0) ? Duration(lhs.count() / rhs) : Duration(0); } inline constexpr Duration operator%(Duration lhs, Duration rhs) { - return (rhs.nanoseconds() != 0) ? - Duration(lhs.nanoseconds() % rhs.nanoseconds()) : Duration(0); + return (rhs.count() != 0) ? + Duration(lhs.count() % rhs.count()) : Duration(0); } inline constexpr bool operator==(Duration lhs, Duration rhs) { - return lhs.nanoseconds() == rhs.nanoseconds(); + return lhs.count() == rhs.count(); } inline constexpr bool operator!=(Duration lhs, Duration rhs) { - return lhs.nanoseconds() != rhs.nanoseconds(); + return lhs.count() != rhs.count(); } inline constexpr bool operator<(Duration lhs, Duration rhs) { - return lhs.nanoseconds() < rhs.nanoseconds(); + return lhs.count() < rhs.count(); } inline constexpr bool operator<=(Duration lhs, Duration rhs) { - return lhs.nanoseconds() <= rhs.nanoseconds(); + return lhs.count() <= rhs.count(); } inline constexpr bool operator>(Duration lhs, Duration rhs) { - return lhs.nanoseconds() > rhs.nanoseconds(); + return lhs.count() > rhs.count(); } inline constexpr bool operator>=(Duration lhs, Duration rhs) { - return lhs.nanoseconds() >= rhs.nanoseconds(); + return lhs.count() >= rhs.count(); } inline StringBuilder &operator<<(StringBuilder &builder, Duration duration) { Added: lib/internal_clock.hpp (+34 -0) 100644 =================================================================== --- /dev/null +++ lib/internal_clock.hpp 2013-02-27 18:23:36 +0900 (bdb3b72) @@ -0,0 +1,34 @@ +/* + Copyright (C) 2013 Brazil, Inc. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#ifndef GRNXX_INTERNAL_CLOCK_HPP +#define GRNXX_INTERNAL_CLOCK_HPP + +#include "basic.hpp" +#include "time.hpp" + +namespace grnxx { + +class InternalClock { + public: + // TODO + static Time now(); +}; + +} // namespace grnxx + +#endif // GRNXX_INTERNAL_CLOCK_HPP Modified: lib/io/file-posix.cpp (+3 -3) =================================================================== --- lib/io/file-posix.cpp 2013-02-26 16:49:04 +0900 (1e34657) +++ lib/io/file-posix.cpp 2013-02-27 18:23:36 +0900 (170f733) @@ -30,8 +30,8 @@ #include "../error.hpp" #include "../exception.hpp" #include "../logger.hpp" +#include "../steady_clock.hpp" #include "../thread.hpp" -#include "../time.hpp" #include "path.hpp" namespace grnxx { @@ -118,8 +118,8 @@ bool FileImpl::lock(FileLockMode mode, Duration timeout) { if (try_lock(mode)) { return true; } - const Time deadline = Time::now() + timeout; - while (Time::now() < deadline) { + const Time deadline = SteadyClock::now() + timeout; + while (SteadyClock::now() < deadline) { if (try_lock(mode)) { return true; } Modified: lib/io/file-windows.cpp (+3 -3) =================================================================== --- lib/io/file-windows.cpp 2013-02-26 16:49:04 +0900 (c40a85a) +++ lib/io/file-windows.cpp 2013-02-27 18:23:36 +0900 (c57bfcd) @@ -26,8 +26,8 @@ #include "../error.hpp" #include "../exception.hpp" #include "../logger.hpp" +#include "../steady_clock.hpp" #include "../thread.hpp" -#include "../time.hpp" #include "path.hpp" namespace grnxx { @@ -113,8 +113,8 @@ bool FileImpl::lock(FileLockMode mode, Duration timeout) { if (try_lock(mode)) { return true; } - const Time deadline = Time::now() + timeout; - while (Time::now() < deadline) { + const Time deadline = SteadyClock::now() + timeout; + while (SteadyClock::now() < deadline) { if (try_lock(mode)) { return true; } Modified: lib/io/file_info.cpp (+3 -3) =================================================================== --- lib/io/file_info.cpp 2013-02-26 16:49:04 +0900 (5075ed9) +++ lib/io/file_info.cpp 2013-02-27 18:23:36 +0900 (ad22972) @@ -80,13 +80,13 @@ class Impl : public FileInfo { return stat_.st_size; } Time last_access_time() const { - return Time(static_cast<int64_t>(stat_.st_atime) * 1000000000); + return Time(static_cast<int64_t>(stat_.st_atime) * 1000000); } Time last_modification_time() const { - return Time(static_cast<int64_t>(stat_.st_mtime) * 1000000000); + return Time(static_cast<int64_t>(stat_.st_mtime) * 1000000); } Time last_status_change_time() const { - return Time(static_cast<int64_t>(stat_.st_ctime) * 1000000000); + return Time(static_cast<int64_t>(stat_.st_ctime) * 1000000); } StringBuilder &write_to(StringBuilder &builder) const; Modified: lib/io/pool-impl.cpp (+4 -4) =================================================================== --- lib/io/pool-impl.cpp 2013-02-26 16:49:04 +0900 (419d3d5) +++ lib/io/pool-impl.cpp 2013-02-27 18:23:36 +0900 (19250c8) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -22,9 +22,9 @@ #include "../exception.hpp" #include "../lock.hpp" #include "../logger.hpp" +#include "../steady_clock.hpp" #include "../string_format.hpp" #include "../thread.hpp" -#include "../time.hpp" #include "path.hpp" namespace grnxx { @@ -338,8 +338,8 @@ void PoolImpl::open_regular_pool(PoolFlags flags, const char *path, if (!header_) { if ((flags & POOL_OPEN) || (~flags & POOL_CREATE)) { - const Time start_time = Time::now(); - while ((Time::now() - start_time) < Duration::seconds(10)) { + const Time start_time = SteadyClock::now(); + while ((SteadyClock::now() - start_time) < Duration::seconds(10)) { if (files_[0]->size() != 0) { break; } Modified: lib/logger.cpp (+3 -3) =================================================================== --- lib/logger.cpp 2013-02-26 16:49:04 +0900 (b20621c) +++ lib/logger.cpp 2013-02-27 18:23:36 +0900 (1f06576) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -23,7 +23,7 @@ #include "backtrace.hpp" #include "lock.hpp" -#include "time.hpp" +#include "system_clock.hpp" namespace grnxx { @@ -175,7 +175,7 @@ void Logger::append_line_header() { const LoggerFlags flags = Logger::flags(); if (flags & LOGGER_WITH_DATE_TIME) { - builder_ << Time::now() << ": "; + builder_ << SystemClock::now() << ": "; } if (flags & LOGGER_WITH_LOCATION) { builder_ << file_ << ':' << line_ << ": In " << func_ << "(): "; Modified: lib/mutex.cpp (+5 -5) =================================================================== --- lib/mutex.cpp 2013-02-26 16:49:04 +0900 (c6c6cd2) +++ lib/mutex.cpp 2013-02-27 18:23:36 +0900 (d9d89d1) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -17,8 +17,8 @@ */ #include "mutex.hpp" +#include "steady_clock.hpp" #include "thread.hpp" -#include "time.hpp" namespace grnxx { @@ -55,11 +55,11 @@ bool Mutex::lock_with_timeout(Duration timeout) { const bool has_deadline = timeout >= Duration(0); Time deadline; if (has_deadline) { - deadline = Time::now() + timeout; + deadline = SteadyClock::now() + timeout; } for (int i = 0; i < MUTEX_CONTEXT_SWITCH_COUNT; ++i) { - if (has_deadline && (Time::now() >= deadline)) { + if (has_deadline && (SteadyClock::now() >= deadline)) { return false; } if (try_lock()) { @@ -68,7 +68,7 @@ bool Mutex::lock_with_timeout(Duration timeout) { Thread::switch_to_others(); } - while (!has_deadline || (Time::now() < deadline)) { + while (!has_deadline || (SteadyClock::now() < deadline)) { if (try_lock()) { return true; } Modified: lib/recycler.cpp (+5 -4) =================================================================== --- lib/recycler.cpp 2013-02-26 16:49:04 +0900 (f496686) +++ lib/recycler.cpp 2013-02-27 18:23:36 +0900 (82aa0e6) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -17,6 +17,7 @@ */ #include "recycler.hpp" +#include "system_clock.hpp" namespace grnxx { @@ -25,10 +26,10 @@ void Recycler::update() { // will not come into this function. However, this is not a perfect barrier. count_ = 0; - // Time::now() takes around 1 microsecond on Core2 Duo 1.6GHz. So, if + // SystemClock::now() takes around 1 microsecond on Core2 Duo 1.6GHz. So, if // RECYCLER_STAMP_COUNT_PER_UPDATE == 500, stamp() spends 2ns/call for - // Time::now() on average. - const Time now = Time::now(); + // SystemClock::now() on average. + const Time now = SystemClock::now(); StampPair current_stamp_pair = stamp_pair_; Copied: lib/steady_clock.hpp (+14 -29) 51% =================================================================== --- lib/duration.cpp 2013-02-26 16:49:04 +0900 (c1215f3) +++ lib/steady_clock.hpp 2013-02-27 18:23:36 +0900 (0424a87) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -15,39 +15,24 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "duration.hpp" +#ifndef GRNXX_STEADY_CLOCK_HPP +#define GRNXX_STEADY_CLOCK_HPP -#include <ostream> +#include "basic.hpp" +#include "time.hpp" -#include "string_format.hpp" +#include <chrono> namespace grnxx { -StringBuilder &Duration::write_to(StringBuilder &builder) const { - if (!builder) { - return builder; +class SteadyClock { + public: + static Time now() { + return Time(std::chrono::duration_cast<std::chrono::microseconds>( + std::chrono::steady_clock::now().time_since_epoch()).count()); } - - uint64_t nanoseconds; - if (nanoseconds_ >= 0) { - nanoseconds = nanoseconds_; - } else { - builder << '-'; - nanoseconds = -nanoseconds_; - } - builder << (nanoseconds / 1000000000); - nanoseconds %= 1000000000; - if (nanoseconds != 0) { - builder << '.' << StringFormat::align_right(nanoseconds, 9, '0'); - } - return builder; -} - -std::ostream &operator<<(std::ostream &stream, Duration duration) { - char buf[32]; - StringBuilder builder(buf); - builder << duration; - return stream.write(builder.c_str(), builder.length()); -} +}; } // namespace grnxx + +#endif // GRNXX_STEADY_CLOCK_HPP Copied: lib/system_clock.hpp (+14 -29) 51% =================================================================== --- lib/duration.cpp 2013-02-26 16:49:04 +0900 (c1215f3) +++ lib/system_clock.hpp 2013-02-27 18:23:36 +0900 (048ec2b) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -15,39 +15,24 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "duration.hpp" +#ifndef GRNXX_SYSTEM_CLOCK_HPP +#define GRNXX_SYSTEM_CLOCK_HPP -#include <ostream> +#include "basic.hpp" +#include "time.hpp" -#include "string_format.hpp" +#include <chrono> namespace grnxx { -StringBuilder &Duration::write_to(StringBuilder &builder) const { - if (!builder) { - return builder; +class SystemClock { + public: + static Time now() { + return Time(std::chrono::duration_cast<std::chrono::microseconds>( + std::chrono::system_clock::now().time_since_epoch()).count()); } - - uint64_t nanoseconds; - if (nanoseconds_ >= 0) { - nanoseconds = nanoseconds_; - } else { - builder << '-'; - nanoseconds = -nanoseconds_; - } - builder << (nanoseconds / 1000000000); - nanoseconds %= 1000000000; - if (nanoseconds != 0) { - builder << '.' << StringFormat::align_right(nanoseconds, 9, '0'); - } - return builder; -} - -std::ostream &operator<<(std::ostream &stream, Duration duration) { - char buf[32]; - StringBuilder builder(buf); - builder << duration; - return stream.write(builder.c_str(), builder.length()); -} +}; } // namespace grnxx + +#endif // GRNXX_SYSTEM_CLOCK_HPP Modified: lib/thread.cpp (+7 -7) =================================================================== --- lib/thread.cpp 2013-02-26 16:49:04 +0900 (c00aae6) +++ lib/thread.cpp 2013-02-27 18:23:36 +0900 (9b27d4b) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -46,10 +46,10 @@ bool Thread::switch_to_others() { void Thread::sleep(Duration duration) { #ifdef GRNXX_WINDOWS - if (duration.nanoseconds() < 0) { + if (duration.count() < 0) { ::Sleep(0); } else { - const int64_t milliseconds = duration.nanoseconds() / 1000000; + const int64_t milliseconds = duration.count() / 1000; if (milliseconds < static_cast<int64_t>(std::numeric_limits<DWORD>::max())) { ::Sleep(static_cast<DWORD>(milliseconds)); @@ -59,24 +59,24 @@ void Thread::sleep(Duration duration) { } #elif defined(GRNXX_HAS_NANOSLEEP) struct timespec request; - if (duration.nanoseconds() < 0) { + if (duration.count() < 0) { request.tv_sec = 0; request.tv_nsec = 0; } else { - const int64_t seconds = duration.nanoseconds() / 1000000000; + const int64_t seconds = duration.count() / 1000000; if (seconds < std::numeric_limits<time_t>::max()) { request.tv_sec = static_cast<time_t>(seconds); } else { request.tv_sec = std::numeric_limits<time_t>::max(); } duration %= Duration::seconds(1); - request.tv_nsec = static_cast<long>(duration.nanoseconds()); + request.tv_nsec = static_cast<long>(duration.count() * 1000); } // Note that ::nanosleep() requires -lrt option. ::nanosleep(&request, nullptr); #else // defined(GRNXX_HAS_NANOSLEEP) // Note that POSIX.1-2008 removes the specification of ::usleep(). - const int64_t microseconds = duration.nanoseconds() / 1000; + const int64_t microseconds = duration.count(); if (microseconds < std::numeric_limits<useconds_t>::max()) { ::usleep(static_cast<useconds_t>(microseconds)); } else { Modified: lib/time.cpp (+3 -44) =================================================================== --- lib/time.cpp 2013-02-26 16:49:04 +0900 (9b52c14) +++ lib/time.cpp 2013-02-27 18:23:36 +0900 (1ac00f2) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -37,53 +37,12 @@ namespace grnxx { -Time Time::now() { -#ifdef GRNXX_WINDOWS - struct _timeb current_time; -# ifdef GRNXX_MSC - const errno_t error_code = ::_ftime_s(¤t_time); - if (error_code != 0) { - // In practice, ::_ftime_s() must not fail. - return now_in_seconds(); - } -# else // GRNXX_MSC - ::_ftime(¤t_time); -# endif // GRNXX_MSC - return Time((current_time.time * int64_t(1000000000)) + - (current_time.millitm * 1000000)); -#else // GRNXX_WINDOWS -# if defined(GRNXX_HAS_CLOCK_GETTIME) - struct timespec current_time; - if (::clock_gettime(CLOCK_REALTIME, ¤t_time) != 0) { - // In practice, ::clock_gettime() must not fail. - return now_in_seconds(); - } - return Time((current_time.tv_sec * int64_t(1000000000)) + - current_time.tv_nsec); -# else // defined(GRNXX_HAS_CLOCK_GETTIME) - // Note: POSIX.1-2008 marks gettimeofday() as obsolete. - struct timeval current_time; - if (::gettimeofday(¤t_time, nullptr) != 0) { - // In practice, ::gettimeofday() must not fail. - return now_in_seconds(); - } - return Time((current_time.tv_sec * int64_t(1000000000)) + - (current_time.tv_usec * 1000)); -# endif // defined(GRNXX_HAS_CLOCK_GETTIME) -#endif // GRNXX_WINDOWS -} - -Time Time::now_in_seconds() { - return Time(static_cast<int64_t>(std::time(nullptr)) * 1000000000); -} - StringBuilder &Time::write_to(StringBuilder &builder) const { if (!builder) { return builder; } - const std::time_t posix_time = - static_cast<std::time_t>(nanoseconds_ / 1000000000); + const std::time_t posix_time = static_cast<std::time_t>(count_ / 1000000); struct tm broken_down_time; #ifdef GRNXX_MSC if (::localtime_s(&posix_time, &broken_down_time) != 0) { @@ -108,7 +67,7 @@ StringBuilder &Time::write_to(StringBuilder &builder) const { << StringFormat::align_right(broken_down_time.tm_hour, 2, '0') << ':' << StringFormat::align_right(broken_down_time.tm_min, 2, '0') << ':' << StringFormat::align_right(broken_down_time.tm_sec, 2, '0') << '.' - << StringFormat::align_right(nanoseconds_ / 1000 % 1000000, 6, '0'); + << StringFormat::align_right(count_ % 1000000, 6, '0'); return builder; } Modified: lib/time.hpp (+33 -28) =================================================================== --- lib/time.hpp 2013-02-26 16:49:04 +0900 (45faf2e) +++ lib/time.hpp 2013-02-27 18:23:36 +0900 (c6fee88) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -23,32 +23,37 @@ namespace grnxx { +// Time in microseconds since the Unix epoch (1970-01-01 00:00:00 UTC). +// 64-bit tick count (usec) is used. class Time { public: + // Trivial default constructor. Time() = default; - explicit constexpr Time(int64_t nanoseconds) : nanoseconds_(nanoseconds) {} + // Construct a time object whose tick count is "count". + explicit constexpr Time(int64_t count) : count_(count) {} - static Time now(); - static Time now_in_seconds(); - - constexpr int64_t nanoseconds() { - return nanoseconds_; - } - void set_nanoseconds(int64_t nanoseconds) { - nanoseconds_ = nanoseconds; + // Return the minimum tick count. + static constexpr Time min() { + return Time(std::numeric_limits<int64_t>::min()); } - - StringBuilder &write_to(StringBuilder &builder) const; - + // Return the maximum tick count. static constexpr Time max() { return Time(std::numeric_limits<int64_t>::max()); } - static constexpr Time min() { - return Time(std::numeric_limits<int64_t>::min()); + + // Return the tick count. + constexpr int64_t count() { + return count_; } + // Set the tick count. + void set_count(int64_t count) { + count_ = count; + } + + StringBuilder &write_to(StringBuilder &builder) const; private: - int64_t nanoseconds_; + int64_t count_; // Copyable. }; @@ -56,44 +61,44 @@ class Time { GRNXX_ASSERT_POD(Time); inline Time &operator+=(Time &lhs, Duration rhs) { - lhs.set_nanoseconds(lhs.nanoseconds() + rhs.nanoseconds()); + lhs.set_count(lhs.count() + rhs.count()); return lhs; } inline Time &operator-=(Time &lhs, Duration rhs) { - lhs.set_nanoseconds(lhs.nanoseconds() - rhs.nanoseconds()); + lhs.set_count(lhs.count() - rhs.count()); return lhs; } inline constexpr Time operator+(Time lhs, Duration rhs) { - return Time(lhs.nanoseconds() + rhs.nanoseconds()); + return Time(lhs.count() + rhs.count()); } inline constexpr Time operator+(Duration lhs, Time rhs) { - return Time(lhs.nanoseconds() + rhs.nanoseconds()); + return Time(lhs.count() + rhs.count()); } inline constexpr Time operator-(Time lhs, Duration rhs) { - return Time(lhs.nanoseconds() - rhs.nanoseconds()); + return Time(lhs.count() - rhs.count()); } inline constexpr Duration operator-(Time lhs, Time rhs) { - return Duration(lhs.nanoseconds() - rhs.nanoseconds()); + return Duration(lhs.count() - rhs.count()); } inline constexpr bool operator==(Time lhs, Time rhs) { - return lhs.nanoseconds() == rhs.nanoseconds(); + return lhs.count() == rhs.count(); } inline constexpr bool operator!=(Time lhs, Time rhs) { - return lhs.nanoseconds() != rhs.nanoseconds(); + return lhs.count() != rhs.count(); } inline constexpr bool operator<(Time lhs, Time rhs) { - return lhs.nanoseconds() < rhs.nanoseconds(); + return lhs.count() < rhs.count(); } inline constexpr bool operator<=(Time lhs, Time rhs) { - return lhs.nanoseconds() <= rhs.nanoseconds(); + return lhs.count() <= rhs.count(); } inline constexpr bool operator>(Time lhs, Time rhs) { - return lhs.nanoseconds() > rhs.nanoseconds(); + return lhs.count() > rhs.count(); } inline constexpr bool operator>=(Time lhs, Time rhs) { - return lhs.nanoseconds() >= rhs.nanoseconds(); + return lhs.count() >= rhs.count(); } inline StringBuilder &operator<<(StringBuilder &builder, Time time) { Modified: test/test_db_blob_vector.cpp (+10 -8) =================================================================== --- test/test_db_blob_vector.cpp 2013-02-26 16:49:04 +0900 (cb7a811) +++ test/test_db_blob_vector.cpp 2013-02-27 18:23:36 +0900 (04234f0) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -22,7 +22,7 @@ #include "db/blob_vector.hpp" #include "logger.hpp" -#include "time.hpp" +#include "steady_clock.hpp" void test_basics() { grnxx::io::Pool::unlink_if_exists("temp.grn"); @@ -361,27 +361,29 @@ void test_defrag() { vector[ids[i]].set(&value[0], length); } - grnxx::Time start = grnxx::Time::now(); + grnxx::Time start = grnxx::SteadyClock::now(); for (std::uint32_t i = 0; i < NUM_VALUES; ++i) { grnxx::db::Blob blob = vector[i]; if (blob.length() > 0) { assert(*static_cast<const char *>(blob.address()) == 'X'); }; } - grnxx::Duration elapsed = (grnxx::Time::now() - start) / NUM_VALUES; - GRNXX_NOTICE() << "before defrag: elapsed [ns] = " << elapsed.nanoseconds(); + grnxx::Duration elapsed = grnxx::SteadyClock::now() - start; + GRNXX_NOTICE() << "before defrag: elapsed [ns] = " + << (elapsed.count() * 1000 / NUM_VALUES); vector.defrag(); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint32_t i = 0; i < NUM_VALUES; ++i) { grnxx::db::Blob blob = vector[i]; if (blob.length() > 0) { assert(*static_cast<const char *>(blob.address()) == 'X'); }; } - elapsed = (grnxx::Time::now() - start) / NUM_VALUES; - GRNXX_NOTICE() << "after defrag: elapsed [ns] = " << elapsed.nanoseconds(); + elapsed = grnxx::SteadyClock::now() - start; + GRNXX_NOTICE() << "after defrag: elapsed [ns] = " + << (elapsed.count() * 1000 / NUM_VALUES); } int main() { Modified: test/test_db_vector.cpp (+35 -35) =================================================================== --- test/test_db_vector.cpp 2013-02-26 16:49:04 +0900 (3c8b968) +++ test/test_db_vector.cpp 2013-02-27 18:23:36 +0900 (0f146d8) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -22,7 +22,7 @@ #include "db/vector.hpp" #include "logger.hpp" -#include "time.hpp" +#include "steady_clock.hpp" struct Point { double x; @@ -194,51 +194,51 @@ void test_times() { std::uint64_t total = 0; - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint64_t id = 0; id < VECTOR_SIZE; ++id) { vector[id] = T(0); } - end = grnxx::Time::now(); - double set_1st_elapsed = 1.0 * (end - start).nanoseconds() / VECTOR_SIZE; + end = grnxx::SteadyClock::now(); + double set_1st_elapsed = 1000.0 * (end - start).count() / VECTOR_SIZE; - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint64_t id = 0; id < VECTOR_SIZE; ++id) { vector[id] = T(1); } - end = grnxx::Time::now(); - double set_2nd_elapsed = 1.0 * (end - start).nanoseconds() / VECTOR_SIZE; + end = grnxx::SteadyClock::now(); + double set_2nd_elapsed = 1000.0 * (end - start).count() / VECTOR_SIZE; - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint64_t id = 0; id < VECTOR_SIZE; ++id) { total += vector[id]; } - end = grnxx::Time::now(); - double get_elapsed = 1.0 * (end - start).nanoseconds() / VECTOR_SIZE; + end = grnxx::SteadyClock::now(); + double get_elapsed = 1000.0 * (end - start).count() / VECTOR_SIZE; - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint64_t id = vector.max_id() - VECTOR_SIZE + 1; id <= vector.max_id(); ++id) { vector[id] = T(0); } - end = grnxx::Time::now(); - double ex_set_1st_elapsed = 1.0 * (end - start).nanoseconds() / VECTOR_SIZE; + end = grnxx::SteadyClock::now(); + double ex_set_1st_elapsed = 1000.0 * (end - start).count() / VECTOR_SIZE; - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint64_t id = vector.max_id() - VECTOR_SIZE + 1; id <= vector.max_id(); ++id) { vector[id] = T(1); } - end = grnxx::Time::now(); - double ex_set_2nd_elapsed = 1.0 * (end - start).nanoseconds() / VECTOR_SIZE; + end = grnxx::SteadyClock::now(); + double ex_set_2nd_elapsed = 1000.0 * (end - start).count() / VECTOR_SIZE; - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint64_t id = vector.max_id() - VECTOR_SIZE + 1; id <= vector.max_id(); ++id) { total += vector[id]; } - end = grnxx::Time::now(); - double ex_get_elapsed = 1.0 * (end - start).nanoseconds() / VECTOR_SIZE; + end = grnxx::SteadyClock::now(); + double ex_get_elapsed = 1000.0 * (end - start).count() / VECTOR_SIZE; const std::uint64_t boundary = vector.page_size() * vector.table_size(); @@ -256,46 +256,46 @@ void test_times() { ids[i] = id_begin + (engine() % range); } - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (int i = 0; i < VECTOR_SIZE; ++i) { vector[ids[i]] = T(0); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); double boundary_set_1st_elapsed = - 1.0 * (end - start).nanoseconds() / VECTOR_SIZE; + 1000.0 * (end - start).count() / VECTOR_SIZE; - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (int i = 0; i < VECTOR_SIZE; ++i) { vector[ids[i]] = T(1); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); double boundary_set_2nd_elapsed = - 1.0 * (end - start).nanoseconds() / VECTOR_SIZE; + 1000.0 * (end - start).count() / VECTOR_SIZE; - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (int i = 0; i < VECTOR_SIZE; ++i) { total += vector[ids[i]]; } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); double boundary_get_elapsed = - 1.0 * (end - start).nanoseconds() / VECTOR_SIZE; + 1000.0 * (end - start).count() / VECTOR_SIZE; const std::uint32_t block_id = vector.block_id(); vector.close(); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); grnxx::db::Vector<T>::unlink(pool, block_id); - end = grnxx::Time::now(); - double unlink_elapsed = (end - start).nanoseconds(); + end = grnxx::SteadyClock::now(); + double unlink_elapsed = 1000.0 * (end - start).count(); vector.create(pool, 0); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint64_t id = 0; id < VECTOR_SIZE; ++id) { vector[id] = T(0); } - end = grnxx::Time::now(); - double default_elapsed = 1.0 * (end - start).nanoseconds() / VECTOR_SIZE; + end = grnxx::SteadyClock::now(); + double default_elapsed = 1000.0 * (end - start).count() / VECTOR_SIZE; GRNXX_NOTICE() << "elapsed [ns]: set = " << set_2nd_elapsed << " (" Modified: test/test_duration.cpp (+17 -18) =================================================================== --- test/test_duration.cpp 2013-02-26 16:49:04 +0900 (fe8db77) +++ test/test_duration.cpp 2013-02-27 18:23:36 +0900 (a174db1) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -26,23 +26,22 @@ int main() { grnxx::LOGGER_ENABLE_COUT); grnxx::Logger::set_max_level(grnxx::NOTICE_LOGGER); - assert(grnxx::Duration::max().nanoseconds() == - std::numeric_limits<std::int64_t>::max()); - assert(grnxx::Duration::min().nanoseconds() == + assert(grnxx::Duration::min().count() == std::numeric_limits<std::int64_t>::min()); + assert(grnxx::Duration::max().count() == + std::numeric_limits<std::int64_t>::max()); - assert(grnxx::Duration(123).nanoseconds() == 123); + assert(grnxx::Duration(123).count() == 123); - assert(grnxx::Duration::nanoseconds(123).nanoseconds() == 123); - assert(grnxx::Duration::seconds(1).nanoseconds() == 1000000000); - assert(grnxx::Duration::minutes(1).nanoseconds() == 1000000000LL * 60); - assert(grnxx::Duration::hours(1).nanoseconds() == 1000000000LL * 60 * 60); - assert(grnxx::Duration::days(1).nanoseconds() == - 1000000000LL * 60 * 60 * 24); - assert(grnxx::Duration::weeks(1).nanoseconds() == - 1000000000LL * 60 * 60 * 24 * 7); + assert(grnxx::Duration::microseconds(123).count() == 123); + assert(grnxx::Duration::seconds(1).count() == 1000000); + assert(grnxx::Duration::minutes(1).count() == 1000000LL * 60); + assert(grnxx::Duration::hours(1).count() == 1000000LL * 60 * 60); + assert(grnxx::Duration::days(1).count() == 1000000LL * 60 * 60 * 24); + assert(grnxx::Duration::weeks(1).count() == 1000000LL * 60 * 60 * 24 * 7); - GRNXX_NOTICE() << "nanosecond = " << grnxx::Duration::nanoseconds(1); + GRNXX_NOTICE() << "microseconds = " << grnxx::Duration::microseconds(1); + GRNXX_NOTICE() << "milliseconds = " << grnxx::Duration::milliseconds(1); GRNXX_NOTICE() << "second = " << grnxx::Duration::seconds(1); GRNXX_NOTICE() << "minute = " << grnxx::Duration::minutes(1); GRNXX_NOTICE() << "hour = " << grnxx::Duration::hours(1); @@ -92,7 +91,7 @@ int main() { std::stringstream stream; stream << grnxx::Duration(123456789); - assert(stream.str() == "0.123456789"); + assert(stream.str() == "123.456789"); stream.str(""); stream << grnxx::Duration::seconds(123); @@ -100,11 +99,11 @@ int main() { stream.str(""); stream << (grnxx::Duration::seconds(456) + grnxx::Duration(789)); - assert(stream.str() == "456.000000789"); + assert(stream.str() == "456.000789"); stream.str(""); stream << grnxx::Duration(-123456789); - assert(stream.str() == "-0.123456789"); + assert(stream.str() == "-123.456789"); stream.str(""); stream << grnxx::Duration::seconds(-123); @@ -112,7 +111,7 @@ int main() { stream.str(""); stream << -(grnxx::Duration::seconds(456) + grnxx::Duration(789)); - assert(stream.str() == "-456.000000789"); + assert(stream.str() == "-456.000789"); return 0; } Modified: test/test_intrinsic.cpp (+20 -20) =================================================================== --- test/test_intrinsic.cpp 2013-02-26 16:49:04 +0900 (1d6fc9d) +++ test/test_intrinsic.cpp 2013-02-27 18:23:36 +0900 (2a71f58) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -19,7 +19,7 @@ #include "intrinsic.hpp" #include "logger.hpp" -#include "time.hpp" +#include "steady_clock.hpp" void test_basics() { assert(grnxx::bit_scan_reverse(std::uint8_t(100)) == 6); @@ -100,69 +100,69 @@ void test_times() { grnxx::Time start, end; - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); std::uint64_t total = 0; for (std::uint32_t i = 1; i <= LOOP_COUNT; ++i) { total += grnxx::bit_scan_reverse(i); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "bit_scan_reverse<32>: total = " << total << ", elapsed [ns] = " - << (1.0 * (end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); total = 0; for (std::uint64_t i = 1; i <= LOOP_COUNT; ++i) { total += grnxx::bit_scan_reverse(i << 20); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "bit_scan_reverse<64>: total = " << total << ", elapsed [ns] = " - << (1.0 * (end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); volatile std::uint32_t count_32 = 0; for (std::uint32_t i = 0; i < LOOP_COUNT; ++i) { assert(grnxx::atomic_fetch_and_add(1, &count_32) == i); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); assert(count_32 == LOOP_COUNT); GRNXX_NOTICE() << "atomic_fetch_and_add<32>: total = " << count_32 << ", elapsed [ns] = " - << (1.0 * (end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); volatile std::uint64_t count_64 = 0; for (std::uint32_t i = 0; i < LOOP_COUNT; ++i) { assert(grnxx::atomic_fetch_and_add(1, &count_64) == i); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); assert(count_64 == LOOP_COUNT); GRNXX_NOTICE() << "atomic_fetch_and_add<64>: total = " << count_64 << ", elapsed [ns] = " - << (1.0 * (end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); const std::int32_t a_32 = 0, b_32 = 1; volatile std::int32_t value_32 = a_32; for (std::uint32_t i = 0; i < (LOOP_COUNT / 2); ++i) { assert(grnxx::atomic_compare_and_swap(a_32, b_32, &value_32)); assert(grnxx::atomic_compare_and_swap(b_32, a_32, &value_32)); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "atomic_compare_and_swap<32>: elapsed [ns] = " - << (1.0 * (end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); const std::int64_t a_64 = 0, b_64 = 1; volatile std::int64_t value_64 = a_64; for (std::uint32_t i = 0; i < (LOOP_COUNT / 2); ++i) { assert(grnxx::atomic_compare_and_swap(a_64, b_64, &value_64)); assert(grnxx::atomic_compare_and_swap(b_64, a_64, &value_64)); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "atomic_compare_and_swap<64>: elapsed [ns] = " - << (1.0 * (end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); } int main() { Modified: test/test_io_pool.cpp (+20 -19) =================================================================== --- test/test_io_pool.cpp 2013-02-26 16:49:04 +0900 (5beb7ee) +++ test/test_io_pool.cpp 2013-02-27 18:23:36 +0900 (ea01491) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -23,6 +23,7 @@ #include "io/pool.hpp" #include "logger.hpp" +#include "steady_clock.hpp" void test_constructor() { grnxx::io::Pool::unlink_if_exists("temp.grn"); @@ -361,58 +362,58 @@ void benchmark() { grnxx::io::Pool pool(grnxx::io::POOL_TEMPORARY, "temp.grn"); // Measure the speed of create_block(). - grnxx::Time start_time = grnxx::Time::now(); + grnxx::Time start_time = grnxx::SteadyClock::now(); for (int i = 0; i < OPERATION_COUNT; ++i) { block_infos[i] = pool.create_block(0); } - grnxx::Duration elapsed = grnxx::Time::now() - start_time; + grnxx::Duration elapsed = grnxx::SteadyClock::now() - start_time; GRNXX_NOTICE() << "create_block: elapsed [ns] = " - << (elapsed / OPERATION_COUNT).nanoseconds(); + << (1000.0 * elapsed.count() / OPERATION_COUNT); // Measure the speed of get_block_info(). - start_time = grnxx::Time::now(); + start_time = grnxx::SteadyClock::now(); for (int i = 0; i < OPERATION_COUNT; ++i) { pool.get_block_info(block_infos[i]->id()); } - elapsed = grnxx::Time::now() - start_time; + elapsed = grnxx::SteadyClock::now() - start_time; GRNXX_NOTICE() << "get_block_info: elapsed [ns] = " - << (elapsed / OPERATION_COUNT).nanoseconds(); + << (1000.0 * elapsed.count() / OPERATION_COUNT); // Measure the speed of get_block_address(). - start_time = grnxx::Time::now(); + start_time = grnxx::SteadyClock::now(); for (int i = 0; i < OPERATION_COUNT; ++i) { pool.get_block_address(*block_infos[i]); } - elapsed = grnxx::Time::now() - start_time; + elapsed = grnxx::SteadyClock::now() - start_time; GRNXX_NOTICE() << "get_block_address_by_info (1st): elapsed [ns] = " - << (elapsed / OPERATION_COUNT).nanoseconds(); + << (1000.0 * elapsed.count() / OPERATION_COUNT); // Measure the speed of get_block_address() again. - start_time = grnxx::Time::now(); + start_time = grnxx::SteadyClock::now(); for (int i = 0; i < OPERATION_COUNT; ++i) { pool.get_block_address(*block_infos[i]); } - elapsed = grnxx::Time::now() - start_time; + elapsed = grnxx::SteadyClock::now() - start_time; GRNXX_NOTICE() << "get_block_address_by_info (2nd): elapsed [ns] = " - << (elapsed / OPERATION_COUNT).nanoseconds(); + << (1000.0 * elapsed.count() / OPERATION_COUNT); // Measure the speed of get_block_address() again and again. - start_time = grnxx::Time::now(); + start_time = grnxx::SteadyClock::now(); for (int i = 0; i < OPERATION_COUNT; ++i) { pool.get_block_address(block_infos[i]->id()); } - elapsed = grnxx::Time::now() - start_time; + elapsed = grnxx::SteadyClock::now() - start_time; GRNXX_NOTICE() << "get_block_address_by_id: elapsed [ns] = " - << (elapsed / OPERATION_COUNT).nanoseconds(); + << (1000.0 * elapsed.count() / OPERATION_COUNT); // Measure the speed of free_block(). - start_time = grnxx::Time::now(); + start_time = grnxx::SteadyClock::now(); for (int i = 0; i < OPERATION_COUNT; ++i) { pool.free_block(block_infos[i]->id()); } - elapsed = grnxx::Time::now() - start_time; + elapsed = grnxx::SteadyClock::now() - start_time; GRNXX_NOTICE() << "free_block: elapsed [ns] = " - << (elapsed / OPERATION_COUNT).nanoseconds(); + << (1000.0 * elapsed.count() / OPERATION_COUNT); } int main() { Modified: test/test_mutex.cpp (+5 -5) =================================================================== --- test/test_mutex.cpp 2013-02-26 16:49:04 +0900 (15d3bcb) +++ test/test_mutex.cpp 2013-02-27 18:23:36 +0900 (bff7291) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -20,7 +20,7 @@ #include "lock.hpp" #include "logger.hpp" #include "mutex.hpp" -#include "time.hpp" +#include "steady_clock.hpp" int main() { grnxx::Logger::set_flags(grnxx::LOGGER_WITH_ALL | @@ -64,15 +64,15 @@ int main() { enum { LOOP_COUNT = 1 << 20 }; - const grnxx::Time start = grnxx::Time::now(); + const grnxx::Time start = grnxx::SteadyClock::now(); for (int i = 0; i < LOOP_COUNT; ++i) { grnxx::Lock lock(&mutex); assert(lock); } - const grnxx::Time end = grnxx::Time::now(); + const grnxx::Time end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "grnxx::Lock: elapsed [ns] = " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); return 0; } Modified: test/test_recycler.cpp (+8 -7) =================================================================== --- test/test_recycler.cpp 2013-02-26 16:49:04 +0900 (eb5feda) +++ test/test_recycler.cpp 2013-02-27 18:23:36 +0900 (c1073ab) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -19,6 +19,7 @@ #include "logger.hpp" #include "recycler.hpp" +#include "steady_clock.hpp" void test() { const grnxx::Duration FROZEN_DURATION = grnxx::Duration::minutes(10); @@ -51,22 +52,22 @@ void benchmark(grnxx::Duration frozen_duration) { double stamp_elapsed; { - const grnxx::Time start = grnxx::Time::now(); + const grnxx::Time start = grnxx::SteadyClock::now(); for (int i = 0; i < STAMP_COUNT; ++i) { recycler.stamp(); } - const grnxx::Time end = grnxx::Time::now(); - stamp_elapsed = 1.0 * (end - start).nanoseconds() / STAMP_COUNT; + const grnxx::Time end = grnxx::SteadyClock::now(); + stamp_elapsed = 1000.0 * (end - start).count() / STAMP_COUNT; } double check_elapsed; { - const grnxx::Time start = grnxx::Time::now(); + const grnxx::Time start = grnxx::SteadyClock::now(); for (int i = 0; i < CHECK_COUNT; ++i) { recycler.check(std::uint16_t(i)); } - const grnxx::Time end = grnxx::Time::now(); - check_elapsed = 1.0 * (end - start).nanoseconds() / CHECK_COUNT; + const grnxx::Time end = grnxx::SteadyClock::now(); + check_elapsed = 1000.0 * (end - start).count() / CHECK_COUNT; } GRNXX_NOTICE() << "frozen_duration = " << frozen_duration Modified: test/test_string.cpp (+8 -8) =================================================================== --- test/test_string.cpp 2013-02-26 16:49:04 +0900 (09de202) +++ test/test_string.cpp 2013-02-27 18:23:36 +0900 (651108f) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -19,7 +19,7 @@ #include "logger.hpp" #include "string.hpp" -#include "time.hpp" +#include "steady_clock.hpp" void test_constructors() { assert(!grnxx::String()); @@ -141,23 +141,23 @@ void benchmark() { grnxx::String str, str2; grnxx::Time start, end; - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (int i = 0; i < LOOP_COUNT; ++i) { str = grnxx::String("This is an apple."); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "string creation: elapsed [ns] = " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (int i = 0; i < LOOP_COUNT; ++i) { str2 = str; } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "string copy: elapsed [ns] = " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); } int main() { Modified: test/test_string_format.cpp (+29 -29) =================================================================== --- test/test_string_format.cpp 2013-02-26 16:49:04 +0900 (c6d5239) +++ test/test_string_format.cpp 2013-02-27 18:23:36 +0900 (ea4c0b6) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -21,8 +21,8 @@ #include <sstream> #include "logger.hpp" +#include "steady_clock.hpp" #include "string_format.hpp" -#include "time.hpp" void test_align() { grnxx::StringBuilder builder; @@ -114,90 +114,90 @@ void benchmark() { grnxx::Time start, end; - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint32_t i = 0; i < LOOP_COUNT; ++i) { std::snprintf(buf, sizeof(buf), "%d", __LINE__); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "std::snprintf(int): elapsed [ns]: " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint32_t i = 0; i < LOOP_COUNT; ++i) { std::snprintf(buf, sizeof(buf), "%04d", __LINE__); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "std::snprintf(align_right): elapsed [ns]: " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint32_t i = 0; i < LOOP_COUNT; ++i) { std::snprintf(buf, sizeof(buf), "%s:%d: %s: In %s(): %s", __FILE__, __LINE__, "error", __func__, "failed"); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "std::snprintf(complex): elapsed [ns]: " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint32_t i = 0; i < LOOP_COUNT; ++i) { std::stringstream stream; stream.rdbuf()->pubsetbuf(buf, sizeof(buf)); stream << __LINE__; } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "std::ostream(int): elapsed [ns]: " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint32_t i = 0; i < LOOP_COUNT; ++i) { std::stringstream stream; stream.rdbuf()->pubsetbuf(buf, sizeof(buf)); stream << std::setw(4) << std::setfill('0') << __LINE__; } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "std::ostream(align_right): elapsed [ns]: " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint32_t i = 0; i < LOOP_COUNT; ++i) { std::stringstream stream; stream.rdbuf()->pubsetbuf(buf, sizeof(buf)); stream << __FILE__ << ':' << __LINE__ << ": " << "error" << ": In " << __func__ << "(): " << "failed"; } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "std::ostream(complex): elapsed [ns]: " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint32_t i = 0; i < LOOP_COUNT; ++i) { grnxx::StringBuilder(buf).builder() << __LINE__; } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "grnxx::StringBuilder(int): elapsed [ns]: " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint32_t i = 0; i < LOOP_COUNT; ++i) { grnxx::StringBuilder(buf).builder() << grnxx::StringFormat::align_right(__LINE__, 4, '0'); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "grnxx::StringBuilder(align_right): elapsed [ns]: " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (std::uint32_t i = 0; i < LOOP_COUNT; ++i) { grnxx::StringBuilder(buf).builder() << __FILE__ << ':' << __LINE__ << ": " << "error" << ": In " << __func__ << "(): " << "failed"; } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "grnxx::StringBuilder(complex): elapsed [ns]: " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); } int main() { Modified: test/test_thread.cpp (+11 -11) =================================================================== --- test/test_thread.cpp 2013-02-26 16:49:04 +0900 (e899fb2) +++ test/test_thread.cpp 2013-02-27 18:23:36 +0900 (5354fd3) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -19,7 +19,7 @@ #include "logger.hpp" #include "thread.hpp" -#include "time.hpp" +#include "steady_clock.hpp" int main() { grnxx::Logger::set_flags(grnxx::LOGGER_WITH_ALL | @@ -28,30 +28,30 @@ int main() { enum { LOOP_COUNT = 1000 }; - grnxx::Time start = grnxx::Time::now(); + grnxx::Time start = grnxx::SteadyClock::now(); for (int i = 0; i < LOOP_COUNT; ++i) { grnxx::Thread::switch_to_others(); } - grnxx::Time end = grnxx::Time::now(); + grnxx::Time end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "switch_to_others(): elapsed [ns]: " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (int i = 0; i < LOOP_COUNT; ++i) { grnxx::Thread::sleep(grnxx::Duration(0)); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "sleep(0): elapsed [ns]: " - << ((end - start).nanoseconds() / LOOP_COUNT); + << (1000.0 * (end - start).count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); grnxx::Thread::sleep(grnxx::Duration::milliseconds(10)); - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); GRNXX_NOTICE() << "sleep(10ms): elapsed [ns] = " - << (end - start).nanoseconds(); + << (1000.0 * (end - start).count()); return 0; } Modified: test/test_time.cpp (+19 -18) =================================================================== --- test/test_time.cpp 2013-02-26 16:49:04 +0900 (8a5d013) +++ test/test_time.cpp 2013-02-27 18:23:36 +0900 (cfc765e) @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Brazil, Inc. + Copyright (C) 2012-2013 Brazil, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -18,46 +18,47 @@ #include <cassert> #include "logger.hpp" -#include "time.hpp" +#include "steady_clock.hpp" +#include "system_clock.hpp" int main() { grnxx::Logger::set_flags(grnxx::LOGGER_WITH_ALL | grnxx::LOGGER_ENABLE_COUT); grnxx::Logger::set_max_level(grnxx::NOTICE_LOGGER); - assert(grnxx::Time::max().nanoseconds() == + assert(grnxx::Time::max().count() == std::numeric_limits<std::int64_t>::max()); - assert(grnxx::Time::min().nanoseconds() == + assert(grnxx::Time::min().count() == std::numeric_limits<std::int64_t>::min()); - grnxx::Time time = grnxx::Time::now(); - GRNXX_NOTICE() << "grnxx::Time::now: " << time; + grnxx::Time time = grnxx::SystemClock::now(); + GRNXX_NOTICE() << "grnxx::SystemClock::now: " << time; - time = grnxx::Time::now_in_seconds(); - GRNXX_NOTICE() << "grnxx::Time::now_in_seconds: " << time; + time = grnxx::SteadyClock::now(); + GRNXX_NOTICE() << "grnxx::SteadyClock::now: " << time; enum { LOOP_COUNT = 1 << 16 }; - grnxx::Time start = grnxx::Time::now(); + grnxx::Time start = grnxx::SteadyClock::now(); for (int i = 0; i < LOOP_COUNT; ++i) { - grnxx::Time::now(); + grnxx::SystemClock::now(); } - grnxx::Time end = grnxx::Time::now(); + grnxx::Time end = grnxx::SteadyClock::now(); grnxx::Duration elapsed = end - start; - GRNXX_NOTICE() << "grnxx::Time::now: average elapsed [ns] = " - << (elapsed.nanoseconds() / LOOP_COUNT); + GRNXX_NOTICE() << "grnxx::SystemClock::now: average elapsed [ns] = " + << (1000.0 * elapsed.count() / LOOP_COUNT); - start = grnxx::Time::now(); + start = grnxx::SteadyClock::now(); for (int i = 0; i < LOOP_COUNT; ++i) { - grnxx::Time::now_in_seconds(); + grnxx::SteadyClock::now(); } - end = grnxx::Time::now(); + end = grnxx::SteadyClock::now(); elapsed = end - start; - GRNXX_NOTICE() << "grnxx::Time::now_in_seconds" + GRNXX_NOTICE() << "grnxx::SteadyClock::now" << ": average elapsed [ns] = " - << (elapsed.nanoseconds() / LOOP_COUNT); + << (1000.0 * elapsed.count() / LOOP_COUNT); return 0; } -------------- next part -------------- HTML����������������������������... Télécharger