l i n u x - u s e r s - g r o u p - o f - d a v i s
Next Meeting:
July 7: Social gathering
Next Installfest:
Latest News:
Jun. 14: June LUGOD meeting cancelled
Page last updated:
2005 Jan 07 14:38

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] perl - benchmark module
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [vox-tech] perl - benchmark module

On Thu, Jan 06, 2005 at 12:07:25PM -0500, Peter Jay Salzman wrote:
> I spent a minute playing with the benchmarking module and found some
> interesting stuff I thought others would find interesting too.
> tr versus regex
> ===============
> Each piece of code was run 400,000 times and the "final time" is the
> total time divided by 400,000.
> This code executes 412,371 times per second:
>    $string = 'hello';
>    $string =~ tr/hello/olleh/;

$string is now "olllh";

> This code executes 180,995 times per second:
>    $string = 'hello';
>    $string =~ s/hello/olleh/;

$string is now "olleh";

These are different operations.  I'll let you look into why.

> tr is clearly faster than regex.  At first, I was surprised that
> single character replacements took longer than word replacements, but
> upon reconsidering it, I think it's certainly plausible.

I think this is what you would expect.  Transliterations are not as
smart as regular expressions.  The tr/// operator simply replaces one
character set with another, character by character.  The s/// operator
compiles the entire string and executes it in the regex engine.

What happens if you precompile the string with the qr// operator?

vox-tech mailing list

LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
EDGE Tech Corp.
For donating some give-aways for our meetings.