Skip to content

Commit

Permalink
Update two testing files to use common_functions.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
fenilgmehta committed Aug 11, 2019
1 parent 3236f58 commit 79c9984
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 134 deletions.
125 changes: 22 additions & 103 deletions test/project_SortingBest_integer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,27 @@
#pragma ide diagnostic ignored "OCSimplifyInspection"
#pragma ide diagnostic ignored "cppcoreguidelines-pro-type-member-init"

#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>

#include "./common_functions.hpp"

#include <boost/sort/sort.hpp>
#include <boost/sort/common/file_vector.hpp>

#include "../src/integer_sort.hpp"
#include "../src/integer_sort_objects_small.hpp"

#include "../references/kxsort.h"
#include "../references/ska_sort.hpp"

#include <unistd.h>

// #include <iostream>
// #include <cstdint>

// #include <iomanip>
// #include <algorithm>
// #include <chrono>
// #include <cstring>
// #include <array>
// #include <vector>

// #include <type_traits>


//#################################################
using namespace std;
using namespace std::chrono;

//#################################################
#define endl '\n'
#define db1(x) cerr << "\033[1;31m" << "\nDebug: " << "\033[0m" << setw(30) << left << #x << " = " << (x);
#define db2(x, y) cerr << "\033[1;31m" << "\nDebug: " << "\033[0m" << #x << " = " << (x) << ", " << #y << " = " << (y);
#define db3(x, y, z) cerr << "\033[1;31m" << "\nDebug: " << "\033[0m" << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z << " = " << (z);
#define dblimit(arr, l, h) cerr << "\033[1;31m" << "Debug: " << "\033[0m" << #arr << " [" << (l) << " : " << (h) << "] = "; for(int64_t i = (l); i <= (h); i++) cerr << (arr)[i] << ", "; cerr << endl;
#define dbiter(name, first, len) cerr << "\033[1;31m" << "Debug: " << "\033[0m" << name << " = "; for(auto i_temp1 = 0, len_temp1 = (len), iter_temp1 = first; i_temp1 < len_temp1; ++i_temp1) cerr<<iter_temp1[i_temp1]<<", "; cerr<<endl;

//#################################################
#define rangeup(_i, _startLimit, _endLimit) for(int64_t (_i) = (_startLimit); (_i) < (_endLimit); (_i)++)
const int32_t columnWidth = 15;


Expand Down Expand Up @@ -105,76 +89,6 @@ inline bool f2_ascendingOrder(const ArrayDataType &a, const ArrayDataType &b){ r
inline bool f3_descendingOrder(const ArrayDataType &a, const ArrayDataType &b){ return a > b; }
inline bool f4_notEquals(const ArrayDataType &a, const ArrayDataType &b) { return a != b; }

//#########################################################################################################################################
//#########################################################################################################################################
//#########################################################################################################################################
template<typename T>
int64_t maxIndex(const T &arr, const int64_t &size) {
int64_t index = 0;
for(int64_t i=1; i<size; i++) if (arr[i] > arr[index]) index = i;
return index;
}

template<typename T>
int64_t minIndex(const T &arr, const int64_t &size) {
int64_t index = 0;
for(int64_t i=1; i<size; i++) if (arr[i] < arr[index]) index = i;
return index;
}

template<typename RandomAccessIterator>
bool isSorted(RandomAccessIterator first, const RandomAccessIterator last, const bool &checkAscendingOrder = settings_SORT_IN_ASCENDING_ORDER) {
first++;
if (checkAscendingOrder) {
while(first != last){
if(*next(first, -1) > *first) return false;
first++;
}
} else {
while(first != last){
if(*next(first, -1) < *first) return false;
first++;
}
}
return true;
}

template<typename RandomAccessIterator>
bool compareArray(RandomAccessIterator first, RandomAccessIterator last, RandomAccessIterator second){
RandomAccessIterator it_first = first, it_second = second;
int index = 0;

while(it_first != last){
if((*it_first)!=(*it_second)){
cout << "\n\nERROR: arrays not equal";
db3(index, *it_first, *it_second)
// dbiter("arr1[]", first, distance(first, last))
// dbiter("arr2[]", second, distance(first, last))
return false;
}
++index;
++it_first;
++it_second;
}
return true;
}

template<typename T>
void printArray(T &arr, int64_t low, int64_t high) {
cout << endl;
for (int64_t i = low; i <= high; i++) cout << arr[i] << ", ";
cout << endl;
}

template<typename RandomAccessIterator>
void fillRandArray(RandomAccessIterator first, RandomAccessIterator last, const ArrayDataType randStart, const ArrayDataType randEnd) {
std::random_device rd;
std::mt19937_64 gen(rd());
std::uniform_int_distribution<ArrayDataType> dis(randStart, randEnd);

for(; first != last; ++first) (*first) = dis(gen);
}

//#########################################################################################################################################
//#########################################################################################################################################
//#########################################################################################################################################
Expand Down Expand Up @@ -229,11 +143,14 @@ int32_t main() {
// baseArray: used to store the random array generated for each test-case
vector<ArrayDataType> arr(static_cast<unsigned long>(maxArrayLength));
vector<ArrayDataType> baseArray(static_cast<unsigned long>(maxArrayLength));
// forward_list<int> fl1;
// ir_sort::integer_sort_stable(begin(fl1), end(fl1));
// std::sort(begin(fl1), end(fl1));

for(int64_t arrayLength=minArrayLength; arrayLength<=maxArrayLength; arrayLength++){
cout << "#####################################################################";
db1(arrayLength)
db1(testCases)
db(arrayLength);
db(testCases);

sort1Ratio = 0, sort2Ratio = 0;

Expand All @@ -248,7 +165,7 @@ int32_t main() {
<< setw(setwLenRatio) << "ratio sort1" << "\t," << setw(setwLenRatio) << "ratio sort2";
}

rangeup(__, 0, testCases) {
urange(__, 0, testCases) {
// index to insert data in timeArr
int64_t timeArrIndex = 0;
int64_t myTempLow = 0, myTempHigh = arrayLength - 1;
Expand All @@ -262,15 +179,15 @@ int32_t main() {
// else
// fillRandArray(m_ALL(baseArray), (-(1LL << myBits)) + 1, -1);

fillRandArray(m_ALL(baseArray), 0, static_cast<const ArrayDataType>((((1uLL << (myBits - 1)) - 1) << 1) + 1));
fillRandArray(m_ALL(baseArray), static_cast<const ArrayDataType>(0), static_cast<const ArrayDataType>((((1uLL << (myBits - 1)) - 1) << 1) + 1));

sort(m_ALL(baseArray));
sort(m_ALL(arr));
// baseArray[baseArray.size() / 2] = -10000000;
}

m_START_TIME
ir_sort::integer_sort_stable(m_ALL(arr), 0);
ir_sort::integer_sort_stable(m_ALL(arr));
// ir_sort::integer_sort(m_ALL(arr), true);
// sort(m_ALL(arr));
// ska_sort(m_ALL(arr));
Expand All @@ -283,11 +200,13 @@ int32_t main() {
// delete[] buffer;
m_END_TIME
timeArr[timeArrIndex++] = duration.count();
if (!isSorted(m_ALL(arr))) cerr << endl << "ERROR: array1 not sorted :(";
if (!is_sorted(m_ALL(arr))) cerr << endl << "ERROR: array1 not sorted :(";
// printArray(arr, myTempLow, myTempHigh);

m_START_TIME_ONLY
fm_sort::fm_sort(m_ALL(baseArray));
ir_sort::integer_sort_small_obj(m_ALL(arr), [](auto &objectNum) { return objectNum; });
// fm_sort::fm_sort(m_ALL(baseArray));

/* ArrayDataType mid_element_arr[5] = {baseArray[0], baseArray[maxArrayLength >> 2], baseArray[maxArrayLength>>1], baseArray[maxArrayLength - (maxArrayLength>>2)], baseArray[maxArrayLength-1]};
ir_sort::basic_sorts::insertion_sort_basic_asc<decltype(begin(baseArray)),ArrayDataType>(begin(baseArray), 0, 4);
ArrayDataType mid_element = mid_element_arr[2];
Expand All @@ -305,7 +224,7 @@ int32_t main() {
// kx::radix_sort(m_ALL(baseArray)); // GREAT from 90 to 3000 // this is NOT stable sorting
m_END_TIME
timeArr[timeArrIndex] = duration.count();
if (!isSorted(m_ALL(baseArray))) cerr << endl << "ERROR: array2 not sorted :(";
if (!is_sorted(m_ALL(baseArray))) cerr << endl << "ERROR: array2 not sorted :(";
// printArray(arr, myTempLow, myTempHigh);

if(settings_PRINT_TIME_COMPARISON){
Expand All @@ -316,7 +235,7 @@ int32_t main() {
sort1Ratio += (timeArr[0] / timeArr[0]);
sort2Ratio += (((1.0 * timeArr[1]) / timeArr[0]));

if (!compareArray(begin(arr)+myTempLow, begin(arr)+myTempHigh+1, begin(baseArray))) { db1(__) };
if (!compareArray(begin(arr)+myTempLow, begin(arr)+myTempHigh+1, begin(baseArray))) { db(__); };

//#################################################
bestThreshold[minIndex(timeArr, timeArrLength)]++;
Expand All @@ -325,7 +244,7 @@ int32_t main() {
cout << endl;
dblimit(bestThreshold,0,timeArrLength-1); // This will print the number of times ir_sort was fast and number of times std::sort was fast
int64_t maxSpeedIndex = maxIndex(bestThreshold, timeArrLength);
db1(bestThreshold[maxSpeedIndex]) // Number of times ir_sort was faster than std::sort out of total testCases or vice-versa
db(bestThreshold[maxSpeedIndex]); // Number of times ir_sort was faster than std::sort out of total testCases or vice-versa

if (maxSpeedIndex == 0) cout << "\n\nsort1 is faster than sort2 ";
else cout << "\n\nsort2 is faster than sort1";
Expand Down
60 changes: 29 additions & 31 deletions test/project_SortingBest_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#pragma ide diagnostic ignored "OCSimplifyInspection"
#pragma ide diagnostic ignored "cppcoreguidelines-pro-type-member-init"

#include <bits/stdc++.h>
// #include <bits/stdc++.h>

// #include <iostream>
#include <iostream>
// #include <cstdint>

// #include <iomanip>
Expand All @@ -21,8 +21,12 @@

// #include <type_traits>

#include "common_functions.hpp"

#include <boost/sort/sort.hpp>
#include <boost/sort/spreadsort/float_sort.hpp>
#include <tim/timsort.h>
#include <iomanip>

#include "../src/integer_sort.hpp"
#include "../references/ska_sort.hpp"
Expand All @@ -32,16 +36,6 @@ using namespace std;
using namespace std::chrono;

//#################################################
#define endl '\n'
#define db1(x) cout << "\nDebug: " << setw(30) << left << #x << " = " << (x);
#define db2(x, y) cout << "\nDebug: " << #x << " = " << (x) << ", " << #y << " = " << (y);
#define db3(x, y, z) cout << "\nDebug: " << #x << " = " << (x) << ", " << #y << " = " << (y) << ", " << #z << " = " << (z);
#define dblimit(arr, l, h) cerr << "\033[1;31m" << "Debug: " << "\033[0m" << #arr << " [" << (l) << " : " << (h) << "] = "; for(int64_t i = (l); i <= (h); i++) cerr << (arr)[i] << ", "; cerr << endl;
#define dbiter(name, first, len) cerr << "\033[1;31m" << "Debug: " << "\033[0m" << name << " = "; for(auto i_temp1 = 0, len_temp1 = (len), iter_temp1 = first; i_temp1 < len_temp1; ++i_temp1) cerr<<iter_temp1[i_temp1]<<", "; cerr<<endl;
#define dbsize(arr, size) cout << "\nDebug: " << #arr << " [" << 0 << " : " << ((size)-1) << "] = "; for(int64_t i = 0; i < (size); i++) cout << (arr)[i] << ", ";

//#################################################
#define rangeup(_i, _startLimit, _endLimit) for(int64_t (_i) = (_startLimit); (_i) < (_endLimit); (_i)++)
const int32_t columnWidth = 15;

class myClassInt;
Expand Down Expand Up @@ -94,7 +88,7 @@ using ArrayIndexType = int64_t;
* global_onlyPositiveNumbers = 2; // -ve numbers
*
* */
#define settings_onlyPositiveNumbers 0
#define settings_onlyPositiveNumbers 1

/*
* Number of bits to be used
Expand Down Expand Up @@ -163,9 +157,9 @@ bool compareArray(RandomAccessIterator first, RandomAccessIterator last, RandomA
while(it_first != last){
if((*it_first)!=(*it_second)){
cout << "\n\nERROR: arrays not equal";
db3(index, *it_first, *it_second)
// dbiter("arr1[]", first, distance(first, last));
// dbiter("arr2[]", second, distance(first, last));
db(index, *it_first, *it_second);
// dbiter_n("arr1[]", first, distance(first, last));
// dbiter_n("arr2[]", second, distance(first, last));
return false;
}
++index;
Expand All @@ -181,14 +175,14 @@ bool compareArray(RandomAccessIterator first, RandomAccessIterator last, RandomA
template<typename T>
int64_t maxIndex(const T &arr, const int64_t &size) {
int64_t index = 0;
rangeup(i, 1, size) if (arr[i] > arr[index]) index = i;
urange(i, 1, size) if (arr[i] > arr[index]) index = i;
return index;
}

template<typename T>
int64_t minIndex(const T &arr, const int64_t &size) {
int64_t index = 0;
rangeup(i, 1, size) if (arr[i] < arr[index]) index = i;
urange(i, 1, size) if (arr[i] < arr[index]) index = i;
return index;
}

Expand Down Expand Up @@ -280,10 +274,10 @@ int32_t main() {
vector<ArrayDataType> arr(static_cast<unsigned long>(maxArrayLength));
vector<ArrayDataType> baseArray(static_cast<unsigned long>(maxArrayLength));

rangeup(arrayLength, minArrayLength, maxArrayLength + 1) {
urange(arrayLength, minArrayLength, maxArrayLength + 1) {
cout << "#####################################################################";
db1(arrayLength)
db1(testCases)
db(arrayLength);
db(testCases);

irSortRatio = 0, stdSortRatio = 0;

Expand All @@ -297,7 +291,7 @@ int32_t main() {
<< setw(setwLenRatio) << "ratio ir_sort" << "\t," << setw(setwLenRatio) << "ratio std::sort";
}

rangeup(__, 0, testCases) {
urange(__, 0, testCases) {
int64_t myTempLow = 0, myTempHigh = arrayLength - 1;

// Fill up the array
Expand All @@ -314,15 +308,15 @@ int32_t main() {
int64_t timeArrIndex = 0;

startTime
boost::sort::spreadsort::integer_sort(arr.begin(), arr.end(), [](const myClassInt& a, const int& val){return a.classGetIndex() >> val;});
// boost::sort::spreadsort::integer_sort(arr.begin(), arr.end(), [](const myClassInt& a, const int& val){return a.classGetIndex() >> val;});
// ska_sort(begin(arr), end(arr), [](myClassInt &a){return a.classGetIndex();});
// boost::sort::spreadsort::integer_sort(begin(arr), end(arr), [](const ArrayDataType& a, const int& val){return a.classGetIndex() >> val;});
// std::sort(begin(arr), end(arr), [](ArrayDataType &a , ArrayDataType &b){return a.classGetIndex() < b.classGetIndex();});
// boost::sort::pdqsort(begin(arr), end(arr), [](ArrayDataType &a, ArrayDataType &b){ return a.classGetIndex() < b.classGetIndex();});
// boost::sort::spinsort(begin(arr), end(arr), [](const ArrayDataType &a, const ArrayDataType &b){ return a.classGetIndex() < b.classGetIndex();});
// boost::sort::flat_stable_sort(begin(arr), end(arr), [](const ArrayDataType &a, const ArrayDataType &b){ return a.classGetIndex() < b.classGetIndex();});

// ir_sort::radixSort_asc<ArrayDataType>(arr, myTempLow, myTempHigh, myTempHigh - myTempLow + 1, f1_getIndex);
ir_sort::radixSort_Positive_asc_8<ArrayDataType>(arr, myTempLow, myTempHigh, myTempHigh - myTempLow + 1, f1_getIndex);
// integer_sort_small_obj(&arr[0] + myTempLow, &arr[0] + myTempHigh + 1, f1_getIndex, true);
// integer_sort_small_obj(&arr[0] + myTempLow, &arr[0] + myTempHigh + 1, [](ArrayDataType &a) { return a.classGetIndex(); }, true); // BEST way to sort
// radixSort_Positive_asc<ArrayDataType>(arr, myTempLow, myTempHigh, myTempHigh - myTempLow + 1, [](ArrayDataType &a) { return a.classGetIndex(); });
Expand All @@ -333,19 +327,23 @@ int32_t main() {
// objPrintArray(arr, myTempLow, myTempHigh);

startTimeOnly
using IndexType = int_fast64_t;
// ir_sort::radixSort_asc<ArrayDataType>(baseArray, myTempLow, myTempHigh, myTempHigh - myTempLow + 1, f1_getIndex);
ir_sort::radixSort_Positive_asc_16<ArrayDataType>(baseArray, myTempLow, myTempHigh, myTempHigh - myTempLow + 1, f1_getIndex);

/* using IndexType = int_fast64_t;
auto arr_first = begin(baseArray);
vector<IndexType > p_arr_index(static_cast<unsigned long>(maxArrayLength));
iota(begin(p_arr_index), end(p_arr_index), 0);
iota(begin(p_arr_index), end(p_arr_index), 0);*/

// ska_sort(begin(p_arr_index), end(p_arr_index), [arr_first](IndexType &a){return arr_first[a].classGetIndex();}); // :( not able to optimise
// boost::sort::spreadsort::integer_sort(begin(p_arr_index), end(p_arr_index), [arr_first](const IndexType a, const int val){return arr_first[a].classGetIndex() >> val;}); // :( not sorting
sort(begin(p_arr_index), end(p_arr_index), [arr_first](IndexType &a, IndexType &b){return arr_first[a].classGetIndex() < arr_first[b].classGetIndex();}); // GOOD, good results
// sort(begin(p_arr_index), end(p_arr_index), [arr_first](IndexType &a, IndexType &b){return arr_first[a].classGetIndex() < arr_first[b].classGetIndex();}); // GOOD, good results
// boost::sort::pdqsort(begin(p_arr_index), end(p_arr_index), [arr_first](IndexType &a, IndexType &b){ return arr_first[a].classGetIndex() < arr_first[b].classGetIndex();}); // GOOD, decent results
// boost::sort::spinsort(begin(p_arr_index), end(p_arr_index), [arr_first](const IndexType &a, const IndexType &b){return arr_first[a].classGetIndex() < arr_first[b].classGetIndex();}); // GOOD, excellent results
// boost::sort::flat_stable_sort(begin(p_arr_index), end(p_arr_index), [arr_first](const IndexType &a, const IndexType &b){ return arr_first[a].classGetIndex() < arr_first[b].classGetIndex();}); // GOOD, for all ranges, excellent results
/* tim::timsort(p_arr_index.begin(), p_arr_index.end(), [arr_first](const IndexType &a, const IndexType &b) { return arr_first[a].classGetIndex() < arr_first[b].classGetIndex(); });
fm_sort::fm_sort_objects(begin(baseArray), end(baseArray), begin(p_arr_index));
fm_sort::fm_sort_objects(begin(baseArray), end(baseArray), begin(p_arr_index));*/

// ir_sort::radixSort_Positive_asc<ArrayDataType>(baseArray, myTempLow, myTempHigh, myTempHigh - myTempLow + 1, f1_getIndex);
// ska_sort(begin(baseArray), end(baseArray), [](myClassInt &a){return a.classGetIndex();});
Expand All @@ -372,16 +370,16 @@ int32_t main() {
irSortRatio += (timeArr[0] / timeArr[0]);
stdSortRatio += (((1.0 * timeArr[1]) / timeArr[0]));

if (!compareArray(begin(arr)+myTempLow, begin(arr)+myTempHigh+1, begin(baseArray))) { db1(__) };
if (!compareArray(begin(arr)+myTempLow, begin(arr)+myTempHigh+1, begin(baseArray))) { db(__); };

//#################################################
bestThreshold[minIndex(timeArr, timeArrLength)]++;
}

cout << endl;
dbsize(bestThreshold, timeArrLength); // This will print the number of times ir_sort was fast and number of times std::sort was fast
dblimit(bestThreshold, 0, timeArrLength-1); // This will print the number of times ir_sort was fast and number of times std::sort was fast
int64_t maxSpeedIndex = maxIndex(bestThreshold, timeArrLength);
db1(bestThreshold[maxSpeedIndex]) // Number of times ir_sort was faster than std::sort out of total testCases or vice-versa
db(bestThreshold[maxSpeedIndex]); // Number of times ir_sort was faster than std::sort out of total testCases or vice-versa

if (maxSpeedIndex == 0) cout << "\n\nir_sort is faster than std::sort ";
else cout << "\n\nstd::sort is faster than ir_sort ";
Expand Down

0 comments on commit 79c9984

Please sign in to comment.