From b6b447ca9a846373b30247a32e4e2242b411b82b Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Sun, 16 Dec 2012 19:22:10 +0700 Subject: [PATCH] Print seconds to 1 decimal place --- src/taoensso/timbre/profiling.clj | 13 +++++++------ src/taoensso/timbre/utils.clj | 13 ++++++++++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/taoensso/timbre/profiling.clj b/src/taoensso/timbre/profiling.clj index ea078f5..af1d838 100644 --- a/src/taoensso/timbre/profiling.clj +++ b/src/taoensso/timbre/profiling.clj @@ -1,7 +1,8 @@ (ns taoensso.timbre.profiling "Simple all-Clojure profiling adapted from clojure.contrib.profile." {:author "Peter Taoussanis"} - (:require [taoensso.timbre :as timbre])) + (:require [taoensso.timbre :as timbre] + [taoensso.timbre.utils :as utils])) (def ^:dynamic *plog* "{::pname [time1 time2 ...] ...}" nil) @@ -67,11 +68,11 @@ ft (fn [nanosecs] (let [pow #(Math/pow 10 %) ok-pow? #(>= nanosecs (pow %)) - to-pow #(long (/ nanosecs (pow %)))] - (cond (ok-pow? 9) (str (to-pow 9) "s") - (ok-pow? 6) (str (to-pow 6) "ms") - (ok-pow? 3) (str (to-pow 3) "μs") - :else (str (long nanosecs) "ns"))))] + to-pow #(utils/round-to (/ nanosecs (pow %1)) %2)] + (cond (ok-pow? 9) (str (to-pow 9 1) "s") + (ok-pow? 6) (str (to-pow 6 0) "ms") + (ok-pow? 3) (str (to-pow 3 0) "μs") + :else (str nanosecs "ns"))))] (with-out-str (printf s-pattern "Name" "Calls" "Min" "Max" "MAD" "Mean" "Time%" "Time") diff --git a/src/taoensso/timbre/utils.clj b/src/taoensso/timbre/utils.clj index abef478..61f97e0 100644 --- a/src/taoensso/timbre/utils.clj +++ b/src/taoensso/timbre/utils.clj @@ -34,4 +34,15 @@ (defn deep-merge "Partial of `deep-merge-with`." [& maps] - (apply deep-merge-with (fn [x y] y) maps)) \ No newline at end of file + (apply deep-merge-with (fn [x y] y) maps)) + +(defn round-to + "Rounds argument to given number of decimal places." + [x places] + (if (zero? places) + (Math/round (double x)) + (let [modifier (Math/pow 10.0 places)] + (/ (Math/round (* x modifier)) modifier)))) + +(comment (round-to 10 0) + (round-to 10.123 2)) \ No newline at end of file