Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: C vs perl

by Juerd (Abbot)
on Apr 28, 2002 at 12:36 UTC ( [id://162652]=note: print w/replies, xml ) Need Help??


in reply to C vs perl

Your example is not fair. In Perl, you use a regex to substitute, but in the C example, you don't use any regex at all. This isn't really a C versus Perl example, but more like a brute-force versus regex one.

If you want to compare languages, use a regex engine in your C example, and use the same substitution. Or, alternatively, write the Perl code without regexes. Of course you can comment on Perl having a very fast built-in regex engine, and you could give a long and short example of Perl code.

Please use equivalent examples. Regexes are in no way unique to Perl.

- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.

Replies are listed 'Best First'.
(All roads lead to Rome) Re: Re: C vs perl
by Ovid (Cardinal) on Apr 28, 2002 at 19:22 UTC

    Juerd wrote:

    If you want to compare languages, use a regex engine in your C example, and use the same substitution. Or, alternatively, write the Perl code without regexes.

    While I understand your point of view, I have to disagree. Different languages naturally lead to different solutions. A program is correct if I does exactly what it is supposed to do and nothing more. The implementation is almost an afterthought. Consider the following snippet.

    foreach my $alpha ( @alphas ) { foreach my $beta ( @betas ) { if ( $alpha eq $beta ) { # do something } } }

    A friend of mine uses that in job interviews. He tells the programmer that a project was moved into production only to discover that this snippet was consuming 20% of the run time, a fact not discovered in testing. My friend has two questions. First, what is the problem? Second, how do you fix it? Interestingly, he says, most programmers that he interviews do not see the problem. Of those who do, many cannot fix it.

    Here was my initial reaction.

    my %alpha; @alpha{ @alphas } = undef; # suppress void context warnings foreach my $beta ( @betas ) { if ( exists $alpha{ $beta } ) { # do something } }

    Now, that seems all well and good, but I am informed that many C programmers look at the problem and say "sort one list and do a binary search". It turns out to not be as fast as the above method, but it's much faster than the original code. Personally, I never would have though of the sort and binary search method. I don't think like a C programmer anymore (if I ever did). Someone from another language altogether might say "sort both lists and look for intersections." Depending on what you're really doing with the data, that answer might be acceptable too.

    To really compare languages, you have to let the them show how a problem would be solved "their way". If you tried to shoehorn C or Perl into the programming style of the other, what are you really comparing? Speed? Perl doesn't have pointers (shhh... no one mention Devel::Pointer), so some of the weird things that C programmers come up with using pointers just don't translate over. C doesn't (natively) have regular expressions or hashes. Typical C programmers probably wouldn't look for those solutions. That doesn't make them invalid, but trying to contrast the inherent abilities of different languages means they shouldn't be reduced to the lowest common denominator. If all roads lead to Rome, who cares what road I take?

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      UPDATED
      foreach my $alphabeta ( my $z; grep { $z = $_; grep { $z eq $_ } @alphas } @betas ){ # do something }
      Granted this still has the same problem as the initial code, but it's twice as fast and saves memory over your speedier (20xOriginal) solution. It also has the side-effect of effectively uniq-ing @betas. If you don't like that replace the outter grep with map.

      --
      perl -pew "s/\b;([mnst])/'$1/g"

Re: Re: C vs perl
by belg4mit (Prior) on Apr 28, 2002 at 18:50 UTC
    They are not unique to Perl, however they are a core feature of the language, bit of a difference there.

    I think the better option would be to instead use the suggested cleaned up Perl and C provided by others, and implement a "dumb" Perl version as well. Perhaps as if to demonstrate how "awkward" it would be.

    --
    perl -pew "s/\b;([mnst])/'$1/g"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://162652]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-03-28 21:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found