]> Cypherpunks repositories - gostls13.git/commit
sort: reduce number of comparisons needed by medianOfThree
authorMartin Möhrmann <martisch@uos.de>
Fri, 9 Jan 2015 22:25:42 +0000 (23:25 +0100)
committerRobert Griesemer <gri@golang.org>
Tue, 13 Jan 2015 19:37:02 +0000 (19:37 +0000)
commit2c7c727c1cbbf943c8d102610769b6d1fed80dca
treedbf1e9d1001253fad2d8020c3deae2f0cfcd21f9
parentc5f810f05886d5ef07cd34ac53636bc069f73f8b
sort: reduce number of comparisons needed by medianOfThree

For some cases we can ensure the correct order of elements in two
instead of three comparisons. It is unnecessary to compare m0 and
m1 again if m2 and m1 are not swapped.

benchmark                   old ns/op      new ns/op      delta
BenchmarkSortString1K       302721         299590         -1.03%
BenchmarkSortInt1K          124055         123215         -0.68%
BenchmarkSortInt64K         12291522       12203402       -0.72%
BenchmarkSort1e2            58027          57111          -1.58%
BenchmarkSort1e4            12426805       12341761       -0.68%
BenchmarkSort1e6            1966250030     1960557883     -0.29%

Change-Id: I2b17ff8dee310ec9ab92a6f569a95932538768a9
Reviewed-on: https://go-review.googlesource.com/2614
Reviewed-by: Robert Griesemer <gri@golang.org>
src/sort/sort.go