]> git.lizzy.rs Git - minetest.git/blobdiff - src/util/timetaker.cpp
Clean up serialization
[minetest.git] / src / util / timetaker.cpp
index 42a248dc657571e8d8fecc827be623ed3980faa6..717449c6df51f6bf929573a53d6a097da63f5907 100644 (file)
@@ -1,6 +1,6 @@
 /*
 Minetest
-Copyright (C) 2010-2012 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
@@ -19,32 +19,36 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "timetaker.h"
 
-#include "../gettime.h"
-#include "../log.h"
+#include "porting.h"
+#include "log.h"
 #include <ostream>
 
-TimeTaker::TimeTaker(const char *name, u32 *result)
+TimeTaker::TimeTaker(const std::string &name, u64 *result, TimePrecision prec)
 {
        m_name = name;
        m_result = result;
-       m_running = true;
-       m_time1 = getTimeMs();
+       m_precision = prec;
+       m_time1 = porting::getTime(prec);
 }
 
-u32 TimeTaker::stop(bool quiet)
+u64 TimeTaker::stop(bool quiet)
 {
-       if(m_running)
-       {
-               u32 time2 = getTimeMs();
-               u32 dtime = time2 - m_time1;
-               if(m_result != NULL)
-               {
+       if (m_running) {
+               u64 dtime = porting::getTime(m_precision) - m_time1;
+               if (m_result != nullptr) {
                        (*m_result) += dtime;
-               }
-               else
-               {
-                       if(quiet == false)
-                               infostream<<m_name<<" took "<<dtime<<"ms"<<std::endl;
+               } else {
+                       if (!quiet) {
+                               static const char* const units[] = {
+                                       "s"  /* PRECISION_SECONDS */,
+                                       "ms" /* PRECISION_MILLI */,
+                                       "us" /* PRECISION_MICRO */,
+                                       "ns" /* PRECISION_NANO */,
+                               };
+                               infostream << m_name << " took "
+                                          << dtime << units[m_precision]
+                                          << std::endl;
+                       }
                }
                m_running = false;
                return dtime;
@@ -52,10 +56,8 @@ u32 TimeTaker::stop(bool quiet)
        return 0;
 }
 
-u32 TimeTaker::getTime()
+u64 TimeTaker::getTimerTime()
 {
-       u32 time2 = getTimeMs();
-       u32 dtime = time2 - m_time1;
-       return dtime;
+       return porting::getTime(m_precision) - m_time1;
 }