|
| 1 | +#include "pch.h" |
| 2 | + |
| 3 | +#include "Utils.h" |
| 4 | + |
| 5 | +template<typename T> |
| 6 | +struct NodeT { |
| 7 | + T value; |
| 8 | + NodeT *next; |
| 9 | + NodeT(const T& _value, NodeT *_next): value(_value), next(_next){} |
| 10 | +}; |
| 11 | + |
| 12 | +template<typename T> |
| 13 | +struct LinkedListSortAlgo { |
| 14 | + typedef NodeT<T> Node; |
| 15 | + |
| 16 | + virtual ~LinkedListSortAlgo() {} |
| 17 | + virtual const char *getName() = 0; |
| 18 | + virtual int getLimit() = 0; |
| 19 | + virtual void sort(Node *begin, Node *end) = 0; |
| 20 | + virtual void beginAlloc(int n) {} |
| 21 | + virtual void endAlloc() {} |
| 22 | + virtual Node* allocNode(int value, Node *next) { return new Node(value, next); } |
| 23 | + virtual void freeNode(Node *n) { delete n; } |
| 24 | + |
| 25 | + void assertSorted(Node *n) { |
| 26 | + for (; n->next != nullptr; n = n->next) { |
| 27 | + if (n->value > n->next->value) { |
| 28 | + printf("sort failed! => %s\n", getName()); |
| 29 | + assert(0); |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + void run(T *begin, T *end) { |
| 35 | + printf("%s: len=%d\n", getName(), end - begin); |
| 36 | + |
| 37 | + Node *head = nullptr; |
| 38 | + { |
| 39 | + double time = getTime(); |
| 40 | + beginAlloc(end - begin); |
| 41 | + for (T *p = end - 1; p >= begin; --p) { |
| 42 | + head = allocNode(*p, head); |
| 43 | + } |
| 44 | + printf("\tcreate list: %fs\n", getTime() - time); |
| 45 | + } |
| 46 | + |
| 47 | + { |
| 48 | + double time = getTime(); |
| 49 | + sort(head, nullptr); |
| 50 | + time = getTime() - time; |
| 51 | + printf("\tsort : %fs, TPE=%fns\n", time, time * 1000000000 / (end - begin)); |
| 52 | + } |
| 53 | + assertSorted(head); |
| 54 | + { |
| 55 | + double time = getTime(); |
| 56 | + for (Node *next; head != nullptr; head = next) { |
| 57 | + next = head->next; |
| 58 | + freeNode(head); |
| 59 | + } |
| 60 | + endAlloc(); |
| 61 | + printf("\tdestroy list: %fs\n", getTime() - time); |
| 62 | + } |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +template<typename T> |
| 67 | +class LinkedListSortAlgo_selectionSort: public LinkedListSortAlgo<T> { |
| 68 | +public: |
| 69 | + using typename LinkedListSortAlgo<T>::Node; |
| 70 | + |
| 71 | + virtual const char *getName() { return "selectionSort"; } |
| 72 | + virtual int getLimit() { return 16 * 1024; } |
| 73 | + virtual void sort(Node *begin, Node *end) { |
| 74 | + for (Node *p = begin; p != end; p = p->next) { |
| 75 | + for (Node *q = p->next; q != end; q = q->next) { |
| 76 | + if (q->value < p->value) swap(p->value, q->value); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +}; |
| 81 | +template<typename T> |
| 82 | +class LinkedListSortAlgo_selectionSortPool: public LinkedListSortAlgo_selectionSort<T> { |
| 83 | +public: |
| 84 | + using typename LinkedListSortAlgo_selectionSort<T>::Node; |
| 85 | + |
| 86 | + virtual const char *getName() { return "selectionSortPool"; } |
| 87 | + virtual Node* allocNode(int value, Node *next) { |
| 88 | + return new (mPoolFree++) Node(value, next); |
| 89 | + } |
| 90 | + virtual void freeNode(Node *n) { |
| 91 | + } |
| 92 | + virtual void beginAlloc(int n) { |
| 93 | + mPool = (Node*)malloc(sizeof(Node) * n); |
| 94 | + mPoolFree = mPool; |
| 95 | + } |
| 96 | + virtual void endAlloc() { |
| 97 | + free(mPool); |
| 98 | + } |
| 99 | +private: |
| 100 | + Node *mPool; |
| 101 | + Node *mPoolFree; |
| 102 | +}; |
| 103 | + |
| 104 | +template<typename T> |
| 105 | +class LinkedListSortAlgo_quickSort: public LinkedListSortAlgo<T> { |
| 106 | +public: |
| 107 | + using typename LinkedListSortAlgo<T>::Node; |
| 108 | + |
| 109 | + virtual const char *getName() { return "quickSort"; } |
| 110 | + virtual int getLimit() { return 512 * 1024; } |
| 111 | + virtual void sort(Node *begin, Node *end) { |
| 112 | + if (begin == end || begin->next == end) return; |
| 113 | + |
| 114 | + Node *mid = begin; |
| 115 | + for (Node *p = begin->next; p != end; p = p->next) { |
| 116 | + if (p->value <= begin->value) { |
| 117 | + mid = mid->next; |
| 118 | + swap(p->value, mid->value); |
| 119 | + } |
| 120 | + } |
| 121 | + swap(begin->value, mid->value); |
| 122 | + |
| 123 | + sort(begin, mid); |
| 124 | + sort(mid->next, end); |
| 125 | + } |
| 126 | +}; |
| 127 | + |
| 128 | +template<typename T> |
| 129 | +class LinkedListSortAlgo_quickSortPool: public LinkedListSortAlgo_quickSort<T> { |
| 130 | +public: |
| 131 | + using typename LinkedListSortAlgo_quickSort<T>::Node; |
| 132 | + |
| 133 | + virtual const char *getName() { return "quickSortPool"; } |
| 134 | + virtual Node* allocNode(int value, Node *next) { |
| 135 | + return new (mPoolFree++) Node(value, next); |
| 136 | + } |
| 137 | + virtual void freeNode(Node *n) { |
| 138 | + } |
| 139 | + virtual void beginAlloc(int n) { |
| 140 | + mPool = (Node*)malloc(sizeof(Node) * n); |
| 141 | + mPoolFree = mPool; |
| 142 | + } |
| 143 | + virtual void endAlloc() { |
| 144 | + free(mPool); |
| 145 | + } |
| 146 | +private: |
| 147 | + Node *mPool; |
| 148 | + Node *mPoolFree; |
| 149 | +}; |
| 150 | + |
| 151 | +static void benchmark(vector<LinkedListSortAlgo<int>*> algos) { |
| 152 | + int lens[] = {1, 5, 13, 19, 33, 65, 128, 255, 512, 1024, 2 * 1024, 4 * 1024, 8 * 1024, 16 * 1024, 32 * 1024, 64 * 1024, 128 * 1024, 256 * 1024, 512 * 1024}; |
| 153 | + for (int len : lens) { |
| 154 | + vector<int> data(len); |
| 155 | + for (int &d : data) d = myrand(len); |
| 156 | + |
| 157 | + vector<int> tdata(data); |
| 158 | + for (auto algo : algos) { |
| 159 | + if (len > algo->getLimit()) continue; |
| 160 | + tdata = data; |
| 161 | + algo->run(&tdata[0], &tdata[0] + tdata.size()); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +int main() { |
| 167 | + srand(time(nullptr)); |
| 168 | + setCpuAffinity(1); |
| 169 | + |
| 170 | + vector<LinkedListSortAlgo<int>*> algos = { |
| 171 | + new LinkedListSortAlgo_selectionSort<int>(), |
| 172 | + new LinkedListSortAlgo_selectionSortPool<int>(), |
| 173 | + new LinkedListSortAlgo_quickSort<int>(), |
| 174 | + new LinkedListSortAlgo_quickSortPool<int>(), |
| 175 | + }; |
| 176 | + benchmark(algos); |
| 177 | + for (auto p : algos) delete p; |
| 178 | +} |
0 commit comments