<commit_before>/****************************************************************************
** gmio benchmarks
** Copyright Fougue (2 Mar. 2015)
** <EMAIL>
**
** This software provides performance benchmarks for the gmio library
** (https://github.com/fougue/gmio)
**
** This software is governed by the CeCILL-B license under French law and
** abiding by the rules of distribution of free software.  You can  use,
** modify and/ or redistribute the software under the terms of the CeCILL-B
** license as circulated by CEA, CNRS and INRIA at the following URL
** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html".
****************************************************************************/

#include "benchmark_tools.h"

#include <float.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#if WIN32
#  include <windows.h>
#  define BENCHMARK_TIMER_WINDOWS
#else
#  include <time.h>
#  define BENCHMARK_TIMER_LIBC
#endif

/* Benchmark timers */

struct benchmark_timer
{
#ifdef BENCHMARK_TIMER_WINDOWS
    LARGE_INTEGER start_time;
    LARGE_INTEGER frequency;
#elif defined(BENCHMARK_TIMER_LIBC)
    clock_t start_tick;
#endif
};

static void benchmark_timer_start(struct benchmark_timer* timer)
{
#ifdef BENCHMARK_TIMER_WINDOWS
    QueryPerformanceFrequency(&timer->frequency);
    QueryPerformanceCounter(&timer->start_time);
#elif defined(BENCHMARK_TIMER_LIBC)
    timer->start_tick = clock();
#endif
}

static gmio_time_ms_t benchmark_timer_elapsed_ms(const struct benchmark_timer* timer)
{
#ifdef BENCHMARK_TIMER_WINDOWS
    LARGE_INTEGER end_time = {0};
    LARGE_INTEGER elapsed = {0};
    QueryPerformanceCounter(&end_time);
    elapsed.QuadPart = end_time.QuadPart - timer->start_time.QuadPart;

    /*
     We now have the elapsed number of ticks, along with the
     number of ticks-per-second. We use these values
     to convert to the number of elapsed microseconds.
     To guard against loss-of-precision, we convert
     to milliseconds *before* dividing by ticks-per-second.
     */

    elapsed.QuadPart *= 1000;
    elapsed.QuadPart /= timer->frequency.QuadPart;
    return elapsed.QuadPart;
#elif defined(BENCHMARK_TIMER_LIBC)
    const clock_t elapsed_ticks = clock() - timer->start_tick;
    const double elapsed_ms = (elapsed_ticks * 1000) / ((double)CLOCKS_PER_SEC);
    return (gmio_time_ms_t)elapsed_ms;
#endif
}

/* Wraps around formatted printing functions */

/*! Wrapp around sprintf() to be used with gprintf_func_exec_time() */
static void sprintf_wrap(void* cookie, const char* fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    vsprintf((char*)cookie, fmt, args);
    va_end(args);
}

/*! Wrap around printf() to be used with gprintf_func_exec_time() */
static void printf_wrap(void* cookie, const char* fmt, ...)
{
    va_list args;
    GMIO_UNUSED(cookie);
    va_start(args, fmt);
    vprintf(fmt, args);
    va_end(args);
}

/*! Typedef on pointer to printf-wrap functions(eg. sprintf_wrap()) */<commit_msg>Fix typo<commit_after>/****************************************************************************
** gmio benchmarks
** Copyright Fougue (2 Mar. 2015)
** <EMAIL>
**
** This software provides performance benchmarks for the gmio library
** (https://github.com/fougue/gmio)
**
** This software is governed by the CeCILL-B license under French law and
** abiding by the rules of distribution of free software.  You can  use,
** modify and/ or redistribute the software under the terms of the CeCILL-B
** license as circulated by CEA, CNRS and INRIA at the following URL
** "http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html".
****************************************************************************/

#include "benchmark_tools.h"

#include <float.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#if WIN32
#  include <windows.h>
#  define BENCHMARK_TIMER_WINDOWS
#else
#  include <time.h>
#  define BENCHMARK_TIMER_LIBC
#endif

/* Benchmark timers */

struct benchmark_timer
{
#ifdef BENCHMARK_TIMER_WINDOWS
    LARGE_INTEGER start_time;
    LARGE_INTEGER frequency;
#elif defined(BENCHMARK_TIMER_LIBC)
    clock_t start_tick;
#endif
};

static void benchmark_timer_start(struct benchmark_timer* timer)
{
#ifdef BENCHMARK_TIMER_WINDOWS
    QueryPerformanceFrequency(&timer->frequency);
    QueryPerformanceCounter(&timer->start_time);
#elif defined(BENCHMARK_TIMER_LIBC)
    timer->start_tick = clock();
#endif
}

static gmio_time_ms_t benchmark_timer_elapsed_ms(const struct benchmark_timer* timer)
{
#ifdef BENCHMARK_TIMER_WINDOWS
    LARGE_INTEGER end_time = {0};
    LARGE_INTEGER elapsed = {0};
    QueryPerformanceCounter(&end_time);
    elapsed.QuadPart = end_time.QuadPart - timer->start_time.QuadPart;

    /*
     We now have the elapsed number of ticks, along with the
     number of ticks-per-second. We use these values
     to convert to the number of elapsed microseconds.
     To guard against loss-of-precision, we convert
     to milliseconds *before* dividing by ticks-per-second.
     */

    elapsed.QuadPart *= 1000;
    elapsed.QuadPart /= timer->frequency.QuadPart;
    return elapsed.QuadPart;
#elif defined(BENCHMARK_TIMER_LIBC)
    const clock_t elapsed_ticks = clock() - timer->start_tick;
    const double elapsed_ms = (elapsed_ticks * 1000) / ((double)CLOCKS_PER_SEC);
    return (gmio_time_ms_t)elapsed_ms;
#endif
}

/* Wraps around formatted printing functions */

/*! Wrap around sprintf() to be used with gprintf_func_exec_time() */
static void sprintf_wrap(void* cookie, const char* fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    vsprintf((char*)cookie, fmt, args);
    va_end(args);
}

/*! Wrap around printf() to be used with gprintf_func_exec_time() */
static void printf_wrap(void* cookie, const char* fmt, ...)
{
    va_list args;
    GMIO_UNUSED(cookie);
    va_start(args, fmt);
    vprintf(fmt, args);
    va_end(args);
}

/*! Typedef on pointer to printf-wrap functions(eg. sprintf_wrap()) */
{'fraction_non_alphanumeric': 0.15283842794759825, 'fraction_numerical': 0.006046355391333557, 'mean_word_length': 4.555037313432836, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '804025', 'n_tokens_mistral': 2023, 'n_tokens_neox': 1919, 'n_words': 511}