Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Regular expressions are expensive. I find a lot - actuially, most - of the things I'd do with RegExs in Perl can be replaced with string methods in Python.

They are not that expensive. In perl, you pay for executing opcodes. If you can do with one regex what would require 10 "fast" string manipulations, then the regex will nearly always be faster.

I imagine Python is similar; there is bookkeeping to be done every time you call a core string function. If you can condense the operation into one call into the core, then you save yourself the bookkeeping overhead.

Additionally, it's also common for Perl users to use RegExs to do things like modify markup languages, which is extremely fragile - use a tree data structure and paths. Python has even has element trees built it in recent versions.

This has nothing to do with Perl. You can write bad code and use bad techniques in any language. I'll bet a Google Code Search will reveal tons of hand-built parsers written in Python. Most people don't know there is a better way to solve a problem, so they use whatever they think of first.

Code should be written for the people who have to maintain it.

When you write code, expect that you will be the primary maintainer forever. You will spend more time reading your own code than anyone else will, so optimize it for yourself.

Your beholders value simple code rather you selfishly saving a few keystrokes.

If you can't read Perl, perhaps it's because you don't know Perl. I can't read Python. Why? Because I never bothered to learn it. Does that make it intrinsically "unreadable" or "unmaintainable"? No; it just means I am dumb. Don't blame the language for your own ignorance.



They are not that expensive.

And even if they were remarkably more expensive, I'd still take "has an awesome implementation of regexes" over "does not have an acceptable implementation of regexes, but does have a few limited string processing methods that are fast". Luckily for Pythonistas, Python does have a reasonable regex implementation (pretty much the same as Perl in most regards, though not as nice to use), so it's not a problem.

But, I'm baffled that someone would suggest that a language is better because you can use more limited tools that might be marginally faster than extremely powerful and flexible tools. And, I'm also a little concerned that the previous post seems unaware of Perls other string processing tools...regexes are not the only tool in the toolbox. Perl is a monster for text processing. Regexes are the teeth...but there are also claws and horns and a pointy tail. I think Perl 6 also has laser eyes and breathes fire, but that might just be a rumor.


> But, I'm baffled that someone would suggest that a language is better because you can use more limited tools that might be marginally faster than extremely powerful and flexible tools

Because Python has both, and so much data is already split on common delimiters, and having a fast way to handle them is awesome?


Because Python has both, and so much data is already split on common delimiters, and having a fast way to handle them is awesome?

I know, and I said so. But, so does Perl (one of several "fast ways" of processing text in Perl is to use regexes, but it's not the only way). That's why I'm confused that you seem to think you're making differentiating statements about the two languages. That's all.

I happen to like Python, I just don't think it makes sense to present Python as a better text processing language than Perl...when, by most measures, including performance, it probably is not.


If there's there are string methods in Perl, great - I'll check this out next time I'm working on some Perl.

Unfortunately - and this is a cultural problem more than a technical one - nobody ever seems to use them, instead favoring RegExs for everything.


> If you can do with one regex what would require 10 "fast" string manipulations, then the regex will nearly always be faster.

Agreed, but most of the time I only need one string manipulation, and from what I can tell, the code behind split and endswith etc. are faster than using RegExs in the same place. Recently I've had to convert something back to Python 1.5, which lacks string methods, but has regex's, and it's noticeably slower. >>It's also common for Perl users to use RegExs to do things like modify markup languages, which is extremely fragile - use a tree data structure and paths >This has nothing to do with Perl. It has something to do with Perl - Perl lacked these data types in the mid 90s Perl boom. So people who learnt Perl then see no problem with using what's always 'worked for them'. I agree it's nothing to do with the language as it stands now.

> When you write code, expect that you will be the primary maintainer forever.

Would you hire someone that said that to work on your startup?

>> Your beholders value simple code rather you selfishly saving a few keystrokes.

> If you can't read Perl, perhaps it's because you don't know Perl.

That's a troll. I can fix a problem with a blank editor and Perl, and I can read and debug most people's Perl. Not being able to understand someone's sloppily indented bracket abortion-code isn't because I don't 'know' the language.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: