diff --git a/benchmark/add_performance_comparison.png b/benchmark/add_performance_comparison.png index 5e1ca06..c77dd11 100644 Binary files a/benchmark/add_performance_comparison.png and b/benchmark/add_performance_comparison.png differ diff --git a/benchmark/benchmark.rb b/benchmark/benchmark.rb index 57e44d4..f33e803 100644 --- a/benchmark/benchmark.rb +++ b/benchmark/benchmark.rb @@ -19,9 +19,10 @@ # Benchmarking original SortedSet bm.report("SortedSet #{n}:") do - total_time = {add: 0, include: 0, loop: 0, delete: 0} + total_time = {initialize: 0, add: 0, include: 0, loop: 0, delete: 0} runs.times do sorted_set = SortedSet.new + total_time[:initialize] += Benchmark.measure { SortedSet.new(list_adds) }.real total_time[:add] += Benchmark.measure { list_adds.each { |i| sorted_set.add(i) } }.real total_time[:include] += Benchmark.measure { (1..n).map { rand((-0.5*n).to_i..(n*1.5).to_i) }.each { |i| sorted_set.include?(i) } }.real total_time[:loop] += Benchmark.measure { sorted_set.each { |i| } }.real @@ -31,6 +32,7 @@ end end.real end + results_for_n[:initialize_sorted_set] = total_time[:initialize] / runs results_for_n[:add_sorted_set] = total_time[:add] / runs results_for_n[:include_sorted_set] = total_time[:include] / runs results_for_n[:loop_sorted_set] = total_time[:loop] / runs @@ -39,9 +41,10 @@ # Benchmarking custom SortedSet bm.report("SortedContainers #{n}:") do - total_time = {add: 0, include: 0, loop: 0, delete: 0} + total_time = {initialize: 0, add: 0, include: 0, loop: 0, delete: 0} runs.times do sorted_set = SortedContainers::SortedSet.new + total_time[:initialize] += Benchmark.measure { SortedContainers::SortedSet.new(list_adds) }.real total_time[:add] += Benchmark.measure { list_adds.each { |i| sorted_set.add(i) } }.real total_time[:include] += Benchmark.measure { (1..n).map { rand((-0.5*n).to_i..(n*1.5).to_i) }.each { |i| sorted_set.include?(i) } }.real total_time[:loop] += Benchmark.measure { sorted_set.each { |i| } }.real @@ -51,6 +54,7 @@ end end.real end + results_for_n[:initialize_sorted_containers] = total_time[:initialize] / runs results_for_n[:add_sorted_containers] = total_time[:add] / runs results_for_n[:include_sorted_containers] = total_time[:include] / runs results_for_n[:loop_sorted_containers] = total_time[:loop] / runs diff --git a/benchmark/benchmark_init_only.rb b/benchmark/benchmark_init_only.rb deleted file mode 100644 index 8edf515..0000000 --- a/benchmark/benchmark_init_only.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -require "benchmark" -require "sorted_set" -require_relative "../lib/sorted_containers/sorted_set" -require "csv" - -sizes = [1_000_000, 2_000_000, 3_000_000, 4_000_000, 5_000_000] -#sizes = [100_000, 200_000, 300_000, 400_000, 500_000] -#sizes = [10_000, 20_000, 30_000, 40_000, 50_000] -results = [] -runs = 5 - -Benchmark.bm(15) do |bm| - sizes.each do |n| - # The items to be added to the set - list_adds = (1..n).to_a.shuffle - results_for_n = { size: n } - - # Benchmarking original SortedSet - bm.report("SortedSet #{n}:") do - total_time = {init: 0} - runs.times do - total_time[:init] += Benchmark.measure { SortedSet.new(list_adds) }.real - end - results_for_n[:init_sorted_set] = total_time[:init] / runs - end - - # Benchmarking custom SortedSet - bm.report("SortedContainers #{n}:") do - total_time = {init: 0} - runs.times do - total_time[:init] += Benchmark.measure { SortedContainers::SortedSet.new(list_adds) }.real - end - results_for_n[:init_sorted_containers] = total_time[:init] / runs - end - results << results_for_n - end -end - -CSV.open("benchmark_results_init.csv", "wb") do |csv| - csv << results.first.keys - results.each do |result| - csv << result.values - end -end \ No newline at end of file diff --git a/benchmark/benchmark_results.csv b/benchmark/benchmark_results.csv index 3055636..053d56e 100644 --- a/benchmark/benchmark_results.csv +++ b/benchmark/benchmark_results.csv @@ -1,6 +1,6 @@ -size,add_sorted_set,include_sorted_set,loop_sorted_set,delete_sorted_set,add_sorted_containers,include_sorted_containers,loop_sorted_containers,delete_sorted_containers -1000000,1.2325135999359191,11.639327200129628,0.14237359995022417,1.3688056000508368,1.564646599907428,0.6316141999326647,0.044612800050526855,1.8568651999346912 -2000000,3.1588088000193237,42.194053599890324,0.3135467999614775,3.3180113999173044,3.7178223999217153,1.5509135999716819,0.08306660009548068,4.205694200005382 -3000000,5.286830999981612,95.30711920000613,0.47653339989483356,5.443703200109303,6.175909599941224,2.700068200007081,0.11789239989593625,6.770565199945122 -4000000,7.538741199858487,153.70711900005116,0.6546002000570297,7.559051800053567,8.882256799936295,4.182955399993807,0.1547184000723064,9.676217600051313 -5000000,10.20743679990992,241.74679239979014,0.8260514000430703,9.85318099996075,11.755913200229406,5.6500453999266025,0.18556239986792206,12.258404000196606 +size,initialize_sorted_set,add_sorted_set,include_sorted_set,loop_sorted_set,delete_sorted_set,initialize_sorted_containers,add_sorted_containers,include_sorted_containers,loop_sorted_containers,delete_sorted_containers +1000000,1.224537000246346,1.3319036000408233,11.751401599962264,0.1402883999980986,1.3348814000375568,0.4023770001716912,1.5611974000930786,0.6331466001458466,0.04364499999210238,1.8914303999394178 +2000000,2.99586120005697,3.4789291999302803,43.044228199962525,0.3109924000687897,3.1296502000652255,0.9056976000778377,3.781497000064701,1.4786425999365747,0.08165540006011725,4.254382199980318 +3000000,5.124303399864584,6.239440600015223,85.73076480003074,0.47364640003070235,5.336848200112581,1.5066193998791277,6.235969400033355,2.5797877999953926,0.1289702001027763,6.770450200047344 +4000000,7.4429126000963155,8.575669200252742,153.32299140002578,0.6475916000083088,7.508836399950087,2.0407237999141215,8.872534200083464,3.9519795999862253,0.14986819997429848,9.702906599920244 +5000000,10.792367999907583,12.09743200019002,241.63080279994756,0.8215247998945415,9.775965599901975,2.8635928000323476,11.81594120003283,5.384774999972433,0.18414179980754852,12.333123400155454 diff --git a/benchmark/benchmark_results_init.csv b/benchmark/benchmark_results_init.csv deleted file mode 100644 index 9a957eb..0000000 --- a/benchmark/benchmark_results_init.csv +++ /dev/null @@ -1,6 +0,0 @@ -size,init_sorted_set,init_sorted_containers -1000000,1.626862599980086,0.5701855999417603 -2000000,3.699613399989903,2.0838129998184742 -3000000,6.832762000150979,3.794983599986881 -4000000,9.501148399990052,3.467436399962753 -5000000,13.889628200046719,4.997745399922133 diff --git a/benchmark/build_graphs.rb b/benchmark/build_graphs.rb index f4a6166..c8523b0 100644 --- a/benchmark/build_graphs.rb +++ b/benchmark/build_graphs.rb @@ -9,6 +9,7 @@ # Prepare data arrays sizes = data["size"] operations = { + "initialize" => %w[initialize_sorted_set initialize_sorted_containers], "add" => %w[add_sorted_set add_sorted_containers], "include" => %w[include_sorted_set include_sorted_containers], "iteration" => %w[loop_sorted_set loop_sorted_containers], @@ -31,7 +32,7 @@ def create_graph(title, _sizes, data1, data2, labels, file_name) g.data("SortedContainers::SortedSet", data2) # X-axis labels - g.x_axis_label = "Number of operations" + g.x_axis_label = "Number of operations/elements" # Labels for x_axis g.labels = labels diff --git a/benchmark/build_graphs_init_only.rb b/benchmark/build_graphs_init_only.rb deleted file mode 100644 index a674d6f..0000000 --- a/benchmark/build_graphs_init_only.rb +++ /dev/null @@ -1,55 +0,0 @@ -# frozen_string_literal: true - -require "gruff" -require "csv" - -# Read data from CSV -data = CSV.read("benchmark_results_init.csv", headers: true, converters: :numeric) - -# Prepare data arrays -sizes = data["size"] -operations = { - "initialize" => %w[init_sorted_set init_sorted_containers] -} - -# Method to create and save a graph -# rubocop:disable Metrics/ParameterLists -def create_graph(title, _sizes, data1, data2, labels, file_name) - g = Gruff::Line.new - g.title = "#{title} performance" - - g.theme = Gruff::Themes::THIRTYSEVEN_SIGNALS - - # Set line colors - g.colors = %w[#ff6600 #3333ff] - - # Define data - g.data("C Implemented RB Tree", data1) - g.data("SortedContainers::SortedSet", data2) - - # X-axis labels - g.x_axis_label = "Number of operations" - - # Labels for x_axis - g.labels = labels - - # Formatting y-axis to ensure no scientific notation, may need to adjust if log scale creates issues - g.y_axis_label = "Time (seconds)" - - # Write the graph to a file - g.write(file_name) -end -# rubocop:enable Metrics/ParameterLists - -# Generate labels for x_axis, format numbers with commas -labels = {} -sizes.each_with_index do |size, index| - labels[index] = size.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse -end - -# Generate a graph for each operation -operations.each do |operation, keys| - puts "#{operation} #{keys}" - create_graph(operation, sizes, data[keys[0]], data[keys[1]], labels, - "#{operation.downcase}_performance_comparison.png") -end diff --git a/benchmark/delete_performance_comparison.png b/benchmark/delete_performance_comparison.png index d180a53..bfca917 100644 Binary files a/benchmark/delete_performance_comparison.png and b/benchmark/delete_performance_comparison.png differ diff --git a/benchmark/include_performance_comparison.png b/benchmark/include_performance_comparison.png index 16bc051..945cd9a 100644 Binary files a/benchmark/include_performance_comparison.png and b/benchmark/include_performance_comparison.png differ diff --git a/benchmark/init_performance_comparison.png b/benchmark/init_performance_comparison.png deleted file mode 100644 index e518a3a..0000000 Binary files a/benchmark/init_performance_comparison.png and /dev/null differ diff --git a/benchmark/initialize_performance_comparison.png b/benchmark/initialize_performance_comparison.png index b5f8ac9..61f94da 100644 Binary files a/benchmark/initialize_performance_comparison.png and b/benchmark/initialize_performance_comparison.png differ diff --git a/benchmark/initiation_performance_comparison.png b/benchmark/initiation_performance_comparison.png deleted file mode 100644 index 92f1869..0000000 Binary files a/benchmark/initiation_performance_comparison.png and /dev/null differ diff --git a/benchmark/iteration_performance_comparison.png b/benchmark/iteration_performance_comparison.png index 76af970..55736cd 100644 Binary files a/benchmark/iteration_performance_comparison.png and b/benchmark/iteration_performance_comparison.png differ