<commit_before>// Copyright (c) 2016 <NAME>
// License: MIT License (for full license see LICENSE)

#ifndef _ZENBENCH_H
#define _ZENBENCH_H

#include <algorithm>
#include <chrono>
#include <vector>
#include <iostream>
#include <iomanip>
#include <sstream>

namespace zenbench
{

using BenchmarkList = std::vector<class Benchmark*>;

class Context
{
public:
    Context(std::chrono::nanoseconds duration) : running(false), useArea(false), duration(duration)
    {}
    
    // Keeps the benchmark running for the duration given in the constructor.
    // Return true as long as there is time left. 
    bool Running()
    {
        if (useArea)
        {
            if (runTime > duration)
            {
                return false;
            }
            iterations++;
            return true;
        }
        if (!running)
        {
            running = true;
            iterations = 1;
            start = std::chrono::high_resolution_clock::now();
            return true;
        }
        auto now = std::chrono::high_resolution_clock::now();
        runTime = now - start;
        if (runTime >= duration)
        {
            return false;
        }
        
        iterations++;
        return true;
    }
    
protected:    
    __int64_t TimePerIteration(__int64_t overhead = 0) const
    {
        auto  perIt = runTime.count()/iterations;
        
        if (!useArea)
        {
            perIt -= overhead;
        }
        
        return perIt;
    }

    __int64_t Iterations() const
    {
        return iterations;
    }
    
    void BeginArea()
    {
        if (!useArea)
        {
            // reset everything set by Running()
            iterations = 1;
            runTime = std::chrono::nanoseconds::zero();  
            useArea = true; 
        }
        start = std::chrono::high_resolution_clock::now();
    }
    
    void EndArea()
    {
        auto now = std::chrono::high_resolution_clock::now();
        runTime += (now - start);
    }

private:
    bool                                             running;
    bool                                             useArea;
    __int64_t                                        iterations;
    std::chrono::high_resolution_clock::time_point   start;
    std::chrono::nanoseconds                         duration;
    std::chrono::nanoseconds                         runTime;
    
    friend class BenchmarkArea;
    friend class Benchmark;
};


class BenchmarkArea
{
public:
    BenchmarkArea(Context& ctxt) : ctxt(ctxt)
    {
        ctxt.BeginArea();
    }
    
    ~BenchmarkArea()<commit_msg>__int64_t -> int64_t<commit_after>// Copyright (c) 2016 <NAME>
// License: MIT License (for full license see LICENSE)

#ifndef _ZENBENCH_H
#define _ZENBENCH_H

#include <algorithm>
#include <chrono>
#include <vector>
#include <iostream>
#include <iomanip>
#include <sstream>

namespace zenbench
{

using BenchmarkList = std::vector<class Benchmark*>;

class Context
{
public:
    Context(std::chrono::nanoseconds duration) : running(false), useArea(false), duration(duration)
    {}
    
    // Keeps the benchmark running for the duration given in the constructor.
    // Return true as long as there is time left. 
    bool Running()
    {
        if (useArea)
        {
            if (runTime > duration)
            {
                return false;
            }
            iterations++;
            return true;
        }
        if (!running)
        {
            running = true;
            iterations = 1;
            start = std::chrono::high_resolution_clock::now();
            return true;
        }
        auto now = std::chrono::high_resolution_clock::now();
        runTime = now - start;
        if (runTime >= duration)
        {
            return false;
        }
        
        iterations++;
        return true;
    }
    
protected:    
    int64_t TimePerIteration(int64_t overhead = 0) const
    {
        auto  perIt = runTime.count()/iterations;
        
        if (!useArea)
        {
            perIt -= overhead;
        }
        
        return perIt;
    }

    int64_t Iterations() const
    {
        return iterations;
    }
    
    void BeginArea()
    {
        if (!useArea)
        {
            // reset everything set by Running()
            iterations = 1;
            runTime = std::chrono::nanoseconds::zero();  
            useArea = true; 
        }
        start = std::chrono::high_resolution_clock::now();
    }
    
    void EndArea()
    {
        auto now = std::chrono::high_resolution_clock::now();
        runTime += (now - start);
    }

private:
    bool                                             running;
    bool                                             useArea;
    int64_t                                        iterations;
    std::chrono::high_resolution_clock::time_point   start;
    std::chrono::nanoseconds                         duration;
    std::chrono::nanoseconds                         runTime;
    
    friend class BenchmarkArea;
    friend class Benchmark;
};


class BenchmarkArea
{
public:
    BenchmarkArea(Context& ctxt) : ctxt(ctxt)
    {
        ctxt.BeginArea();
    }
    
    ~BenchmarkArea()
{'fraction_non_alphanumeric': 0.09832272990167727, 'fraction_numerical': 0.006554848660111818, 'mean_word_length': 1.533203125, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4848595', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1446, 'n_words': 342}