#include "spreadsort.hpp" #include #include #include #include #include /*struct DATATYPE { int a, b, c, d, e, f, w, x, y, z; inline long operator>>(const unsigned offset) const { return a >> offset; } inline bool operator<(const DATATYPE & rhs) const{ return a < rhs.a; } };*/ #define DATATYPE float struct lessthan { bool operator()(const DATATYPE &x, const DATATYPE &y) const { return x < y; } }; struct rightshift { int operator()(const DATATYPE &x, const unsigned offset) const { return *((int *)&x) >> offset; } }; //Pass in an argument to test std::sort int main(int argc, const char ** argv) { FILE * pInputFile,*pOutputFile; size_t uFileLength=0,uCount,uSize=sizeof(DATATYPE); void * pcData; bool stdSort = false; unsigned loopCount = 1; for(int u = 1; u < argc; ++u) { if(!strcmp(argv[u], "-std")) stdSort = true; else loopCount = atoi(argv[u]); } pInputFile=fopen("input.txt","r"); if(!pInputFile) { printf("c:\\input.txt could not be opened\n"); return 1; } double total = 0.0; std::vector array; //Run multiple loops, if requested for(unsigned u = 0; u < loopCount; ++u) { fseek(pInputFile,0,SEEK_END); uFileLength=ftell(pInputFile); fseek(pInputFile,0,SEEK_SET); pcData=malloc(uFileLength); fread(pcData,uSize,uCount=uFileLength/uSize,pInputFile); //Conversion to a vector array.reserve(uCount); for(unsigned v = 0; v < uCount; ++v) array.push_back(((DATATYPE *)pcData)[v]); free(pcData); clock_t start, end; double elapsed; start = clock(); if(stdSort) //std::sort(&(array[0]), &(array[0]) + uCount); std::sort(array.begin(), array.end()); else //integer_sort(&(array[0]), &(array[0]) + uCount); float_sort(array.begin(), array.end(), rightshift()); end = clock(); elapsed = ((double) (end - start)) ; if(stdSort) pOutputFile=fopen("StandardSortOut.txt","w"); else pOutputFile=fopen("SpreadSortOut.txt","w"); if(pOutputFile) { /*for(unsigned v = 0; v < uCount; ++v) printf("%f\n", array[v]);*/ fwrite(&(array[0]),uSize,uCount,pOutputFile); fclose(pOutputFile); } total += elapsed; array.clear(); } if(stdSort) printf("std::sort elapsed time %f\n", total / CLOCKS_PER_SEC); else printf("spreadsort elapsed time %f\n", total / CLOCKS_PER_SEC); return 0; }