diff --git a/cmd/tools/bench/sorting.results.txt b/cmd/tools/bench/sorting.results.txt new file mode 100644 index 0000000000..549bf585cc --- /dev/null +++ b/cmd/tools/bench/sorting.results.txt @@ -0,0 +1,139 @@ +# ./v -prod -cc clang-12 cmd/tools/bench/sorting.v +# for seqkind in increasing decreasing random; do +# for how in qsort aswcc; do +# for x in 10_000_000 1000_000 100_000 10_000 1000 100 10 ; do +# ./cmd/tools/bench/sorting $how $x $seqkind; +# done; +# done; +# echo --------------------------------; +# done > cmd/tools/bench/sorting.results.txt + +> a.len: 10000000 | a: [1, 2, 3, 4, 5, 6, 7] ... [9999994, 9999995, 9999996, 9999997, 9999998, 9999999, 10000000] +> a.len: 10000000 | a: [1, 2, 3, 4, 5, 6, 7] ... [9999994, 9999995, 9999996, 9999997, 9999998, 9999999, 10000000] +SPENT 461.034 ms in qsort 10000000 increasing +> a.len: 1000000 | a: [1, 2, 3, 4, 5, 6, 7] ... [999994, 999995, 999996, 999997, 999998, 999999, 1000000] +> a.len: 1000000 | a: [1, 2, 3, 4, 5, 6, 7] ... [999994, 999995, 999996, 999997, 999998, 999999, 1000000] +SPENT 40.616 ms in qsort 1000000 increasing +> a.len: 100000 | a: [1, 2, 3, 4, 5, 6, 7] ... [99994, 99995, 99996, 99997, 99998, 99999, 100000] +> a.len: 100000 | a: [1, 2, 3, 4, 5, 6, 7] ... [99994, 99995, 99996, 99997, 99998, 99999, 100000] +SPENT 4.415 ms in qsort 100000 increasing +> a.len: 10000 | a: [1, 2, 3, 4, 5, 6, 7] ... [9994, 9995, 9996, 9997, 9998, 9999, 10000] +> a.len: 10000 | a: [1, 2, 3, 4, 5, 6, 7] ... [9994, 9995, 9996, 9997, 9998, 9999, 10000] +SPENT 0.304 ms in qsort 10000 increasing +> a.len: 1000 | a: [1, 2, 3, 4, 5, 6, 7] ... [994, 995, 996, 997, 998, 999, 1000] +> a.len: 1000 | a: [1, 2, 3, 4, 5, 6, 7] ... [994, 995, 996, 997, 998, 999, 1000] +SPENT 0.042 ms in qsort 1000 increasing +> a.len: 100 | a: [1, 2, 3, 4, 5, 6, 7] ... [94, 95, 96, 97, 98, 99, 100] +> a.len: 100 | a: [1, 2, 3, 4, 5, 6, 7] ... [94, 95, 96, 97, 98, 99, 100] +SPENT 0.009 ms in qsort 100 increasing +> a.len: 10 | a: [1, 2, 3, 4, 5, 6, 7] ... [4, 5, 6, 7, 8, 9, 10] +> a.len: 10 | a: [1, 2, 3, 4, 5, 6, 7] ... [4, 5, 6, 7, 8, 9, 10] +SPENT 0.005 ms in qsort 10 increasing +> a.len: 10000000 | a: [1, 2, 3, 4, 5, 6, 7] ... [9999994, 9999995, 9999996, 9999997, 9999998, 9999999, 10000000] +> a.len: 10000000 | a: [1, 2, 3, 4, 5, 6, 7] ... [9999994, 9999995, 9999996, 9999997, 9999998, 9999999, 10000000] +SPENT 109.615 ms in aswcc 10000000 increasing +> a.len: 1000000 | a: [1, 2, 3, 4, 5, 6, 7] ... [999994, 999995, 999996, 999997, 999998, 999999, 1000000] +> a.len: 1000000 | a: [1, 2, 3, 4, 5, 6, 7] ... [999994, 999995, 999996, 999997, 999998, 999999, 1000000] +SPENT 10.520 ms in aswcc 1000000 increasing +> a.len: 100000 | a: [1, 2, 3, 4, 5, 6, 7] ... [99994, 99995, 99996, 99997, 99998, 99999, 100000] +> a.len: 100000 | a: [1, 2, 3, 4, 5, 6, 7] ... [99994, 99995, 99996, 99997, 99998, 99999, 100000] +SPENT 1.038 ms in aswcc 100000 increasing +> a.len: 10000 | a: [1, 2, 3, 4, 5, 6, 7] ... [9994, 9995, 9996, 9997, 9998, 9999, 10000] +> a.len: 10000 | a: [1, 2, 3, 4, 5, 6, 7] ... [9994, 9995, 9996, 9997, 9998, 9999, 10000] +SPENT 0.106 ms in aswcc 10000 increasing +> a.len: 1000 | a: [1, 2, 3, 4, 5, 6, 7] ... [994, 995, 996, 997, 998, 999, 1000] +> a.len: 1000 | a: [1, 2, 3, 4, 5, 6, 7] ... [994, 995, 996, 997, 998, 999, 1000] +SPENT 0.016 ms in aswcc 1000 increasing +> a.len: 100 | a: [1, 2, 3, 4, 5, 6, 7] ... [94, 95, 96, 97, 98, 99, 100] +> a.len: 100 | a: [1, 2, 3, 4, 5, 6, 7] ... [94, 95, 96, 97, 98, 99, 100] +SPENT 0.006 ms in aswcc 100 increasing +> a.len: 10 | a: [1, 2, 3, 4, 5, 6, 7] ... [4, 5, 6, 7, 8, 9, 10] +> a.len: 10 | a: [1, 2, 3, 4, 5, 6, 7] ... [4, 5, 6, 7, 8, 9, 10] +SPENT 0.005 ms in aswcc 10 increasing +-------------------------------- +> a.len: 9999999 | a: [9999999, 9999998, 9999997, 9999996, 9999995, 9999994, 9999993] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 9999999 | a: [1, 2, 3, 4, 5, 6, 7] ... [9999993, 9999994, 9999995, 9999996, 9999997, 9999998, 9999999] +SPENT 452.092 ms in qsort 10000000 decreasing +> a.len: 999999 | a: [999999, 999998, 999997, 999996, 999995, 999994, 999993] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 999999 | a: [1, 2, 3, 4, 5, 6, 7] ... [999993, 999994, 999995, 999996, 999997, 999998, 999999] +SPENT 39.840 ms in qsort 1000000 decreasing +> a.len: 99999 | a: [99999, 99998, 99997, 99996, 99995, 99994, 99993] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 99999 | a: [1, 2, 3, 4, 5, 6, 7] ... [99993, 99994, 99995, 99996, 99997, 99998, 99999] +SPENT 3.609 ms in qsort 100000 decreasing +> a.len: 9999 | a: [9999, 9998, 9997, 9996, 9995, 9994, 9993] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 9999 | a: [1, 2, 3, 4, 5, 6, 7] ... [9993, 9994, 9995, 9996, 9997, 9998, 9999] +SPENT 0.430 ms in qsort 10000 decreasing +> a.len: 999 | a: [999, 998, 997, 996, 995, 994, 993] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 999 | a: [1, 2, 3, 4, 5, 6, 7] ... [993, 994, 995, 996, 997, 998, 999] +SPENT 0.058 ms in qsort 1000 decreasing +> a.len: 99 | a: [99, 98, 97, 96, 95, 94, 93] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 99 | a: [1, 2, 3, 4, 5, 6, 7] ... [93, 94, 95, 96, 97, 98, 99] +SPENT 0.013 ms in qsort 100 decreasing +> a.len: 9 | a: [9, 8, 7, 6, 5, 4, 3] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 9 | a: [1, 2, 3, 4, 5, 6, 7] ... [3, 4, 5, 6, 7, 8, 9] +SPENT 0.006 ms in qsort 10 decreasing +> a.len: 9999999 | a: [9999999, 9999998, 9999997, 9999996, 9999995, 9999994, 9999993] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 9999999 | a: [1, 2, 3, 4, 5, 6, 7] ... [9999993, 9999994, 9999995, 9999996, 9999997, 9999998, 9999999] +SPENT 2313.316 ms in aswcc 10000000 decreasing +> a.len: 999999 | a: [999999, 999998, 999997, 999996, 999995, 999994, 999993] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 999999 | a: [1, 2, 3, 4, 5, 6, 7] ... [999993, 999994, 999995, 999996, 999997, 999998, 999999] +SPENT 203.740 ms in aswcc 1000000 decreasing +> a.len: 99999 | a: [99999, 99998, 99997, 99996, 99995, 99994, 99993] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 99999 | a: [1, 2, 3, 4, 5, 6, 7] ... [99993, 99994, 99995, 99996, 99997, 99998, 99999] +SPENT 17.406 ms in aswcc 100000 decreasing +> a.len: 9999 | a: [9999, 9998, 9997, 9996, 9995, 9994, 9993] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 9999 | a: [1, 2, 3, 4, 5, 6, 7] ... [9993, 9994, 9995, 9996, 9997, 9998, 9999] +SPENT 1.467 ms in aswcc 10000 decreasing +> a.len: 999 | a: [999, 998, 997, 996, 995, 994, 993] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 999 | a: [1, 2, 3, 4, 5, 6, 7] ... [993, 994, 995, 996, 997, 998, 999] +SPENT 0.137 ms in aswcc 1000 decreasing +> a.len: 99 | a: [99, 98, 97, 96, 95, 94, 93] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 99 | a: [1, 2, 3, 4, 5, 6, 7] ... [93, 94, 95, 96, 97, 98, 99] +SPENT 0.015 ms in aswcc 100 decreasing +> a.len: 9 | a: [9, 8, 7, 6, 5, 4, 3] ... [7, 6, 5, 4, 3, 2, 1] +> a.len: 9 | a: [1, 2, 3, 4, 5, 6, 7] ... [3, 4, 5, 6, 7, 8, 9] +SPENT 0.005 ms in aswcc 10 decreasing +-------------------------------- +> a.len: 10000000 | a: [5860191, 5357534, 516370, 444218, 4782576, 3296560, 9597422] ... [9885313, 9298812, 1016850, 595010, 3994125, 6996906, 8189289] +> a.len: 10000000 | a: [1, 2, 3, 3, 4, 4, 5] ... [9999994, 9999994, 9999995, 9999996, 9999997, 9999998, 9999998] +SPENT 1490.203 ms in qsort 10000000 random +> a.len: 1000000 | a: [564075, 516370, 444218, 588272, 209384, 864959, 195323] ... [363186, 584539, 475461, 794706, 23108, 232638, 590085] +> a.len: 1000000 | a: [1, 1, 1, 1, 1, 3, 3] ... [999990, 999990, 999991, 999993, 999995, 999997, 999998] +SPENT 128.973 ms in qsort 1000000 random +> a.len: 100000 | a: [93023, 39787, 90906, 63984, 21510, 44477, 62876] ... [61585, 45923, 26539, 20911, 51779, 34695, 29023] +> a.len: 100000 | a: [1, 1, 2, 3, 4, 5, 7] ... [99991, 99992, 99994, 99994, 99998, 99998, 99999] +SPENT 10.583 ms in qsort 100000 random +> a.len: 10000 | a: [7019, 733, 7844, 97, 1994, 2517, 8308] ... [1293, 4423, 8853, 6021, 9549, 4161, 3700] +> a.len: 10000 | a: [1, 1, 2, 2, 2, 3, 3] ... [9992, 9992, 9994, 9995, 9998, 9998, 9999] +SPENT 0.873 ms in qsort 10000 random +> a.len: 1000 | a: [863, 875, 274, 733, 794, 496, 494] ... [530, 615, 371, 561, 800, 501, 585] +> a.len: 1000 | a: [2, 3, 4, 4, 5, 6, 6] ... [994, 994, 994, 994, 996, 997, 998] +SPENT 0.079 ms in qsort 1000 random +> a.len: 100 | a: [95, 18, 58, 26, 6, 48, 67] ... [11, 80, 16, 60, 58, 71, 1] +> a.len: 100 | a: [1, 3, 4, 5, 5, 6, 6] ... [94, 95, 95, 97, 97, 98, 99] +SPENT 0.010 ms in qsort 100 random +> a.len: 10 | a: [4, 6, 3, 8, 1, 4, 7] ... [8, 1, 4, 7, 5, 8, 4] +> a.len: 10 | a: [1, 3, 4, 4, 4, 5, 6] ... [4, 4, 5, 6, 7, 8, 8] +SPENT 0.005 ms in qsort 10 random +> a.len: 10000000 | a: [5860191, 5357534, 516370, 444218, 4782576, 3296560, 9597422] ... [9885313, 9298812, 1016850, 595010, 3994125, 6996906, 8189289] +> a.len: 10000000 | a: [1, 2, 3, 3, 4, 4, 5] ... [9999994, 9999994, 9999995, 9999996, 9999997, 9999998, 9999998] +SPENT 6279.383 ms in aswcc 10000000 random +> a.len: 1000000 | a: [564075, 516370, 444218, 588272, 209384, 864959, 195323] ... [363186, 584539, 475461, 794706, 23108, 232638, 590085] +> a.len: 1000000 | a: [1, 1, 1, 1, 1, 3, 3] ... [999990, 999990, 999991, 999993, 999995, 999997, 999998] +SPENT 369.465 ms in aswcc 1000000 random +> a.len: 100000 | a: [93023, 39787, 90906, 63984, 21510, 44477, 62876] ... [61585, 45923, 26539, 20911, 51779, 34695, 29023] +> a.len: 100000 | a: [1, 1, 2, 3, 4, 5, 7] ... [99991, 99992, 99994, 99994, 99998, 99998, 99999] +SPENT 24.082 ms in aswcc 100000 random +> a.len: 10000 | a: [7019, 733, 7844, 97, 1994, 2517, 8308] ... [1293, 4423, 8853, 6021, 9549, 4161, 3700] +> a.len: 10000 | a: [1, 1, 2, 2, 2, 3, 3] ... [9992, 9992, 9994, 9995, 9998, 9998, 9999] +SPENT 1.861 ms in aswcc 10000 random +> a.len: 1000 | a: [863, 875, 274, 733, 794, 496, 494] ... [530, 615, 371, 561, 800, 501, 585] +> a.len: 1000 | a: [2, 3, 4, 4, 5, 6, 6] ... [994, 994, 994, 994, 996, 997, 998] +SPENT 0.150 ms in aswcc 1000 random +> a.len: 100 | a: [95, 18, 58, 26, 6, 48, 67] ... [11, 80, 16, 60, 58, 71, 1] +> a.len: 100 | a: [1, 3, 4, 5, 5, 6, 6] ... [94, 95, 95, 97, 97, 98, 99] +SPENT 0.016 ms in aswcc 100 random +> a.len: 10 | a: [4, 6, 3, 8, 1, 4, 7] ... [8, 1, 4, 7, 5, 8, 4] +> a.len: 10 | a: [1, 3, 4, 4, 4, 5, 6] ... [4, 4, 5, 6, 7, 8, 8] +SPENT 0.005 ms in aswcc 10 random +-------------------------------- diff --git a/cmd/tools/bench/sorting.v b/cmd/tools/bench/sorting.v new file mode 100644 index 0000000000..b1ee389921 --- /dev/null +++ b/cmd/tools/bench/sorting.v @@ -0,0 +1,79 @@ +module main + +// This program compares the performance of the native C.qsort function, +// used by the a.sort/0 method, vs the more flexible and platform independent +// (at the cost of being slightly slower) C.vqsort_r (adapted from musl), +// used by the a.sort_with_compare_context/2 method. +// +// Usage: +// ./v -prod -cc gcc cmd/tools/bench/sorting.v +// for seqkind in increasing decreasing random; do +// for how in qsort aswcc; do +// for x in 10_000_000 1000_000 100_000 10_000 1000 100 10 ; do +// ./cmd/tools/bench/sorting $how $x $seqkind; +// done; +// done; +// echo --------------------------------; +// done > cmd/tools/bench/sorting.results.txt +import os +import rand +import benchmark + +fn my_context_compare_ints(const_a &int, const_b &int, context voidptr) int { + res := *const_a - *const_b + if res < 0 { + return -1 + } + if res > 0 { + return 1 + } + return 0 +} + +fn generate(kind string, mut a []int, maxn int) { + match kind { + 'random' { + for _ in 0 .. maxn { + a << rand.int_in_range(1, maxn) or { 0 } + } + } + 'increasing' { + for idx in 0 .. maxn { + a << idx + 1 + } + } + 'decreasing' { + for idx in 1 .. maxn { + a << maxn - idx + } + } + else {} + } +} + +fn sort(how string, mut a []int) { + match how { + 'aswcc' { + a.sort_with_compare_context(my_context_compare_ints, voidptr(3)) + } + 'qsort' { + a.sort() + } + else {} + } +} + +fn main() { + rand.seed([u32(0), 1]) + how := os.args[1] or { 'qsort' } + maxn := os.args[2] or { '1000' }.int() + kind := os.args[3] or { 'random' } + // println('> how: $how | maxn: $maxn') + mut a := []int{cap: maxn} + generate(kind, mut a, maxn) + println('> a.len: ${a.len:12} | a: ${a#[0..7]:50} ... ${a#[-7..]:-50}') + mut b := benchmark.start() + sort(how, mut a) + println('> a.len: ${a.len:12} | a: ${a#[0..7]:50} ... ${a#[-7..]:-50}') + b.measure('$how $maxn $kind') +}