diff options
| author | Kouhei Sutou <[email protected]> | 2016-07-27 13:58:07 +0900 |
|---|---|---|
| committer | Kouhei Sutou <[email protected]> | 2016-07-27 13:58:07 +0900 |
| commit | 6bb0775d7603d020e64fb68fbc78e184745e8b0d (patch) | |
| tree | 6bc46c3b2c8692bb8b0d0761bcc5c2080cbe3bac /mrbgems/mruby-string-ext | |
| parent | 3757b16dab96c687429a9cec7c2b498218ef0e27 (diff) | |
| download | mruby-6bb0775d7603d020e64fb68fbc78e184745e8b0d.tar.gz mruby-6bb0775d7603d020e64fb68fbc78e184745e8b0d.zip | |
Reduce needless Array generation in some String methods
Here are some benchmarks:
each_char:
# /tmp/each_char.rb
a = "a" * 1000000
a.each_char do |x|
end
Without this change:
% time bin/mruby /tmp/each_char.rb
bin/mruby /tmp/each_char.rb 1.07s user 0.02s system 99% cpu 1.088 total
With this change:
% time bin/mruby /tmp/each_char.rb
bin/mruby /tmp/each_char.rb 0.52s user 0.01s system 99% cpu 0.530 total
2 times faster with this change.
codepoints:
# /tmp/codepoints.rb
a = "a" * 1000000
a.codepoints do |x|
end
Without this change:
% time bin/mruby /tmp/codepoints.rb
bin/mruby /tmp/codepoints.rb 1.16s user 0.05s system 99% cpu 1.216 total
With this change:
% time bin/mruby /tmp/codepoints.rb
bin/mruby /tmp/codepoints.rb 0.56s user 0.02s system 99% cpu 0.589 total
Diffstat (limited to 'mrbgems/mruby-string-ext')
| -rw-r--r-- | mrbgems/mruby-string-ext/mrblib/string.rb | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/mrbgems/mruby-string-ext/mrblib/string.rb b/mrbgems/mruby-string-ext/mrblib/string.rb index 7e65eb6b2..a291b8207 100644 --- a/mrbgems/mruby-string-ext/mrblib/string.rb +++ b/mrbgems/mruby-string-ext/mrblib/string.rb @@ -354,7 +354,7 @@ class String def chars(&block) if block_given? - self.split('').map do |i| + self.split('').each do |i| block.call(i) end self @@ -366,7 +366,7 @@ class String def each_char(&block) return to_enum :each_char unless block - split('').map do |i| + split('').each do |i| block.call(i) end self @@ -376,7 +376,7 @@ class String len = self.size if block_given? - self.split('').map do|x| + self.split('').each do|x| block.call(x.ord) end self |
