package main import ( "bufio" "fmt" "math/rand" "os" dg "source/pkg/gen_data" rs "source/pkg/resulter" ll "source/pkg/structures/linked_list" "time" ) const ( countUsers = 10000 countRepeat = 5 countRandomSearch = 10000 countNotExitstSearch = 10 countDeletes = 50 ) func isInArr(arr []int, length int, target int) bool { for i := 0; i < length; i++ { if arr[i] == target { return true } } return false } func Razdelitel() { for i := 0; i < 20; i++ { fmt.Print("-") } fmt.Println() } func pressEnterToContinue() { fmt.Print("Нажмите Enter для продолжения...") bufio.NewReader(os.Stdin).ReadBytes('\n') } func main() { fmt.Println("hello world!") results := make([]rs.BenchmarkResult, 0, countUsers) averageInsertTime := 0. Razdelitel() fmt.Println("Тестирование вставки:") var head *ll.LinkedList = nil for testNum := 0; testNum < countRepeat; testNum++ { head = nil testData := dg.RecordsShuffled(countUsers) start := time.Now() for i := 0; i < countUsers; i++ { head.Insert(testData[i]) } elapsed := time.Since(start).Seconds() averageInsertTime += elapsed results = append(results, rs.BenchmarkResult{ Structure: "Связный список", Mode: "Случайный", Operation: "Вставка", Time: elapsed, }) } averageInsertTime /= countRepeat results = append(results, rs.BenchmarkResult{ Structure: "Связный список", Mode: "Случайный", Operation: "Вставка", Time: averageInsertTime, }) for i := 0; i < 6; i++ { fmt.Println(results[i].ToString()) } Razdelitel() // fmt.Println("Тестирование Поиска:") // // results = make([]rs.BenchmarkResult, 0, countUsers) // averageSearchTime := 0. // for testNum := 0; testNum < countRepeat; testNum++ { // // var head *ll.LinkedList = nil // // head = dg.RecordsShuffled(countUsers) // testData := make([]ds.MyData, countRandomSearch) // for i := 0; i < countRandomSearch; i++ { // // randInd := rand.Intn(countUsers) // randInd := rand.Intn(1000) + 9000 // testData[i], _ = head.GetByInd(randInd) // // fmt.Println(randInd) // } // start := time.Now() // for i := 0; i < countRandomSearch; i++ { // head.Search(testData[i].Name) // } // elapsed := time.Since(start).Seconds() // averageSearchTime += elapsed // results = append(results, rs.BenchmarkResult{ // Structure: "Связный список", Mode: "Случайный", Operation: "Поиск", Time: elapsed, // }) // } // averageSearchTime /= countRepeat // results = append(results, rs.BenchmarkResult{ // Structure: "Связный список", Mode: "Случайный", Operation: "Поиск", Time: averageSearchTime, // }) // for i := 0; i < len(results); i++ { // fmt.Println(results[i].ToString()) // } resultsS := runSearchBenchmark(head) for i := 0; i < len(resultsS); i++ { fmt.Println(resultsS[i].ToString()) } // rs.AppendRaw(results) } func runSearchBenchmark(head *ll.LinkedList) []rs.BenchmarkResult { fmt.Println("\n=== Тестирование Поиска ===") const countRandomSearch = 1000 // уменьшим для поиска var results []rs.BenchmarkResult var totalTime float64 // Предварительно собираем имена для поиска names := make([]string, countUsers) for i := 0; i < countUsers; i++ { data, found := head.GetByInd(i) if found { names[i] = data.Name } } for testNum := 0; testNum < countRepeat; testNum++ { // Выбираем случайные имена searchNames := make([]string, countRandomSearch) for i := 0; i < countRandomSearch; i++ { searchNames[i] = names[rand.Intn(countUsers)] } start := time.Now() for _, name := range searchNames { el, ok := head.Search(name) if ok { fmt.Println(el) } } elapsed := time.Since(start).Seconds() totalTime += elapsed fmt.Printf(" Тест %d: %.6f сек (%.2f мкс/оп)\n", testNum+1, elapsed, elapsed/float64(countRandomSearch)*1_000_000) } avgTime := totalTime / float64(countRepeat) fmt.Printf("Среднее: %.6f сек\n", avgTime) results = append(results, rs.BenchmarkResult{ Structure: "Связный список", Mode: "Случайный", Operation: "Поиск", Time: avgTime, }) return results }