-
Notifications
You must be signed in to change notification settings - Fork 0
/
timealgorithms.cxx
75 lines (69 loc) · 1.9 KB
/
timealgorithms.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <fstream>
#include <iostream>
#include <chrono>
#include <ctime>
#include "json.hpp"
#include "insertionsort.h"
#include "quicksort.h"
#include "mergesort.h"
int main(int argc, char** argv) {
double InsertionSortTime;
double MergeSortTime;
double QuickSortTime;
if(argc !=2){
std::cout<<"Usage: timealgorithms file.json"<<std::endl;
return -1;
}
std::ifstream ifs(argv[1]);
nlohmann::json input;
ifs >> input;
std::string sample="Sample";
int numSamples = input["metadata"]["numSamples"];
int arraySize = input["metadata"]["arraySize"];
std::cout<<"sampleName," \
"InsertionSortTime," \
"InsertionSortCompares," \
"InsertionSortMemaccess," \
"MergeSortTime," \
"MergeSortCompares," \
"MergeSortMemaccess," \
"QuickSortTime," \
"QuickSortCompares," \
"QuickSortMemaccess"<<std::endl;
for(int i=1;i<=numSamples;i++){
std::string sampleName=sample + std::to_string(i);
std::vector<int>array;
for(int j=0;j<arraySize;j++){
array.push_back(input[sampleName][j]);
}
InsertionSortTime=0.0;
InsertionSortCompares=0;
InsertionSortMemaccess=0;
MergeSortTime=0.0;
MergeSortCompares=0;
MergeSortMemaccess=0;
QuickSortTime=0.0;
QuickSortCompares=0;
QuickSortMemaccess=0;
clock_t tStart = clock();
InsertionSort(&array);
InsertionSortTime=(((double)(clock() - tStart))/CLOCKS_PER_SEC);
tStart = clock();
MergeSort(&array);
MergeSortTime=(((double)(clock() - tStart))/CLOCKS_PER_SEC);
tStart = clock();
QuickSort(&array);
QuickSortTime=(((double)(clock() - tStart))/CLOCKS_PER_SEC);
std::cout<<sampleName<<"," \
<<InsertionSortTime<<"," \
<<InsertionSortCompares<<"," \
<<InsertionSortMemaccess<<"," \
<<MergeSortTime<<"," \
<<MergeSortCompares<<"," \
<<MergeSortMemaccess<<"," \
<<QuickSortTime<<"," \
<<QuickSortCompares<<"," \
<<QuickSortMemaccess<<std::endl;
}
return 0;
}