Last active
August 29, 2015 14:07
-
-
Save stefanosc/a262f46ca2e33bcd3819 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ProfileMethod | |
| attr_reader :test_methods, :results | |
| def initialize | |
| @test_array = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | |
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | |
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | |
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | |
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | |
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | |
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | |
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | |
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | |
| [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]] | |
| @test_methods = self.class.instance_methods.grep(/using_/) | |
| @results = Hash[@test_methods.map { |e| [e, 0] }] | |
| end | |
| def profile | |
| 100_000.times { | |
| @test_methods.shuffle.each do |method| | |
| @results[method] += self.send(method) | |
| end | |
| } | |
| @results.each do |k,v| | |
| puts "#{k} took in total #{v} seconds to run 100,000 times" | |
| end | |
| end | |
| def using_each | |
| t = Time.now | |
| b = [] | |
| @test_array.each {|e| b << e[2]} | |
| b | |
| t1 = Time.now | |
| t1 - t | |
| end | |
| def using_transpose | |
| t = Time.now | |
| @test_array.transpose[2] | |
| t1 = Time.now | |
| t1 - t | |
| end | |
| def using_map | |
| t = Time.now | |
| @test_array.map { |e| e[2] } | |
| t1 = Time.now | |
| t1 - t | |
| end | |
| def using_times | |
| t = Time.now | |
| b = [] | |
| @test_array.size.times { |n| b << @test_array[n][2] } | |
| b | |
| t1 = Time.now | |
| t1 - t | |
| end | |
| end | |
| profile_instance = ProfileMethod.new | |
| profile_instance.profile | |
| puts "-" * 70 | |
| profile_instance.profile | |
| puts "-" * 70 | |
| profile_instance.profile | |
| puts "-" * 70 | |
| profile_instance.profile | |
| puts "-" * 70 | |
| profile_instance.profile | |
| puts "-" * 70 | |
| =begin | |
| using_each took in total 0.15058900000010217 seconds to run 100,000 times | |
| using_transpose took in total 0.41107199999939986 seconds to run 100,000 times | |
| using_map took in total 0.13535200000009373 seconds to run 100,000 times | |
| using_times took in total 0.17524400000011414 seconds to run 100,000 times | |
| ---------------------------------------------------------------------- | |
| using_each took in total 0.3011159999995495 seconds to run 100,000 times | |
| using_transpose took in total 0.8274969999973887 seconds to run 100,000 times | |
| using_map took in total 0.2726849999998221 seconds to run 100,000 times | |
| using_times took in total 0.33535299999938445 seconds to run 100,000 times | |
| ---------------------------------------------------------------------- | |
| using_each took in total 0.46122299999780403 seconds to run 100,000 times | |
| using_transpose took in total 1.2426729999946375 seconds to run 100,000 times | |
| using_map took in total 0.40910999999767317 seconds to run 100,000 times | |
| using_times took in total 0.5007279999979476 seconds to run 100,000 times | |
| ---------------------------------------------------------------------- | |
| using_each took in total 0.6118289999977313 seconds to run 100,000 times | |
| using_transpose took in total 1.6469549999914233 seconds to run 100,000 times | |
| using_map took in total 0.5441779999968219 seconds to run 100,000 times | |
| using_times took in total 0.6630769999975049 seconds to run 100,000 times | |
| ---------------------------------------------------------------------- | |
| using_each took in total 0.7590519999982025 seconds to run 100,000 times | |
| using_transpose took in total 2.047383999988493 seconds to run 100,000 times | |
| using_map took in total 0.6817219999984199 seconds to run 100,000 times | |
| using_times took in total 0.8306449999970401 seconds to run 100,000 times | |
| ---------------------------------------------------------------------- | |
| =end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment