Created
December 3, 2025 01:21
-
-
Save shugo/0028538e723c3f46bdb219046ab5058b to your computer and use it in GitHub Desktop.
strip vs gsub benchmark
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
| voyager:tmp$ cat b.rb | |
| require "benchmark" | |
| TARGET = " \t\r\n\f\v\0" + "x" * 1024 + "\0 \t\r\n\f\v" | |
| Benchmark.bmbm do |x| | |
| x.report("strip") do | |
| 10000.times do | |
| TARGET.strip | |
| end | |
| end | |
| x.report("gsub") do | |
| 10000.times do | |
| TARGET.gsub(/\A\s+|\s+\z/, "") | |
| end | |
| end | |
| end | |
| voyager:tmp$ ruby b.rb | |
| Rehearsal ----------------------------------------- | |
| strip 0.004674 0.000938 0.005612 ( 0.005618) | |
| gsub 0.284665 0.001060 0.285725 ( 0.285733) | |
| -------------------------------- total: 0.291337sec | |
| user system total real | |
| strip 0.000953 0.001997 0.002950 ( 0.002950) | |
| gsub 0.288242 0.000002 0.288244 ( 0.288261) | |
| voyager:tmp$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment