 |
|
File perf_sort.d, 3.4 kB
(added by sean, 11 months ago)
|
|
| Line | |
|---|
| 1 |
import tango.io.Console, |
|---|
| 2 |
tango.io.FileConduit, |
|---|
| 3 |
tango.io.MappedBuffer, |
|---|
| 4 |
tango.time.StopWatch, |
|---|
| 5 |
tango.core.Array, |
|---|
| 6 |
tango.stdc.stdio; |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
int main (char[][] args) |
|---|
| 10 |
{ |
|---|
| 11 |
char[][] dictionary; |
|---|
| 12 |
|
|---|
| 13 |
for (int i = 1; i < args.length; ++i) |
|---|
| 14 |
{ |
|---|
| 15 |
char[] input; |
|---|
| 16 |
int inword; |
|---|
| 17 |
int wstart; |
|---|
| 18 |
|
|---|
| 19 |
auto mmap = new MappedBuffer(new FileConduit(args[i])); |
|---|
| 20 |
input = cast(char[]) mmap.slice; |
|---|
| 21 |
|
|---|
| 22 |
for (int j = 0; j < input.length; j++) |
|---|
| 23 |
{ |
|---|
| 24 |
char c = input[j]; |
|---|
| 25 |
|
|---|
| 26 |
if (c >= '0' && c <= '9') |
|---|
| 27 |
{ |
|---|
| 28 |
} |
|---|
| 29 |
else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') |
|---|
| 30 |
{ |
|---|
| 31 |
if (!inword) |
|---|
| 32 |
{ |
|---|
| 33 |
wstart = j; |
|---|
| 34 |
inword = 1; |
|---|
| 35 |
} |
|---|
| 36 |
} |
|---|
| 37 |
else if (inword) |
|---|
| 38 |
{ |
|---|
| 39 |
char[] word = input[wstart .. j]; |
|---|
| 40 |
dictionary ~= word; |
|---|
| 41 |
inword = 0; |
|---|
| 42 |
} |
|---|
| 43 |
} |
|---|
| 44 |
if (inword) |
|---|
| 45 |
{ |
|---|
| 46 |
char[] word = input[wstart .. input.length]; |
|---|
| 47 |
dictionary ~= word; |
|---|
| 48 |
} |
|---|
| 49 |
} |
|---|
| 50 |
|
|---|
| 51 |
char[][] tmpA = dictionary.dup; |
|---|
| 52 |
char[][] tmpB = dictionary.dup; |
|---|
| 53 |
char[][] tmpC = dictionary.dup; |
|---|
| 54 |
StopWatch w; |
|---|
| 55 |
double a, b, c; |
|---|
| 56 |
|
|---|
| 57 |
w.start; |
|---|
| 58 |
sort( tmpA ); |
|---|
| 59 |
a = w.stop; |
|---|
| 60 |
w.start; |
|---|
| 61 |
introSort( tmpB ); |
|---|
| 62 |
b = w.stop; |
|---|
| 63 |
w.start; |
|---|
| 64 |
tmpC.sort; |
|---|
| 65 |
c = w.stop; |
|---|
| 66 |
printf( "Tango: %f\nintroSort: %f\nPhobos: %f\n", a, b, c ); |
|---|
| 67 |
return 0; |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
alias char[] type; |
|---|
| 72 |
enum : size_t { INSERTIONSORT_THRESHOLD = 80 } |
|---|
| 73 |
bool cmp(type a, type b) { |
|---|
| 74 |
return a < b; |
|---|
| 75 |
} |
|---|
| 76 |
void swap(T)(inout T a, inout T b) { |
|---|
| 77 |
auto |
|---|
| 78 |
t = a; |
|---|
| 79 |
a = b; |
|---|
| 80 |
b = t; |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
void introSort(type[] a) { |
|---|
| 84 |
static type median(type a, type b, type c) { |
|---|
| 85 |
if (cmp(a, b)) |
|---|
| 86 |
if (cmp(b, c)) |
|---|
| 87 |
return b; |
|---|
| 88 |
else if (cmp(a, c)) |
|---|
| 89 |
return c; |
|---|
| 90 |
else |
|---|
| 91 |
return a; |
|---|
| 92 |
else if (cmp(a, c)) |
|---|
| 93 |
return a; |
|---|
| 94 |
else if (cmp(b, c)) |
|---|
| 95 |
return c; |
|---|
| 96 |
else |
|---|
| 97 |
return b; |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
static void doIntroSort(type[] a, size_t maxDepth) { |
|---|
| 101 |
while (a.length > INSERTIONSORT_THRESHOLD) { |
|---|
| 102 |
if (maxDepth-- == 0) |
|---|
| 103 |
return heapSort(a); |
|---|
| 104 |
|
|---|
| 105 |
type pivot = median(a[0], a[$/2], a[$-1]); |
|---|
| 106 |
size_t cut = 0, i = a.length; |
|---|
| 107 |
|
|---|
| 108 |
do { |
|---|
| 109 |
while (cmp(a[cut], pivot)) |
|---|
| 110 |
++cut; |
|---|
| 111 |
--i; |
|---|
| 112 |
while (cmp(pivot, a[i])) |
|---|
| 113 |
--i; |
|---|
| 114 |
swap(a[cut++], a[i]); |
|---|
| 115 |
|
|---|
| 116 |
} while (cut < i); |
|---|
| 117 |
|
|---|
| 118 |
doIntroSort(a[cut..$], maxDepth); |
|---|
| 119 |
a = a[0..cut]; |
|---|
| 120 |
} |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
if (a.length > 1) { |
|---|
| 124 |
size_t lg = 0; |
|---|
| 125 |
size_t i = a.length; |
|---|
| 126 |
do { |
|---|
| 127 |
++lg; |
|---|
| 128 |
i /= 2; |
|---|
| 129 |
} while (i > 1); |
|---|
| 130 |
doIntroSort(a, lg*2); |
|---|
| 131 |
insertionSort(a); |
|---|
| 132 |
} |
|---|
| 133 |
} |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
void insertionSort(type[] a) { |
|---|
| 137 |
for (size_t i = 1; i < a.length; ++i) { |
|---|
| 138 |
type val = a[i]; |
|---|
| 139 |
|
|---|
| 140 |
size_t pos = i-1; |
|---|
| 141 |
for (; pos < a.length && cmp(val, a[pos]); --pos) |
|---|
| 142 |
a[pos+1] = a[pos]; |
|---|
| 143 |
|
|---|
| 144 |
a[pos+1] = val; |
|---|
| 145 |
} |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
void heapSort(type[] a) { |
|---|
| 150 |
if (a.length <= 1) |
|---|
| 151 |
return; |
|---|
| 152 |
|
|---|
| 153 |
auto n = a.length; |
|---|
| 154 |
auto i = n / 2; |
|---|
| 155 |
|
|---|
| 156 |
for (;;) { |
|---|
| 157 |
type t = void; |
|---|
| 158 |
|
|---|
| 159 |
if (i > 0) |
|---|
| 160 |
t = a[--i]; |
|---|
| 161 |
else { |
|---|
| 162 |
if (--n == 0) |
|---|
| 163 |
return; |
|---|
| 164 |
|
|---|
| 165 |
t = a[n]; |
|---|
| 166 |
a[n] = a[0]; |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
auto parent = i; |
|---|
| 170 |
auto child = i*2 + 1; |
|---|
| 171 |
|
|---|
| 172 |
while (child < n) { |
|---|
| 173 |
if (child + 1 < n && cmp(a[child], a[child+1])) |
|---|
| 174 |
++child; |
|---|
| 175 |
|
|---|
| 176 |
if (cmp(t, a[child])) { |
|---|
| 177 |
a[parent] = a[child]; |
|---|
| 178 |
parent = child; |
|---|
| 179 |
child = parent*2 + 1; |
|---|
| 180 |
} else |
|---|
| 181 |
break; |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
a[parent] = t; |
|---|
| 185 |
} |
|---|
| 186 |
} |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic