timealgorithms.cxx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <ctime>
  7. #include <vector>
  8. #include "json.hpp"
  9. #include "insertionsort.h"
  10. #include "mergesort.h"
  11. #include "quicksort.h"
  12. int main(int argc, char* argv[]) {
  13. // read JSON file and store in jsonObject
  14. std::ifstream file;
  15. file.open(argv[1]);
  16. nlohmann::json jsonObject;
  17. // Store the contents filename into jsonObject
  18. if (file.is_open()) {
  19. file >> jsonObject;
  20. }
  21. // initiate CSV file quickto which data will be written
  22. /*
  23. std::ofstream theFile;
  24. theFile.open("timealgorithms.csv");
  25. theFile << "Sample,InsertionSortTime,InsertionSortCompares,InsertionSortMemaccess,MergeSortTime,MergeSortCompares,MergeSortMemaccess,QuickSortTime,QuickSortCompares,QuickSortMemaccess\n";
  26. */
  27. std::cout << "Sample,InsertionSortTime,InsertionSortCompares,InsertionSortMemaccess,MergeSortTime,MergeSortCompares,MergeSortMemaccess,QuickSortTime,QuickSortCompares,QuickSortMemaccess" << std::endl;
  28. // initialize arrays for JSON data
  29. // each should have it's own copy cause the array get's sorted within each function
  30. std::vector<int>* insArray = new std::vector<int>();
  31. std::vector<int>* merArray = new std::vector<int>();
  32. std::vector<int>* quickArray = new std::vector<int>();
  33. // initialize insertion sort variables
  34. int *insReturnArray;
  35. clock_t insStartTime;
  36. float insTime;
  37. // initialize merge sort variables
  38. int *mergeReturnArray;
  39. clock_t merStartTime;
  40. float merTime;
  41. // initialize quick sort variables
  42. int *quickReturnArray;
  43. clock_t quickStartTime;
  44. float quickTime;
  45. std::string sampleName; // Sample key name to iterate
  46. for (auto itr = jsonObject.begin(); itr != jsonObject.end(); ++itr) {
  47. sampleName = itr.key();
  48. if (sampleName != "metadata") {
  49. for (auto arrayItr = jsonObject[sampleName].begin(); arrayItr != jsonObject[sampleName].end(); ++arrayItr) {
  50. insArray -> push_back(*arrayItr);
  51. merArray -> push_back(*arrayItr);
  52. quickArray -> push_back(*arrayItr);
  53. }
  54. // Add the sample name to the CSV file line
  55. //theFile << itr.key() << ",";
  56. std::cout << itr.key() << ",";
  57. // insertion sort evaluation
  58. insStartTime = clock();
  59. insReturnArray = InsertionSort(insArray);
  60. insStartTime = clock() - insStartTime;
  61. insTime = ((float)insStartTime)/CLOCKS_PER_SEC;
  62. // Add the insertion sort data to the CSV file line
  63. //theFile << insTime << "," << *insReturnArray << "," << *(insReturnArray + 1) << "," ;
  64. std::cout << insTime << "," << *insReturnArray << "," << *(insReturnArray + 1) << ",";
  65. // merge sort evaluation
  66. merStartTime = clock();
  67. mergeReturnArray = MergeSort(merArray);
  68. merStartTime = clock() - merStartTime;
  69. merTime = ((float)merStartTime)/CLOCKS_PER_SEC;
  70. // Add the merge sort data to the CSV file line
  71. //theFile << merTime << "," << *mergeReturnArray << "," << *(mergeReturnArray + 1) << "," ;
  72. std::cout << merTime << "," << *mergeReturnArray << "," << *(mergeReturnArray + 1) << "," ;
  73. // quick sort evaluation
  74. quickStartTime = clock();
  75. quickReturnArray = QuickSort(quickArray);
  76. quickStartTime = clock() - quickStartTime;
  77. quickTime = ((float)quickStartTime)/CLOCKS_PER_SEC;
  78. // Add the quick sort data to the CSV file line
  79. //theFile << quickTime << "," << *quickReturnArray << "," << *(quickReturnArray + 1);
  80. //theFile << "\n";
  81. std::cout << quickTime << "," << *quickReturnArray << "," << *(quickReturnArray + 1) << std::endl;
  82. // clear the arrays for the next sample
  83. insArray -> clear();
  84. merArray -> clear();
  85. quickArray -> clear();
  86. }
  87. }
  88. //theFile.close(); // close CSV file
  89. file.close(); // close JSON file
  90. return 0;
  91. }