{
}
Online Python Compiler
Online R Compiler
Online SQL Editor
Online HTML/CSS Editor
Online Java Compiler
Online C Compiler
Online C++ Compiler
Online C# Compiler
Online JavaScript Compiler
Online Typescript Compiler
Typescript Online Compiler
Online GoLang Compiler
Online Rust Compiler
Scala Online Compiler
Dart Online Compiler
Ruby Online Compiler
Online PHP Compiler
Online Swift Compiler
Generating Link
Generating Link
Share your code
Share code
Copy Link
Copied to clipboard
or share using
Ends in
Buy 1 year, 🎁 Gift 1 year — completely FREE
Split the cost with a friend. You both get 12 months for $99.
Buy 1 year, Gift 1 year —completely FREE
Start FREE Trial
Start FREE Trial
Ends in
Buy 1 year, 🎁 Gift 1 year — completely FREE
Split the cost with a friend. You both get 12 months for $99.
Buy 1 year, Gift 1 year —completely FREE
Start FREE Trial
Start FREE Trial
Go Online Compiler
Learn Python App
Learn Python
main.go
Output
main.go
Share
Run
Run
package main import ( "container/heap" "fmt" "math/rand" "time" ) const n = 100_000 type tuple struct { k1 int k2 int k3 int k4 int } type Task struct { keys tuple key int taskID int userID int } func NewTask(k1, k2, k3, k4 int, taskID, userID int) Task { key := k1 + k2 + k3 + k4 return Task{tuple{k1, k2, k3, k4}, key, taskID, userID} } type TaskHeap []Task func (h TaskHeap) Len() int { return len(h) } func (h TaskHeap) Less(i, j int) bool { return h[i].key < h[j].key } func (h TaskHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *TaskHeap) Push(x any) { *h = append(*h, x.(Task)) } func (h *TaskHeap) Pop() any { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } func tasks() []Task { tasks := []Task{} for i := range n { tasks = append(tasks, NewTask( -rand.Intn(1000), rand.Intn(1000), -rand.Intn(1000), -rand.Intn(1000), i, rand.Intn(1000), )) } return tasks } func main() { tasks := tasks() h := make(TaskHeap, len(tasks)) heap.Init(&h) start := time.Now() for _, task := range tasks { heap.Push(&h, task) } for range len(tasks) { _ = heap.Pop(&h) } elapsed := time.Since(start) fmt.Println(elapsed) }
Output
Clear