From 88da29cad45787872baf0869d5a8f624645167b4 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 13 Jun 2026 14:10:44 +1200 Subject: [PATCH] Add priority heap benchmarks --- benchmark/io/event/priority_heap.rb | 80 +++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 benchmark/io/event/priority_heap.rb diff --git a/benchmark/io/event/priority_heap.rb b/benchmark/io/event/priority_heap.rb new file mode 100644 index 00000000..145d67ee --- /dev/null +++ b/benchmark/io/event/priority_heap.rb @@ -0,0 +1,80 @@ +# frozen_string_literal: true + +# Released under the MIT License. +# Copyright, 2026, by Samuel Williams. + +require "sus/fixtures/benchmark" +require "io/event/priority_heap" + +describe IO::Event::PriorityHeap do + include Sus::Fixtures::Benchmark + + let(:entries) {(0...10_000).to_a} + let(:append_entries) {(10_000...15_000).to_a} + + def build_heap(entries) + heap = subject.new + heap.concat(entries) + return heap + end + + measure "push in-order entries" do |repeats| + heap = subject.new + index = 0 + + repeats.times do + heap.push(index) + index += 1 + end + end + + measure "push reverse-order entries" do |repeats| + heap = subject.new + index = 0 + + repeats.times do + heap.push(-index) + index += 1 + end + end + + measure "concat entries into empty heap" do |repeats| + repeats.times do + heap = subject.new + heap.concat(entries) + end + end + + measure "pop entries" do |repeats| + heap = build_heap(entries) + + repeats.times do + if heap.empty? + heap.concat(entries) + end + + heap.pop + end + end + + measure "heapify after deleting half and appending half" do |repeats| + repeats.times do + heap = build_heap(entries) + + heap.heapify do |contents| + contents.delete_if{|element| element.even?} + contents.concat(append_entries) + end + end + end + + measure "delete_if half the entries" do |repeats| + repeats.times do + heap = build_heap(entries) + + heap.delete_if do |element| + element.even? + end + end + end +end