Si usted está interesado en el rendimiento, escribí un punto de referencia rápida:
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 } }
Los resultados:
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)
En otras palabras, squish
es lento con el Very Large Strings ™. Aparte de eso, split
es más rápido (el doble de rápido si no está usando squish
).
eso es exactamente lo que estaba buscando. Gracias. –
¿Qué tal "agregar algo de rock-n-roll"? Aquí hay tres palabras, mientras que su variante encontrará cinco. – IDBD
añadido guión también. – YOU