6 #define rdtscll(val) __asm__ __volatile__ ("rdtsc" : "=A" (val))
8 const int SELECTMODE
= 14;
9 const int BIGDATA
= 10000; // Relying on int = long
13 void SelectionSort(int data
[], int left
, int right
) {
15 for(i
= left
; i
< right
; i
++) {
17 for(j
=i
+1; j
<= right
; j
++)
18 if(data
[j
] < data
[min
]) min
= j
;
25 int Partition( int d
[], int left
, int right
)
49 void Quicksort( int d
[], int left
, int right
)
51 if(left
< (right
-SELECTMODE
)) {
52 int split_pt
= Partition(d
,left
, right
);
53 Quicksort(d
, left
, split_pt
);
54 Quicksort(d
, split_pt
+1, right
);
56 else SelectionSort(d
, left
, right
);
59 int main(int argc
, char* argv
[]) {
61 data
= (int*)calloc(BIGDATA
,4);
62 unsigned long int timeStart
;
64 unsigned long int timeReadLoopStart
;
65 unsigned long int timeReadLoopEnd
;
67 unsigned long int timeSortLoopStart
;
68 unsigned long int timeSortLoopEnd
;
70 unsigned long int timeWriteLoopStart
;
71 unsigned long int timeWriteLoopEnd
;
73 unsigned long int timeEnd
;
77 int i
,j
,thisInt
,dataSize
= 0;
82 randfile
= fopen(argv
[1],"r");
83 sortfile
= fopen(argv
[2],"w");
84 if (randfile
== NULL
|| sortfile
== NULL
) {
85 fprintf(stderr
,"Could not open all files.\n");
89 rdtscll(timeReadLoopStart
);
92 while (!feof(randfile
)) {
93 fscanf(randfile
,"%d",&thisInt
);
94 if (feof(randfile
)) { break; }
96 sumUnsorted
+= thisInt
;
97 //fprintf(stdout,"[%d] Read item: %d\n",i,thisInt);
106 rdtscll(timeReadLoopEnd
);
107 rdtscll(timeSortLoopStart
);
109 Quicksort(data
, 0, dataSize
-1);
111 rdtscll(timeSortLoopEnd
);
112 rdtscll(timeWriteLoopStart
);
115 for(j
= 0; j
< dataSize
; j
++) {
116 if (data
[j
] < last
) {
117 fprintf(stderr
,"The data is not in order\n");
118 fprintf(stderr
,"Noticed the problem at j = %d\n",j
);
122 fprintf(sortfile
,"%d\n",data
[j
]);
127 rdtscll(timeWriteLoopEnd
);
131 fprintf(stdout
,"Sorted %d items.\n",dataSize
);
132 fprintf(stdout
,"Open Files : %ldt.\n",(long)timeReadLoopStart
- (long)timeStart
);
133 fprintf(stdout
,"Read Data : %ldt.\n",(long)timeReadLoopEnd
- (long)timeReadLoopStart
);
134 fprintf(stdout
,"Sort Data : %ldt.\n",(long)timeSortLoopEnd
- (long)timeSortLoopStart
);
135 fprintf(stdout
,"Write Data : %ldt.\n",(long)timeWriteLoopEnd
- (long)timeWriteLoopStart
);
136 fprintf(stdout
,"Total Time : %ldt.\n",(long)timeEnd
- (long)timeStart
);