aboutsummaryrefslogtreecommitdiff
path: root/tests/qsort
diff options
context:
space:
mode:
authorpzhang <pzhang@mozilla.com>2013-07-31 10:32:44 +0800
committerpzhang <pzhang@mozilla.com>2013-07-31 10:32:44 +0800
commitad2bd9af8354849c88254dc01f20fd96549a796f (patch)
treeba88e996f6d37966f9bf3a2fc027a3471e7cc3a7 /tests/qsort
parent61526bc8b6eca8950e34616fc9cad2212c18abe6 (diff)
Reduce call chain of qsort
This will considerably improve the performance of qsort-heavily-used projects like Android-Pinyin-IME, more discussion: https://groups.google.com/forum/#!topic/emscripten-discuss/Ah2CNfQ8ra4 https://github.com/kripken/emscripten/pull/1437
Diffstat (limited to 'tests/qsort')
-rw-r--r--tests/qsort/benchmark.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/qsort/benchmark.cpp b/tests/qsort/benchmark.cpp
new file mode 100644
index 00000000..950ca437
--- /dev/null
+++ b/tests/qsort/benchmark.cpp
@@ -0,0 +1,43 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+
+typedef unsigned int uint32;
+
+int cmp_uint(const void *i1, const void *i2) {
+ if (*static_cast<const uint32*>(i1) >
+ *static_cast<const uint32*>(i2))
+ return 1;
+
+ if (*static_cast<const uint32*>(i1) <
+ *static_cast<const uint32*>(i2))
+ return -1;
+
+ return 0;
+}
+
+int main() {
+ clock_t start = clock();
+ const size_t TIMES = 10000;
+ for (size_t i = 0; i < TIMES; i++) {
+ const size_t num = 100;
+ uint32 rnd[num] = { \
+ 407, 236, 765, 529, 24, 13, 577, 900, 242, 245, \
+ 782, 972, 514, 100, 596, 470, 680, 65, 370, 788, \
+ 44, 330, 579, 314, 914, 399, 100, 945, 992, 412, \
+ 308, 102, 895, 529, 216, 422, 851, 778, 28, 804, \
+ 325, 975, 961, 623, 922, 667, 141, 755, 416, 575, \
+ 712, 503, 174, 675, 14, 647, 544, 881, 858, 621, \
+ 26, 283, 460, 252, 146, 16, 571, 570, 14, 143, \
+ 674, 985, 477, 386, 932, 490, 611, 127, 702, 619, \
+ 104, 892, 58, 635, 663, 424, 714, 740, 229, 538, \
+ 167, 181, 193, 193, 657, 778, 217, 573, 764, 745};
+
+ qsort(rnd, num, sizeof(uint32), cmp_uint);
+ }
+ clock_t end = clock();
+
+ float diff = (((float)end - (float)start) / CLOCKS_PER_SEC ) * 1000;
+ printf("cost %fms\n", diff);
+}
+