<commit_before>// g++ playingWithFnPtrs.cpp -o playingWithFnPtrs -std=c++11 -Wall -O3

#include <iostream>
using std::cout;
using std::endl;
#include <chrono>
#include "node_f.hpp"
#include "node_s.hpp"



num _add(num a, num b) {return a + b;}
num _sub(num a, num b) {return a - b;}
num _mul(num a, num b) {return a * b;}
num _div(num a, num b) {return a / b;}

enum switch_op {so_add, so_sub, so_mul, so_div};
num everyFn(switch_op op, num a, num b) {
  switch (op) {
    case so_add: return a+b;
    case so_sub: return a-b;
    case so_mul: return a*b;
    case so_div: return a/b;
  }
  return 0;
}


#define _checkData \
for (uint i = 0; i < dataSize; i++) {\
  if (data[i] != 100) {\
    cout << "incorrect data at " << i << ": " << data[i] << endl;\
    exit(1);\
  }\
  data[i] = 0;\
}


int main(int argc, char const **argv) {
  long dataSize = 1e6;
  num data[dataSize];
  cout << "dataSize: " << dataSize << endl;
  for (uint i = 0; i < dataSize; i++) {data[i] = 0;}
  
  //control
  {
  	auto start_time = std::chrono::high_resolution_clock::now();
    for (uint i = 0; i < dataSize; i++) {
      data[i] = _add(90, _sub(12, _div(8, _mul(2, 2))));
    }
  	auto end_time = std::chrono::high_resolution_clock::now();
    cout << "control: " <<
    std::chrono::duration_cast<std::chrono::microseconds>(
      end_time - start_time
    ).count() << " microseconds" << endl;
  }
  _checkData
  
  //every fn ptr per iteration
  {
    //the conditions are to prevent the compiler from inlining
    const op opAdd = argc > 1000 ? _sub : _add;
    const op opSub = argc > 1000 ? _mul : _sub;
    const op opMul = argc > 1000 ? _div : _mul;
    const op opDiv = argc > 1000 ? _add : _div;
  	auto start_time = std::chrono::high_resolution_clock::now();
    for (uint i = 0; i < dataSize; i++) {
      data[i] = opAdd(90, opSub(12, opDiv(8, opMul(2, 2))));
    }
  	auto end_time = std::chrono::high_resolution_clock::now();
    cout << "every fn ptr per iteration: " <<
    std::chrono::duration_cast<std::chrono::microseconds>(
      end_time - start_time
    ).count() << " microseconds" << endl;
  }
  _checkData
  
  //switch per iteration
  {
    const switch_op a = argc > 1000 ? so_sub : so_add;
    const switch_op s = argc > 1000 ? so_mul : so_sub;
    const switch_op m = argc > 1000 ? so_div : so_mul;
    const switch_op d = argc > 1000 ? so_add : so_div;
  	auto start_time = std::chrono::high_resolution_clock::now();
    for (uint i = 0; i < dataSize; i++) {
      data[i] = everyFn(a, 90, everyFn(s, 12, everyFn(d, 8, everyFn(m, 2, 2))));
    }
  	auto end_time = std::chrono::high_resolution_clock::now();
    cout << "switch per iteration: " <<
    std::chrono::duration_cast<std::chrono::microseconds>(
      end_time - start_time
    ).count() << " microseconds" << endl;
  }
  _checkData
  
  //one fn ptr per iteration
  {
    const op opAdd = argc > 1000 ? _sub : _add;
    const op opSub = argc > 1000 ? _mul : _sub;
    const op opMul = argc > 1000 ? _div : _mul;
    const op opDiv = argc > 1000 ? _add : _div;
  	auto start_time = std::chrono::high_resolution_clock::now();
    for (uint i = 0; i < dataSize; i++) {data[i] = opMul(2, 2);}
    for (uint i = 0; i < dataSize; i++) {data[i] = opDiv(8, data[i]);}
    for (uint i = 0; i < dataSize; i++) {data[i] = opSub(12, data[i]);}
    for (uint i = 0; i < dataSize; i++) {data[i] = opAdd(90, data[i]);}
  	auto end_time = std::chrono::high_resolution_clock::now();
    cout << "one fn ptr per iteration: " <<
    std::chrono::duration_cast<std::chrono::microseconds>(
      end_time - start_time
    ).count() << " microseconds" << endl;
  }
  _checkData
  
  //node_f
  {
    std::vector<node> mulArgs = {node( 2), node(2)};
    node muln = node(nf_mul, mulArgs);
    std::vector<node> divArgs = {node( 8), muln};
    node divn = node(nf_div, divArgs);
    std::vector<node> subArgs = {node(12), divn};
    node subn = node(nf_sub, subArgs);
    std::vector<node> addArgs = {node(90), subn};
    node addn = node(nf_add, addArgs);
    auto start_time = std::chrono::high_resolution_clock::now();
    addn.outputTo(data, dataSize);
  	auto end_time = std::chrono::high_resolution_clock::now();
    cout << "node_f: " <<
    std::chrono::duration_cast<std::chrono::microseconds>(
      end_time - start_time
    ).count() << " microseconds" << endl;
  }
  _checkData
  
  //node_s
  {
    std::vector<node_s> mulArgs = {node_s( 2), node_s(2)};
    node_s muln = node_s(ni_mul, mulArgs);
    std::vector<node_s> divArgs = {node_s( 8), muln};
    node_s divn = node_s(ni_div, divArgs);
    std::vector<node_s> subArgs = {node_s(12), divn};
    node_s subn = node_s(ni_sub, subArgs);
    std::vector<node_s> addArgs = {node_s(90), subn};
    node_s addn = node_s(ni_add, addArgs);
    auto start_time = std::chrono::high_resolution_clock::now();
    addn.outputTo(data, dataSize);
  	auto end_time = std::chrono::high_resolution_clock::now();
    cout << "node_s: " <<
    std::chrono::duration_cast<std::chrono::microseconds>(
      end_time - start_time
    ).count() << " microseconds" << endl;
  }
  _checkData
  
  return 0;
}

<commit_msg>added the index to the function in perfTest<commit_after>// g++ playingWithFnPtrs.cpp -o playingWithFnPtrs -std=c++11 -Wall -O3

#include <iostream>
using std::cout;
using std::endl;
#include <chrono>
#include "node_f.hpp"
#include "node_s.hpp"



num _add(num a, num b) {return a + b;}
num _sub(num a, num b) {return a - b;}
num _mul(num a, num b) {return a * b;}
num _div(num a, num b) {return a / b;}

enum switch_op {so_add, so_sub, so_mul, so_div};
num everyFn(switch_op op, num a, num b) {
  switch (op) {
    case so_add: return a+b;
    case so_sub: return a-b;
    case so_mul: return a*b;
    case so_div: return a/b;
  }
  return 0;
}


#define _checkData \
for (uint i = 0; i < dataSize; i++) {\
  if (data[i] != i) {\
    cout << "incorrect data at " << i << ": " << data[i] << endl;\
    exit(1);\
  }\
  data[i] = -1;\
}


int main(int argc, char const **argv) {
  long dataSize = 1e6;
  num data[dataSize];
  cout << "dataSize: " << dataSize << endl;
  for (uint i = 0; i < dataSize; i++) {data[i] = 0;}
  
  //control
  {
  	auto start_time = std::chrono::high_resolution_clock::now();
    for (uint i = 0; i < dataSize; i++) {
      data[i] = _add(i, _sub(_mul(2, _div(i, 2)), i));
    }
  	auto end_time = std::chrono::high_resolution_clock::now();
    cout << "control: " <<
    std::chrono::duration_cast<std::chrono::microseconds>(
      end_time - start_time
    ).count() << " microseconds" << endl;
  }
  
  //every fn ptr per iteration
  {
    //the conditions are to prevent the compiler from inlining
    const op opAdd = argc > 1000 ? _sub : _add;
    const op opSub = argc > 1000 ? _mul : _sub;
    const op opMul = argc > 1000 ? _div : _mul;
    const op opDiv = argc > 1000 ? _add : _div;
  	auto start_time = std::chrono::high_resolution_clock::now();
    for (uint i = 0; i < dataSize; i++) {
      data[i] = opAdd(i, opSub(opMul(2, opDiv(i, 2)), i));
    }
  	auto end_time = std::chrono::high_resolution_clock::now();
    cout << "every fn ptr per iteration: " <<
    std::chrono::duration_cast<std::chrono::microseconds>(
      end_time - start_time
    ).count() << " microseconds" << endl;
  }
  _checkData
  
  //switch per iteration
  {
    const switch_op a = argc > 1000 ? so_sub : so_add;
    const switch_op s = argc > 1000 ? so_mul : so_sub;
    const switch_op m = argc > 1000 ? so_div : so_mul;
    const switch_op d = argc > 1000 ? so_add : so_div;
  	auto start_time = std::chrono::high_resolution_clock::now();
    for (uint i = 0; i < dataSize; i++) {
      data[i] = everyFn(a, i, everyFn(s, everyFn(m, 2, everyFn(d, i, 2)), i));
    }
  	auto end_time = std::chrono::high_resolution_clock::now();
    cout << "switch per iteration: " <<
    std::chrono::duration_cast<std::chrono::microseconds>(
      end_time - start_time
    ).count() << " microseconds" << endl;
  }
  _checkData
  
  
  //node_f
  {
    std::vector<node> divArgs = {node(nf_globalIndex), node( 2)};
    node divn = node(nf_div, divArgs);
    std::vector<node> mulArgs = {node( 2), divn};
    node muln = node(nf_mul, mulArgs);
    std::vector<node> subArgs = {muln, node(nf_globalIndex)};
    node subn = node(nf_sub, subArgs);
    std::vector<node> addArgs = {node(nf_globalIndex), subn};
    node addn = node(nf_add, addArgs);
    auto start_time = std::chrono::high_resolution_clock::now();
    addn.outputTo(data, dataSize);
  	auto end_time = std::chrono::high_resolution_clock::now();
    cout << "node_f: " <<
    std::chrono::duration_cast<std::chrono::microseconds>(
      end_time - start_time
    ).count() << " microseconds" << endl;
  }
  _checkData
  
  //node_s
  {
    std::vector<node_s> divArgs = {node_s(ni_globalIndex), node_s( 2)};
    node_s divn = node_s(ni_div, divArgs);
    std::vector<node_s> mulArgs = {node_s( 2), divn};
    node_s muln = node_s(ni_mul, mulArgs);
    std::vector<node_s> subArgs = {muln, node_s(ni_globalIndex)};
    node_s subn = node_s(ni_sub, subArgs);
    std::vector<node_s> addArgs = {node_s(ni_globalIndex), subn};
    node_s addn = node_s(ni_add, addArgs);
    auto start_time = std::chrono::high_resolution_clock::now();
    addn.outputTo(data, dataSize);
  	auto end_time = std::chrono::high_resolution_clock::now();
    cout << "node_s: " <<
    std::chrono::duration_cast<std::chrono::microseconds>(
      end_time - start_time
    ).count() << " microseconds" << endl;
  }
  _checkData
  
  return 0;
}


{'fraction_non_alphanumeric': 0.16619362976978871, 'fraction_numerical': 0.0177651634605277, 'mean_word_length': 3.3502514860539554, 'pattern_counts': {'":': 2, '<': 168, '<?xml version=': 0, '>': 54, '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': '7439754', 'n_tokens_mistral': 3799, 'n_tokens_neox': 3612, 'n_words': 908}