Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding mysort function with a benchmark test
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package benchdb | ||
|
||
import ( | ||
"math/rand" | ||
"sort" | ||
"testing" | ||
) | ||
|
||
func mySort(data sort.Interface, a, b int) { | ||
// Insertion sort borrowed from the std library. | ||
for i := a + 1; i < b; i++ { | ||
for j := i; j > a && data.Less(j, j-1); j-- { | ||
data.Swap(j, j-1) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMySort1K(b *testing.B) { | ||
b.StopTimer() | ||
for i := 0; i < b.N; i++ { | ||
data := make([]int, 1000) | ||
for i := 0; i < len(data); i++ { | ||
data[i] = rand.Int() | ||
} | ||
b.StartTimer() | ||
mySort(sort.IntSlice(data), 0, len(data)) | ||
b.StopTimer() | ||
} | ||
} |