Module: AlgorithmDemo
- Included in:
- DiscreteMathTopics::PreliminaryMath
- Defined in:
- lib/modules/topics/demos/algorithm_demo.rb
Defined Under Namespace
Modules: Helpers
Class Method Summary collapse
Class Method Details
.run ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/modules/topics/demos/algorithm_demo.rb', line 48 def self.run n = 3000 movies = AlgorithmDemo::Helpers.generate_movies_list_of_size(n) AlgorithmDemo::Helpers.sort_all_movies hash = AlgorithmDemo::Helpers.generate_hash_for_movies(movies) treemap = AlgorithmDemo::Helpers.generate_treemap_for_movies(movies) sample_key = Movie.all[-1].clone.title sample_value = Movie.all[-1].clone.description puts "Please wait, processing Big-O Benchmarks..." spinner = Utils::Spinner.new threads = [] # Ruby Threads - https://ruby-doc.org/core-2.4.2/Thread.html spinner_thread = Thread.start { AlgorithmDemo.run_spinner(spinner) } benchmarking_thread = Thread.start { # O(1) - Benchmark Search/Lookup # References: # - https://ruby-doc.org/stdlib-2.4.2/libdoc/benchmark/rdoc/Benchmark.html # - https://stackoverflow.com/questions/1592649/examples-of-algorithms-which-has-o1-on-log-n-and-olog-n-complexities Benchmark.bm do |x| x.report { treemap.get(sample_key) } # RubyRBTreeMap x.report { hash[sample_key] } # Hash end # O(n) - Benchmark Delete Benchmark.bm do |x| x.report { treemap.delete(sample_key) } # RubyRBTreeMap x.report { hash.delete(sample_key) } # Hash end # O(n^2) - Benchmark Sort Benchmark.bm do |x| x.report { Algorithms::Sort.bubble_sort Movie.all } # Bubble Sort x.report { Movie.all.sort } # Object end # Stop spinner rotating spinner.stop_processing # Kill spinner thread Thread.kill(spinner_thread) } threads << spinner_thread threads << benchmarking_thread threads.each { |thread| thread.join } # AlgorithmDemo.show_status_of_threads(threads) end |
.run_spinner(spinner) ⇒ Object
38 39 40 |
# File 'lib/modules/topics/demos/algorithm_demo.rb', line 38 def self.run_spinner(spinner) spinner.start_processing end |
.show_status_of_threads(threads) ⇒ Object
42 43 44 45 46 |
# File 'lib/modules/topics/demos/algorithm_demo.rb', line 42 def self.show_status_of_threads(threads) threads.each_with_index { |thread, index| puts "Thread #{index} status: #{thread.status ? 'OPEN' : 'CLOSED'}" } end |