timealgorithms.cxx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. std::ofstream theFile;
  23. theFile.open("timealgorithms.csv");
  24. theFile << "Sample,InsertionSortTime,InsertionSortCompares,InsertionSortMemaccess,MergeSortTime,MergeSortCompares,MergeSortMemaccess,QuickSortTime,QuickSortCompares,QuickSortMemaccess\n";
  25. // initialize arrays for JSON data
  26. // each should have it's own copy cause the array get's sorted within each function
  27. std::vector<int>* insArray = new std::vector<int>();
  28. std::vector<int>* merArray = new std::vector<int>();
  29. std::vector<int>* quickArray = new std::vector<int>();
  30. // initialize insertion sort variables
  31. int *insReturnArray;
  32. time_t insStartTime;
  33. time_t insTime;
  34. // initialize merge sort variables
  35. int *mergeReturnArray;
  36. time_t merStartTime;
  37. time_t merTime;
  38. // initialize quick sort variables
  39. int *quickReturnArray;
  40. time_t quickStartTime;
  41. time_t quickTime;
  42. std::string sampleName; // Sample key name to iterate
  43. for (auto itr = jsonObject.begin(); itr != jsonObject.end(); ++itr) {
  44. sampleName = itr.key();
  45. if (sampleName != "metadata") {
  46. for (auto arrayItr = jsonObject[sampleName].begin(); arrayItr != jsonObject[sampleName].end(); ++arrayItr) {
  47. insArray -> push_back(*arrayItr);
  48. merArray -> push_back(*arrayItr);
  49. quickArray -> push_back(*arrayItr);
  50. }
  51. // Add the sample name to the CSV file line
  52. theFile << itr.key() << ",";
  53. // insertion sort evaluation
  54. insStartTime = time(0);
  55. insReturnArray = InsertionSort(insArray);
  56. insTime = time(0) - insStartTime;
  57. // Add the insertion sort data to the CSV file line
  58. theFile << insTime << "," << *insReturnArray << "," << *(insReturnArray + 1) << "," ;
  59. // merge sort evaluation
  60. merStartTime = time(0);
  61. mergeReturnArray = MergeSort(merArray);
  62. merTime = time(0) - merStartTime;
  63. // Add the merge sort data to the CSV file line
  64. theFile << merTime << "," << *mergeReturnArray << "," << *(mergeReturnArray + 1) << "," ;
  65. // quick sort evaluation
  66. quickStartTime = time(0);
  67. quickReturnArray = QuickSort(quickArray);
  68. quickTime = time(0) - quickStartTime;
  69. // Add the quick sort data to the CSV file line
  70. theFile << quickTime << "," << *quickReturnArray << "," << *(quickReturnArray + 1);
  71. theFile << "\n";
  72. // clear the arrays for the next sample
  73. insArray -> clear();
  74. merArray -> clear();
  75. quickArray -> clear();
  76. }
  77. }
  78. theFile.close(); // close CSV file
  79. file.close(); // close JSON file
  80. return 0;
  81. }