Se siete interessati in termini di prestazioni, ho scritto un rapido punto di riferimento:
require 'benchmark'
require 'bigdecimal/math'
require 'active_support/core_ext/string/filters'
# Where "shakespeare" is the full text of The Complete Works of William Shakespeare...
puts 'Benchmarking shakespeare.scan(/\w+/).size x50'
puts Benchmark.measure { 50.times { shakespeare.scan(/\w+/).size } }
puts 'Benchmarking shakespeare.squish.scan(/\w+/).size x50'
puts Benchmark.measure { 50.times { shakespeare.squish.scan(/\w+/).size } }
puts 'Benchmarking shakespeare.split.size x50'
puts Benchmark.measure { 50.times { shakespeare.split.size } }
puts 'Benchmarking shakespeare.squish.split.size x50'
puts Benchmark.measure { 50.times { shakespeare.squish.split.size } }
I risultati:
Benchmarking shakespeare.scan(/\w+/).size x50
13.980000 0.240000 14.220000 (14.234612)
Benchmarking shakespeare.squish.scan(/\w+/).size x50
40.850000 0.270000 41.120000 (41.109643)
Benchmarking shakespeare.split.size x50
5.820000 0.210000 6.030000 ( 6.028998)
Benchmarking shakespeare.squish.split.size x50
31.000000 0.260000 31.260000 (31.268706)
In altre parole, squish
è lento con il Very Large stringhe ™. Oltre a questo, split
è più veloce (due volte più veloce se non si utilizza squish
).
Questo è esattamente quello che stavo cercando. Grazie. –
Cosa ne pensi di "aggiungere un po 'di rock-n-roll"? Ci sono tre parole qui mentre la tua variante ne troverà cinque. – IDBD
aggiunto anche il trattino. – YOU