Created
June 7, 2009 14:44
-
-
Save Narnach/125358 to your computer and use it in GitHub Desktop.
Benchmark to show explicit returns are slower than implicit returns.
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
| #!/usr/bin/env ruby | |
| # Benchmark explicit vs implicit returns. | |
| # | |
| # In a loop doing 5,000,000 implicit and the same explicit returns, | |
| # The following stats came back: | |
| # | |
| # user system total real | |
| # explicit return 2.040000 0.010000 2.050000 ( 2.074879) | |
| # implicit return 1.310000 0.010000 1.320000 ( 1.333344) | |
| # | |
| # So, besides using less code, implicit returns are actually quite a bit | |
| # faster than explicit returns. | |
| require 'benchmark' | |
| class Return | |
| def implicit | |
| 1 | |
| end | |
| def explicit | |
| return 1 | |
| end | |
| end | |
| Benchmark.bm do |x| | |
| r = Return.new | |
| n = 5_000_000 | |
| x.report('explicit return' ) { n.times { r.explicit } } | |
| x.report('implicit return' ) { n.times { r.implicit } } | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment