Last active
September 16, 2019 11:38
-
-
Save thesunwave/9b4674431cb49c0b25404f91089f9e1c to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"flag" | |
"fmt" | |
"github.com/loov/hrtime" | |
"io/ioutil" | |
"os" | |
"regexp" | |
"sort" | |
"strconv" | |
"sync" | |
_ "time" | |
) | |
var file *os.File | |
var err error | |
var stringSlice []string | |
var intSlice []int | |
var count *int | |
func init() { | |
path := flag.String("path", "./test.txt", "a string") | |
count = flag.Int("count", 1, "an integer") | |
fmt.Println("path", *path) | |
flag.Parse() | |
data, err := ioutil.ReadFile(*path) | |
if err != nil { | |
fmt.Println("File reading error", err) | |
panic(err) | |
} | |
fmt.Println("will be counted at: ", count, "times") | |
compiledRegexp, _ := regexp.Compile("\\d+") | |
stringSlice = compiledRegexp.FindAllString(string(data), -1) | |
for _, i := range stringSlice { | |
j, err := strconv.Atoi(i) | |
if err != nil { | |
panic(err) | |
} | |
intSlice = append(intSlice, j) | |
} | |
} | |
var wg sync.WaitGroup | |
func localSort(slice []int, wg *sync.WaitGroup) { | |
defer wg.Done() | |
fmt.Println(sort.IntsAreSorted(slice)) | |
sort.Ints(slice) | |
fmt.Println(sort.IntsAreSorted(slice)) | |
} | |
func main() { | |
start := hrtime.Now() | |
fmt.Println(hrtime.Since(start)) | |
bench := hrtime.NewBenchmark(*count) | |
var counter = 1 | |
var slice []int | |
for bench.Next() { | |
wg.Add(1) | |
//slice := intSlice | |
copy(slice, intSlice) | |
//fmt.Println(&slice) | |
fmt.Println(sort.IntsAreSorted(slice)) | |
fmt.Println(sort.IntsAreSorted(intSlice)) | |
go localSort(slice, &wg) | |
counter += 1 | |
} | |
wg.Wait() | |
fmt.Println(bench.Histogram(10)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment