os97673/regexp_m17n — pull request #28 by svyatov (2014-08-02 10:12:34+00:00)
Title: 3 different solutions with benchmark

So, if we agree to skip dummy encodings, there is basically 3 solutions at the time.

**Encode regular expression with string encoding**
```ruby
regex = Regexp.new '^.+$'.encode(str.encoding)
str =~ regex
```

**Convert string encoding to a compatible one, e.g. UTF-8**
```ruby
str.encode('UTF-8') =~ /^.+$/
```

**String unpack/pack solution by @kreeger**
```ruby
str.unpack('C*').pack('U*') =~ /^.+$/
```

And here is benchmark results for these solutions. The code is in `benchmark.rb` file.

Result for string length = 256
user system total real
Regex encode: 1.530000 0.010000 1.540000 ( 1.547422)
String encode: 1.270000 0.010000 1.280000 ( 1.271730)
String unpack/pack: 6.520000 0.110000 6.630000 ( 6.633206)

Result for string length = 1024
user system total real
Regex encode: 3.760000 0.030000 3.790000 ( 3.799044)
String encode: 4.330000 0.020000 4.350000 ( 4.344520)
String unpack/pack: 24.640000 0.520000 25.160000 ( 25.169441)

As you can see `unpack/pack` solution is the worst in terms of performance. String encoding solution is faster then regular expression, but till the string is small enough.

So we need to know how big our strings are gonna be. If you need universal solution - regular expression encoding is the best choice. If strings are small and performance is our number one concern - then string encoding.