Created
January 29, 2016 09:36
-
-
Save jodosha/42fae9b05c1a819323f3 to your computer and use it in GitHub Desktop.
Ruby: String#chars.each vs String#each_char
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 | |
| require 'benchmark/ips' | |
| GC.disable | |
| STRING = ("a" * 100).freeze | |
| Benchmark.ips do |x| | |
| x.report('chars.each') { STRING.chars.each {|c| c.upcase} } | |
| x.report('each_char') { STRING.each_char {|c| c.upcase} } | |
| x.compare! | |
| end | |
| __END__ | |
| Result: | |
| Calculating ------------------------------------- | |
| chars.each 4.238k i/100ms | |
| each_char 5.022k i/100ms | |
| ------------------------------------------------- | |
| chars.each 48.040k (±46.1%) i/s - 194.948k | |
| each_char 62.546k (±42.9%) i/s - 210.924k in 5.262923s | |
| Comparison: | |
| each_char: 62546.1 i/s | |
| chars.each: 48039.9 i/s - 1.30x slower | |
| Ruby: | |
| ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin14] | |
| Hardware: | |
| Hardware Overview: | |
| Model Name: MacBook Pro | |
| Model Identifier: MacBookPro12,1 | |
| Processor Name: Intel Core i7 | |
| Processor Speed: 3,1 GHz | |
| Number of Processors: 1 | |
| Total Number of Cores: 2 | |
| L2 Cache (per Core): 256 KB | |
| L3 Cache: 4 MB | |
| Memory: 16 GB | |
| Boot ROM Version: MBP121.0167.B15 | |
| SMC Version (system): 2.28f7 | |
| Software: | |
| System Software Overview: | |
| System Version: OS X 10.11.3 (15D21) | |
| Kernel Version: Darwin 15.3.0 | |
| Time since boot: 23:27 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment