From 593aee080b8872ab4a11082fff534fd75f3ea831 Mon Sep 17 00:00:00 2001 From: Arpit Mohapatra Date: Sun, 8 Sep 2024 17:25:24 +0530 Subject: [PATCH] fixed build issues --- cmd/performance/main.go | 191 --------------------------------- cmd/performance/performance.go | 191 +++++++++++++++++++++++++++++++++ go.mod | 2 + go.sum | 2 + 4 files changed, 195 insertions(+), 191 deletions(-) delete mode 100644 cmd/performance/main.go create mode 100644 cmd/performance/performance.go create mode 100644 go.sum diff --git a/cmd/performance/main.go b/cmd/performance/main.go deleted file mode 100644 index 4e990e1..0000000 --- a/cmd/performance/main.go +++ /dev/null @@ -1,191 +0,0 @@ -// package main - -// import ( -// "fmt" -// "math/rand" -// "runtime" -// "strconv" -// "time" - -// mapset "github.com/deckarep/golang-set/v2" -// "github.com/marpit19/goquickmap/pkg/quickmap" -// "github.com/marpit19/goquickmap/pkg/quickset" -// ) - -// const ( -// numOperations = 1000000 -// numBatchOperations = 10000 -// ) - -// func main() { -// fmt.Println("Performance Comparison") -// fmt.Printf("Number of operations: %d\n", numOperations) -// fmt.Printf("Number of batch operations: %d\n", numBatchOperations) - -// compareMap() -// compareSet() -// } - -// func compareMap() { -// fmt.Println("\n--- Map Comparison ---") - -// // Built-in map -// start := time.Now() -// m := make(map[string]int) -// for i := 0; i < numOperations; i++ { -// key := strconv.Itoa(i) -// m[key] = i -// } -// builtinInsertTime := time.Since(start) - -// start = time.Now() -// for i := 0; i < numOperations; i++ { -// key := strconv.Itoa(i) -// _ = m[key] -// } -// builtinGetTime := time.Since(start) - -// start = time.Now() -// for i := 0; i < numOperations; i++ { -// key := strconv.Itoa(i) -// delete(m, key) -// } -// builtinDeleteTime := time.Since(start) - -// // QuickMap -// start = time.Now() -// qm := quickmap.New() -// for i := 0; i < numOperations; i++ { -// key := strconv.Itoa(i) -// qm.Insert(key, i) -// } -// quickmapInsertTime := time.Since(start) - -// start = time.Now() -// for i := 0; i < numOperations; i++ { -// key := strconv.Itoa(i) -// _, _ = qm.Get(key) -// } -// quickmapGetTime := time.Since(start) - -// start = time.Now() -// for i := 0; i < numOperations; i++ { -// key := strconv.Itoa(i) -// qm.Delete(key) -// } -// quickmapDeleteTime := time.Since(start) - -// // Batch operations -// batchKeys := make([]string, numBatchOperations) -// batchMap := make(map[string]interface{}, numBatchOperations) -// for i := 0; i < numBatchOperations; i++ { -// key := strconv.Itoa(rand.Intn(numOperations)) -// batchKeys[i] = key -// batchMap[key] = i -// } - -// start = time.Now() -// qm.InsertMany(batchMap) -// quickmapBatchInsertTime := time.Since(start) - -// start = time.Now() -// qm.DeleteMany(batchKeys) -// quickmapBatchDeleteTime := time.Since(start) - -// // Print results -// fmt.Println("Built-in map:") -// fmt.Printf(" Insert: %v\n", builtinInsertTime) -// fmt.Printf(" Get: %v\n", builtinGetTime) -// fmt.Printf(" Delete: %v\n", builtinDeleteTime) - -// fmt.Println("QuickMap:") -// fmt.Printf(" Insert: %v\n", quickmapInsertTime) -// fmt.Printf(" Get: %v\n", quickmapGetTime) -// fmt.Printf(" Delete: %v\n", quickmapDeleteTime) -// fmt.Printf(" Batch Insert (%d items): %v\n", numBatchOperations, quickmapBatchInsertTime) -// fmt.Printf(" Batch Delete (%d items): %v\n", numBatchOperations, quickmapBatchDeleteTime) -// } - -// func compareSet() { -// fmt.Println("\n--- Set Comparison ---") - -// // golang-set -// start := time.Now() -// s := mapset.NewSet[string]() -// for i := 0; i < numOperations; i++ { -// s.Add(strconv.Itoa(i)) -// } -// mapsetAddTime := time.Since(start) - -// start = time.Now() -// for i := 0; i < numOperations; i++ { -// s.Contains(strconv.Itoa(i)) -// } -// mapsetContainsTime := time.Since(start) - -// start = time.Now() -// for i := 0; i < numOperations; i++ { -// s.Remove(strconv.Itoa(i)) -// } -// mapsetRemoveTime := time.Since(start) - -// // QuickSet -// start = time.Now() -// qs := quickset.New() -// for i := 0; i < numOperations; i++ { -// qs.Add(strconv.Itoa(i)) -// } -// quicksetAddTime := time.Since(start) - -// start = time.Now() -// for i := 0; i < numOperations; i++ { -// qs.Contains(strconv.Itoa(i)) -// } -// quicksetContainsTime := time.Since(start) - -// start = time.Now() -// for i := 0; i < numOperations; i++ { -// qs.Remove(strconv.Itoa(i)) -// } -// quicksetRemoveTime := time.Since(start) - -// // Batch operations -// batchElements := make([]string, numBatchOperations) -// for i := 0; i < numBatchOperations; i++ { -// batchElements[i] = strconv.Itoa(rand.Intn(numOperations)) -// } - -// start = time.Now() -// qs.AddMany(batchElements) -// quicksetBatchAddTime := time.Since(start) - -// start = time.Now() -// qs.RemoveMany(batchElements) -// quicksetBatchRemoveTime := time.Since(start) - -// // Print results -// fmt.Println("golang-set:") -// fmt.Printf(" Add: %v\n", mapsetAddTime) -// fmt.Printf(" Contains: %v\n", mapsetContainsTime) -// fmt.Printf(" Remove: %v\n", mapsetRemoveTime) - -// fmt.Println("QuickSet:") -// fmt.Printf(" Add: %v\n", quicksetAddTime) -// fmt.Printf(" Contains: %v\n", quicksetContainsTime) -// fmt.Printf(" Remove: %v\n", quicksetRemoveTime) -// fmt.Printf(" Batch Add (%d items): %v\n", numBatchOperations, quicksetBatchAddTime) -// fmt.Printf(" Batch Remove (%d items): %v\n", numBatchOperations, quicksetBatchRemoveTime) -// } - -// func printMemUsage() { -// var m runtime.MemStats -// runtime.ReadMemStats(&m) -// fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc)) -// fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc)) -// fmt.Printf("\tSys = %v MiB", bToMb(m.Sys)) -// fmt.Printf("\tNumGC = %v\n", m.NumGC) -// } - -// func bToMb(b uint64) uint64 { -// return b / 1024 / 1024 -// } diff --git a/cmd/performance/performance.go b/cmd/performance/performance.go new file mode 100644 index 0000000..8ab7b0f --- /dev/null +++ b/cmd/performance/performance.go @@ -0,0 +1,191 @@ +package main + +import ( + "fmt" + "math/rand" + "runtime" + "strconv" + "time" + + mapset "github.com/deckarep/golang-set/v2" + "github.com/marpit19/goquickmap/pkg/quickmap" + "github.com/marpit19/goquickmap/pkg/quickset" +) + +const ( + numOperations = 1000000 + numBatchOperations = 10000 +) + +func main() { + fmt.Println("Performance Comparison") + fmt.Printf("Number of operations: %d\n", numOperations) + fmt.Printf("Number of batch operations: %d\n", numBatchOperations) + + compareMap() + compareSet() +} + +func compareMap() { + fmt.Println("\n--- Map Comparison ---") + + // Built-in map + start := time.Now() + m := make(map[string]int) + for i := 0; i < numOperations; i++ { + key := strconv.Itoa(i) + m[key] = i + } + builtinInsertTime := time.Since(start) + + start = time.Now() + for i := 0; i < numOperations; i++ { + key := strconv.Itoa(i) + _ = m[key] + } + builtinGetTime := time.Since(start) + + start = time.Now() + for i := 0; i < numOperations; i++ { + key := strconv.Itoa(i) + delete(m, key) + } + builtinDeleteTime := time.Since(start) + + // QuickMap + start = time.Now() + qm := quickmap.New() + for i := 0; i < numOperations; i++ { + key := strconv.Itoa(i) + qm.Insert(key, i) + } + quickmapInsertTime := time.Since(start) + + start = time.Now() + for i := 0; i < numOperations; i++ { + key := strconv.Itoa(i) + _, _ = qm.Get(key) + } + quickmapGetTime := time.Since(start) + + start = time.Now() + for i := 0; i < numOperations; i++ { + key := strconv.Itoa(i) + qm.Delete(key) + } + quickmapDeleteTime := time.Since(start) + + // Batch operations + batchKeys := make([]string, numBatchOperations) + batchMap := make(map[string]interface{}, numBatchOperations) + for i := 0; i < numBatchOperations; i++ { + key := strconv.Itoa(rand.Intn(numOperations)) + batchKeys[i] = key + batchMap[key] = i + } + + start = time.Now() + qm.InsertMany(batchMap) + quickmapBatchInsertTime := time.Since(start) + + start = time.Now() + qm.DeleteMany(batchKeys) + quickmapBatchDeleteTime := time.Since(start) + + // Print results + fmt.Println("Built-in map:") + fmt.Printf(" Insert: %v\n", builtinInsertTime) + fmt.Printf(" Get: %v\n", builtinGetTime) + fmt.Printf(" Delete: %v\n", builtinDeleteTime) + + fmt.Println("QuickMap:") + fmt.Printf(" Insert: %v\n", quickmapInsertTime) + fmt.Printf(" Get: %v\n", quickmapGetTime) + fmt.Printf(" Delete: %v\n", quickmapDeleteTime) + fmt.Printf(" Batch Insert (%d items): %v\n", numBatchOperations, quickmapBatchInsertTime) + fmt.Printf(" Batch Delete (%d items): %v\n", numBatchOperations, quickmapBatchDeleteTime) +} + +func compareSet() { + fmt.Println("\n--- Set Comparison ---") + + // golang-set + start := time.Now() + s := mapset.NewSet[string]() + for i := 0; i < numOperations; i++ { + s.Add(strconv.Itoa(i)) + } + mapsetAddTime := time.Since(start) + + start = time.Now() + for i := 0; i < numOperations; i++ { + s.Contains(strconv.Itoa(i)) + } + mapsetContainsTime := time.Since(start) + + start = time.Now() + for i := 0; i < numOperations; i++ { + s.Remove(strconv.Itoa(i)) + } + mapsetRemoveTime := time.Since(start) + + // QuickSet + start = time.Now() + qs := quickset.New() + for i := 0; i < numOperations; i++ { + qs.Add(strconv.Itoa(i)) + } + quicksetAddTime := time.Since(start) + + start = time.Now() + for i := 0; i < numOperations; i++ { + qs.Contains(strconv.Itoa(i)) + } + quicksetContainsTime := time.Since(start) + + start = time.Now() + for i := 0; i < numOperations; i++ { + qs.Remove(strconv.Itoa(i)) + } + quicksetRemoveTime := time.Since(start) + + // Batch operations + batchElements := make([]string, numBatchOperations) + for i := 0; i < numBatchOperations; i++ { + batchElements[i] = strconv.Itoa(rand.Intn(numOperations)) + } + + start = time.Now() + qs.AddMany(batchElements) + quicksetBatchAddTime := time.Since(start) + + start = time.Now() + qs.RemoveMany(batchElements) + quicksetBatchRemoveTime := time.Since(start) + + // Print results + fmt.Println("golang-set:") + fmt.Printf(" Add: %v\n", mapsetAddTime) + fmt.Printf(" Contains: %v\n", mapsetContainsTime) + fmt.Printf(" Remove: %v\n", mapsetRemoveTime) + + fmt.Println("QuickSet:") + fmt.Printf(" Add: %v\n", quicksetAddTime) + fmt.Printf(" Contains: %v\n", quicksetContainsTime) + fmt.Printf(" Remove: %v\n", quicksetRemoveTime) + fmt.Printf(" Batch Add (%d items): %v\n", numBatchOperations, quicksetBatchAddTime) + fmt.Printf(" Batch Remove (%d items): %v\n", numBatchOperations, quicksetBatchRemoveTime) +} + +func printMemUsage() { + var m runtime.MemStats + runtime.ReadMemStats(&m) + fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc)) + fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc)) + fmt.Printf("\tSys = %v MiB", bToMb(m.Sys)) + fmt.Printf("\tNumGC = %v\n", m.NumGC) +} + +func bToMb(b uint64) uint64 { + return b / 1024 / 1024 +} diff --git a/go.mod b/go.mod index 6f722b7..be2ff11 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/marpit19/goquickmap go 1.23.1 + +require github.com/deckarep/golang-set/v2 v2.6.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..241f6fe --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM= +github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=