Measuring executing time I've written a small piece of code to measure execution time. As this is my first attempt at something meaningful, I would really like some input on ways to improve my code. 

Timer.h

/**
    Purpose: Measuring execution time using chrono.

    @author Rick Regeling
    @version 1.0 2017/01/05
*/

#ifndef TIMER_H
#define TIMER_H

#include &lt;chrono&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;map&gt;

namespace library {

    // Default unit: std::chrono::milliseconds
    template&lt;typename Unit = std::chrono::milliseconds&gt;
    class Timer {
    public:
        using Clock = std::conditional_t&lt;std::chrono::high_resolution_clock::is_steady,
            std::chrono::high_resolution_clock,
            std::chrono::steady_clock&gt;;

        Timer() = default;
        Timer(const std::string&amp; name) : m_name{ std::move(name) } {}
        ~Timer() {
            m_end = Clock::now();
            output();
        }

        // Disabled operations:
        Timer(const Timer&amp;) = delete;
        Timer(Timer&amp;&amp;) = delete;
        Timer&amp; operator=(const Timer&amp;) = delete;
        Timer&amp; operator=(Timer&amp;&amp;) = delete;
    private:
        /**
            Build a map&lt;const char*, const char*&gt; and find the unit.
            @return The unit name.
        */
        const char* unit_name() {
            const std::map&lt;const char* const, const char*&gt; units{
                { typeid(std::chrono::hours).name(),"h" },
                { typeid(std::chrono::minutes).name(), "m" },
                { typeid(std::chrono::seconds).name(), "s" },
                { typeid(std::chrono::nanoseconds).name(), "ns" },
                { typeid(std::chrono::microseconds).name(), "mms" },
                { typeid(std::chrono::milliseconds).name(), "ms" } };
            auto result = units.find(typeid(Unit).name());
            return result-&gt;second;
        }

        /**
            Output the information.
        */
        void output() {
            std::cout &lt;&lt; "Elapsed time";
            // Determine if a name is used:
            std::cout &lt;&lt; (m_name == "" ? ": " : " (" + m_name + "): ");
            // Calculate the total time and get the unit:
            std::cout &lt;&lt; (std::chrono::duration_cast&lt;Unit&gt;
                (m_end - m_start).count()) &lt;&lt; unit_name() &lt;&lt; '\n';
        }

        // Members:
        const Clock::time_point m_start = Clock::now();
        Clock::time_point m_end{};
        const std::string m_name{};
    };
}
#endif


Example:

#include "Timer.h"
#include &lt;vector&gt;
#include &lt;numeric&gt;

void foo() {
    std::vector&lt;int&gt; v(1'000'000, 42);
    std::accumulate(std::begin(v), std::end(v), 0);
}

int main() {
    // default behavior:
    {
        library::Timer&lt;&gt; timer;
        foo();
    }

    // different unit + description:
    {
        library::Timer&lt;std::chrono::nanoseconds&gt; ex_timer("foo");
        foo();
    }
    return EXIT_SUCCESS;
}
Usage:

Famous Scott Meyers' words: "Make interfaces easy to use correctly and hard to use incorrectly". It should be kept in mind.

It is really cumbersome to use. Users have to create different scopes and they don't have any control over what's being output and where it's being output.

Also, it seems like the class is somewhat one shot, so users can't make multiple runs. Usually people make considerable amount of runs to normalize the numbers and diminish the noise. The interface could emphasize that.

It is needed to keep in mind that the responsibility of this class is to measure the timings. Don't do anything with it, just provide a way to read it.

Alternative solution:

template&lt;typename Func&gt;
class session {
    Func function;
    std::vector&lt;std::chrono::duration&lt;double&gt;&gt; timings;
public:
    session(const Func&amp; f) :
            function(f)
    {}

    session(Func&amp;&amp; f) :
            function(std::move(f)) {}

    template&lt;class ... Args&gt;
    void measure(Args &amp;&amp;... args) {
        auto start = std::chrono::high_resolution_clock::now();
        function(std::forward&lt;Args&gt;(args)...);
        auto end = std::chrono::high_resolution_clock::now();

        timings.push_back(end - start);
    }

    //writes into the range until either exhausts it or timings vector is exhausted
    template&lt;typename Unit, typename OutputIt&gt;
    void get_as(OutputIt first, OutputIt last) {
        auto it = timings.begin();
        for (; it != timings.end() &amp;&amp; first != last;
               ++first, ++it) {
            *first = std::chrono::duration_cast&lt;Unit&gt;(*it);
        }
    }
};


As you can see, single measure() call doesn't return the time, but rather get_as() is used to get all of the measurements at once, and throw it into some function that computes average and generally does processing of the collected data. The interface does little dictating on what users should do, and provides enough flexibility. In C++17, there even won't be a need to write 

session&lt;T&gt; s(t);


it will be just 

session s(t);


If it is so wanted, you can write freestanding measure() function to get timings of one function call.

What is done right:

The code is really generic, which is nice for portability reasons.

Rule of five nailed down.

Bad stuff:

(m_name == "" ? ": " : " (" + m_name + "): ");


This line is like calling for a challenge "Try to understand me!".

 m_end = Clock::now();
 output();


My opinion is that if you don't initialize a member variable during construction, then the member variable is a candidate for local variable and being passed by reference/const reference.

Don't postpone to runtime:

Get the string for units right in the beginning using the fact that units are aliases for std::ratio&lt;&gt;s.