Sha256: 1bd4e8492c55122276955900f40f0b191c34f506e4dbe0c279c3738427f42f65
Contents?: true
Size: 1.03 KB
Versions: 2
Compression:
Stored size: 1.03 KB
Contents
require "bundler/setup" require 'multi_string_replace' require 'benchmark' require 'pry-byebug' class String def mgsub(key_value_pairs=[].freeze) regexp_fragments = key_value_pairs.collect { |k,v| k } gsub( Regexp.union(*regexp_fragments)) do |match| key_value_pairs.detect{|k,v| k =~ match}[1] end end end body = File.read(File.join('spec', 'fixtures', 'test.txt')) replace = { 'Lorem' => 'XXXX', 'ipsum' => 'yyyyy', 'sapien' => 'zzzzzz', 'sed' => 'pppppppp', 'Fusce' => 'wwwwwwww', 'non' => 'NON', 'sit' => 'SIT', 'laoreet' => 'lllll', 'Cras' => 'uuuuuuuu', 'nunc' => 'eeeeeee', 'cursus' => '乧乨乩乪乫乬乭乮乯买乱乲乳乴乵乶乷乸乹乺乻乼乽乾乿', } File.write('replaced.txt', body.gsub(/(#{replace.keys.join('|')})/, replace)) File.write('replaced2.txt', MultiStringReplace.replace(body, replace)) Benchmark.bmbm do |x| x.report "multi gsub" do body.mgsub(replace.map { |k, v| [/#{k}/, v] } ) end x.report "MultiStringReplace" do MultiStringReplace.replace(body, replace) end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
multi_string_replace-0.1.1 | bin/benchmark.rb |
multi_string_replace-0.1.0 | bin/benchmark.rb |