<commit_before>#pragma once

#include "timer.h"

namespace cortex
{
        ///
        /// \brief measure a function call (in microseconds)
        ///
        template
        <
                typename toperator
        >
        microseconds_t measure_usec(const toperator& op, const std::size_t trials)
        {
                const timer_t timer;

                for (std::size_t i = 0; i < trials; ++ i)
                {
                        op();
                }

                return std::max(microseconds_t(1), timer.microseconds());
        }

        ///
        /// \brief robustly measure a function call (in the given period)
        ///
        template
        <
                typename tperiod,
                typename toperator
        >
        tperiod measure_robustly(const toperator& op, const std::size_t trials)
        {
                const microseconds_t min_usecs(100 * 1000);

                // calibrate the number of function calls to achieve the minimum time resolution
                std::size_t count = trials; 
                microseconds_t usecs(0);
                while (true)
                {
                        usecs = measure_usec(op, count);
                        if (usecs < min_usecs)
                        {
                                count *= 2;
                        }
                        else
                        {
                                break;
                        }
                }

                return (usecs + tperiod(count / 2)) / count;
        }

        ///
        /// \brief robustly measure a function call (in nanoseconds)
        ///
        template
        <
                typename toperator
        >
        nanoseconds_t measure_robustly_nsec(const toperator& op, const std::size_t trials)
        {
                return measure_robustly<nanoseconds_t>(op, trials);
        }
        
        ///
        /// \brief robustly measure a function call (in microseconds)
        ///
        template
        <
                typename toperator
        >
        microseconds_t measure_robustly_usec(const toperator& op, const std::size_t trials)
        {
                return measure_robustly<microseconds_t>(op, trials);
        }
        

        ///
        /// \brief robustly measure a function call (in miliseconds)
        ///
        template
        <
                typename toperator
        >
        milliseconds_t measure_robustly_msec(const toperator& op, const std::size_t trials)
        {
                return milliseconds_t((measure_robustly_usec(op, trials).count() + 500) / 1000);
        }
}
<commit_msg>improve & simplify measurements<commit_after>#pragma once

#include "timer.h"

namespace cortex
{
        ///
        /// \brief measure a function call (in nanoseconds)
        ///
        template
        <
                typename toperator
        >
        nanoseconds_t measure_nsec(const toperator& op, const std::size_t trials)
        {
                const timer_t timer;

                for (std::size_t i = 0; i < trials; ++ i)
                {
                        op();
                }

                return timer.nanoseconds();
        }

        ///
        /// \brief robustly measure a function call (in the given period)
        ///
        template
        <
                typename toperator
        >
        nanoseconds_t measure_robustly(const toperator& op, const std::size_t trials)
        {
                const nanoseconds_t min_nsecs(10 * 1000 * 1000);

                // calibrate the number of function calls to achieve the minimum time resolution
                std::size_t count = std::max(std::size_t(1), trials / 2);
                nanoseconds_t nsecs(0);
                while (nsecs < min_nsecs)
                {
                        count *= 2;                
                        nsecs = measure_nsec(op, count);
                }
                
                return nsecs / count;
        }

        ///
        /// \brief robustly measure a function call (in nanoseconds)
        ///
        template
        <
                typename toperator
        >
        nanoseconds_t measure_robustly_nsec(const toperator& op, const std::size_t trials)
        {
                return measure_robustly(op, trials);
        }
        
        ///
        /// \brief robustly measure a function call (in microseconds)
        ///
        template
        <
                typename toperator
        >
        microseconds_t measure_robustly_usec(const toperator& op, const std::size_t trials)
        {
                return std::chrono::duration_cast<microseconds_t>(measure_robustly(op, trials));
        }        

        ///
        /// \brief robustly measure a function call (in miliseconds)
        ///
        template
        <
                typename toperator
        >
        milliseconds_t measure_robustly_msec(const toperator& op, const std::size_t trials)
        {
                return std::chrono::duration_cast<milliseconds_t>(measure_robustly(op, trials));
        }
}

{'fraction_non_alphanumeric': 0.08245408362641657, 'fraction_numerical': 0.006643220007815553, 'mean_word_length': 1.161739864864865, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4303973', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1298, 'n_words': 318}