3 ms·
Which method did you use to match the line in Ruby? I noticed String[/\b\w{15}\b/] performs way better than String.match(/\b\w{15}\b/).
by faebi 8y ago
Which method did you use to match the line in Ruby?
I noticed String[/\b\w{15}\b/] performs way better than String.match(/\b\w{15}\b/).
- cutler 8y agoThe Ruby code is: IO.foreach('logs1.txt') {|x| puts x if /\b\w{15}\b/.match? x }
- yxhuvud 8y agoWhat happens if you lift the regexp out of the block and reuse the same regexp instance?
- cutler 8y agoTried: regex = Regexp.new '\b\w{15}\b' IO.foreach('logs1.txt') {|x| puts x if regex.match? x } ... but performance and RAM usage were identical.
- JohnBooty 8y agoRuby's smart enough to freeze regexen by default. I think that landed around 2.0/2.1/2.2 pry(main)> 3.times { puts /i_am_a_reg_ex/.object_id } 70131978677720 70131978677720 70131978677720
- cutler 8y agoIt could be argued anyway that String[/\b\w{15}\b/] is not idiomatic.