<commit_before>#include <cstdio>
#include <ctime>
#include <uclog/bprintf.hpp>

enum { N = 1024 * 128 };

struct measure_time
{
    measure_time(double* res): res(res)
    {
        clock_gettime(CLOCK_MONOTONIC, &tic);
    }

    ~measure_time()
    {
        timespec toc;
        clock_gettime(CLOCK_MONOTONIC, &toc);
        if (toc.tv_nsec < tic.tv_nsec)
        {
            toc.tv_nsec += 1000ULL * 1000 * 1000;
            toc.tv_sec -= 1;
        }
        double elapsed_msecs = double(1000) * (toc.tv_sec - tic.tv_sec) + (toc.tv_nsec - tic.tv_nsec) / double(1000 * 1000);
        *res = elapsed_msecs;
    }

    timespec tic;
    double* res;
};

struct test_result
{
    const char* name;
    double v1;
    double v2;
};

template <typename TestCase>
test_result test(const char* name)
{
    test_result res;
    res.name = name;
    {
        measure_time _(&res.v1);
        for (int i = 0; i < N; i++)
        {
            TestCase()(&snprintf);
        }
    }
    {
        measure_time _(&res.v2);
        for (int i = 0; i < N; i++)
        {
            TestCase()(&uclog::snbprintf);
        }
    }
    return res;
}

struct test_fmt
{
    template <typename FuncType>
    void operator()(FuncType fun)
    {
        char buf[1024];
        fun(buf, 1024,
            "hisuhdspoaiuhdaouhfaodshapsdgoihsdgouashpfdhfp[wenpimc[ejmiejrcqwejrwepijraoakpsoka"
        );
    }
};

struct test_strings
{
    template <typename FuncType>
    void operator()(FuncType fun)
    {
        char buf[1024];
        fun(buf, 1024, "%s %s %s", "ala", "ma", "kota");
    }
};

struct test_int
{
    template <typename FuncType>
    void operator()(FuncType fun)
    {
        char buf[1024];
        fun(buf, 1024, "%d %d %d %d", 42, 42, 42, 42);
    }
};

struct test_float
{
    template <typename FuncType>
    void operator()(FuncType fun)
    {
        char buf[1024];
        fun(buf, 1024, "%f %le %G %lA", 42., 42., 42., 42.);
    }
};

struct test_many_ints
{
    template <typename FuncType>
    void operator()(FuncType fun)
    {
        char buf[1024];
        fun(buf, 1024,
            "%hhd %hhd %hhd %hhd "
            "%hd %hd %hd %hd "
            "%d %d %d %d "
            "%ld %ld %ld %ld "
            "%lld %lld %lld %lld",
            42, 42, 42, 42,
            42, 42, 42, 42,
            42, 42, 42, 42,
            42L, 42L, 42L, 42L,
            42LL, 42LL, 42LL, 42LL
        );
    }
};

int main()
{
    test_result results[] = {
        test<test_fmt>("test_fmt"),
        test<test_strings>("test_strings"),<commit_msg>function objects used in test (more flexibility)<commit_after>#include <cstdio>
#include <ctime>
#include <uclog/bprintf.hpp>

enum { N = 1024 * 128 };

struct measure_time
{
    measure_time(double* res): res(res)
    {
        clock_gettime(CLOCK_MONOTONIC, &tic);
    }

    ~measure_time()
    {
        timespec toc;
        clock_gettime(CLOCK_MONOTONIC, &toc);
        if (toc.tv_nsec < tic.tv_nsec)
        {
            toc.tv_nsec += 1000ULL * 1000 * 1000;
            toc.tv_sec -= 1;
        }
        double elapsed_msecs = double(1000) * (toc.tv_sec - tic.tv_sec) + (toc.tv_nsec - tic.tv_nsec) / double(1000 * 1000);
        *res = elapsed_msecs;
    }

    timespec tic;
    double* res;
};

struct test_result
{
    const char* name;
    double v1;
    double v2;
};

struct printf_adapter
{
    void operator()(void* buf, size_t size, const char* fmt, ...)
    {
        va_list args;
        va_start(args, fmt);
        vsnprintf(static_cast<char*>(buf), size, fmt, args);
        va_end(args);
    }
};

struct bprintf_adapter
{
    void operator()(void* buf, size_t size, const char* fmt, ...)
    {
        va_list args;
        va_start(args, fmt);
        uclog::vsnbprintf(buf, size, fmt, args);
        va_end(args);
    }
};

template <template <typename> class TestCase>
test_result test(const char* name)
{
    test_result res;
    res.name = name;
    {
        measure_time _(&res.v1);
        for (int i = 0; i < N; i++)
        {
            TestCase<printf_adapter>()();
        }
    }
    {
        measure_time _(&res.v2);
        for (int i = 0; i < N; i++)
        {
            TestCase<bprintf_adapter>()();
        }
    }
    return res;
}

template <typename FuncType>
struct test_fmt
{
    void operator()()
    {
        char buf[1024];
        FuncType()(buf, 1024,
            "hisuhdspoaiuhdaouhfaodshapsdgoihsdgouashpfdhfp[wenpimc[ejmiejrcqwejrwepijraoakpsoka"
        );
    }
};

template <typename FuncType>
struct test_strings
{
    void operator()()
    {
        char buf[1024];
        FuncType()(buf, 1024, "%s %s %s", "ala", "ma", "kota");
    }
};

template <typename FuncType>
struct test_int
{
    void operator()()
    {
        char buf[1024];
        FuncType()(buf, 1024, "%d %d %d %d", 42, 42, 42, 42);
    }
};

template <typename FuncType>
struct test_float
{
    void operator()()
    {
        char buf[1024];
        FuncType()(buf, 1024, "%f %le %G %lA", 42., 42., 42., 42.);
    }
};

template <typename FuncType>
struct test_many_ints
{
    void operator()()
    {
        char buf[1024];
        FuncType()(buf, 1024,
            "%hhd %hhd %hhd %hhd "
            "%hd %hd %hd %hd "
            "%d %d %d %d "
            "%ld %ld %ld %ld "
            "%lld %lld %lld %lld",
            42, 42, 42, 42,
            42, 42, 42, 42,
            42, 42, 42, 42,
            42L, 42L, 42L, 42L,
            42LL, 42LL, 42LL, 42LL
        );
    }
};

int main()
{
    test_result results[] = {
        test<test_fmt>("test_fmt"),
        test<test_strings>("test_strings"),
{'fraction_non_alphanumeric': 0.1445375155943682, 'fraction_numerical': 0.04776332204598111, 'mean_word_length': 1.9229166666666666, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 29, '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': '6495296', 'n_tokens_mistral': 2331, 'n_tokens_neox': 2059, 'n_words': 474}