azumakuniyuki / perl-benchmark-collection

My perl benchmark scripts
4 stars 1 forks source link

Transliteration and substitution is not the same #1

Open vsespb opened 11 years ago

vsespb commented 11 years ago

https://github.com/azumakuniyuki/perl-benchmark-collection/blob/master/operator/s-vs-y.pl#L16

s{::}{/}g

is not same as

y{:}{/}s

'y' replaces each ':' with '/', while 's' replaces '::' with one character '/'

http://perldoc.perl.org/functions/y.html

azumakuniyuki commented 11 years ago

You are right... these are not equivalent. But I wrote that code for accomplishing the purpose which get a module path from a module name. Do you have a better idea ?

vsespb commented 11 years ago

I think for benchmarking need to compare functions which return same result for same arguments, so need compare 'y//' with 's//' code only if 's//' replaces one character.

It's not really important if those functions called with some useful arguments and do real job.

If you want useful code example - you can, for example, replace '/' with '\' in filenames to convern Unix path to Windows path.

Or you can just replace s{::}{/}g with s{:}{/}g. It will perform same way as tr// - replace only one character (filenames with two slashes are still valid)