// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
#include "high_res_timer.h"

#include <iostream>
#include <iomanip>
#include <ios>
#include <string>
#include <cmath>
#include <cstdlib>

using namespace std;

void printres(TIMECOUNT_T t1, TIMECOUNT_T t2, int n1, int n2, const char *ops)
{
    double xperf = (double)n1 * (double)n2 / ((t2 - t1) * 1.0 / UNITS_PER_SECOND);
    cout << fixed << setprecision(4) << (xperf / 1000000) << " million " << ops << " per second" << endl;
}

const int NINT = 10000;
const int NFP = 1000;
const int NSTR = 100;
const int N = 1000000;

void testint(int scale)
{
    int nintscale = NINT / scale;
    TIMECOUNT_T t1 = GET_TIMECOUNT;
    for(int i = 0; i < nintscale; i++)
    {
        int sum = i;
        for(int j = 0; j < N; j++)
        {
            sum = ((sum + 1) * 2 + 1) / 2;
        }
        if(sum != (i + N))
        {
            cout << "Integer test error" << endl;
            exit(0);
        }
    }
    TIMECOUNT_T t2 = GET_TIMECOUNT;
    printres(t1, t2, nintscale, N, "integer operations");
}

void testfp(int scale)
{
    int nfpscale = NFP / scale;
    TIMECOUNT_T t1 = GET_TIMECOUNT;
    for(int i = 0; i < nfpscale; i++)
    {
        double sum = i;
        for(int j = 0; j < N; j++)
        {
            sum = ((sum + 1) * 2 + 1) / 2;
        }
        if(fabs(sum - (i + 1.5 * N)) > 1)
        {
            cout << "Floating point test error" << endl;
            exit(0);
        }
    }
    TIMECOUNT_T t2 = GET_TIMECOUNT;
    printres(t1, t2, nfpscale, N, "floating point operations");
}

const string ALFA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int VMS_FACTOR = 100;

void teststr(int scale)
{
    int nstrscale = NSTR / scale;
    TIMECOUNT_T t1 = GET_TIMECOUNT;
    for(int i = 0; i < nstrscale * VMS_FACTOR; i++)
    {
        string s, buf;
        buf = "";
        for(int j = 0; j < N / VMS_FACTOR; j = j + 10)
        {
            s = ALFA + ALFA;
            int ix = (i + j) % ALFA.length();
            //buf = buf + s.substr(ix, 1) + s.substr(ix + 1, 2) + s.substr(ix + 3, 3) + s.substr(ix + 6, 4);
            buf.append(s, ix, 1);
            buf.append(s, ix + 1, 2);
            buf.append(s, ix + 3, 3);
            buf.append(s, ix + 6, 4);
        }
        int ix1 = (N / VMS_FACTOR) / 3;
        int ix2 = 2 * (N / VMS_FACTOR) / 3; 
        if(buf.length() != (N / VMS_FACTOR) || buf[ix1] != ALFA[(i + ix1) % ALFA.length()] || buf[ix2] != ALFA[(i + ix2) % ALFA.length()])
        {
            cout << "String test error" << endl;
            exit(0);
        }
    }
    TIMECOUNT_T t2 = GET_TIMECOUNT;
    printres(t1, t2, nstrscale, N / 10, "string operations");
}

#define REP 10

int main(int argc, char *argv[])
{
    int i;
    int scale;
    cout << "C++:" << endl;
    if(argc > 1)
    {
        scale = atoi(argv[1]);
    }
    else
    {
        scale = 1;
    }
    for(i = 0; i < REP; i++)
    {
        testint(scale);
    }
    for(i = 0; i < REP; i++)
    {
        testfp(scale);
    }
    for(i = 0; i < REP; i++)
    {
        teststr(scale);
    }
    return 0;
}
