Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2006
    Posts
    14
    Rep Power
    0

    Perl trim function to remove whitespace.. a faster way?


    Is there a faster way to write this code?

    Code:
    # Perl trim function to remove whitespace from the start and end of the string
    sub trim($)
    {
    	my $string = shift;
    	$string =~ s/^\s+//;
    	$string =~ s/\s+$//;
    	return $string;
    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2006
    Posts
    263
    Rep Power
    11
    You can use trim() ltrim() rtrim() str_replace()

    EDIT:

    my mistake.. I was thinking PHP
    Last edited by vietboy505; April 19th, 2006 at 05:14 PM. Reason: PHP
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Los Angeles
    Posts
    116
    Rep Power
    16
    Originally Posted by perl_coder
    Is there a faster way to write this code?

    Code:
    # Perl trim function to remove whitespace from the start and end of the string
    sub trim($)
    {
    	my $string = shift;
    	$string =~ s/^\s+//;
    	$string =~ s/\s+$//;
    	return $string;
    }
    What you have there is just fine. Don't go thinking that some exotic way to do this is going to be any better or faster. If there is I doubt that it's going to make any difference in the execution speed.

    Max

    Comments on this post

    • KevinADC agrees : this is the best and fastest way
  6. #4
  7. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    862
    my only recommendation is not to write it as a prototype but as a regular function/subroutine:

    Code:
    sub trim
    {
    	my $string = shift;
    	$string =~ s/^\s+//;
    	$string =~ s/\s+$//;
    	return $string;
    }
    but if you have some reason to code it as a prototype that's OK.
  8. #5
  9. C Neophyte.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    Melbourne Australia
    Posts
    405
    Rep Power
    46
    perl Code:
     
    sub trim {
        my($str) = shift =~ m!^\s*(.+?)\s*$!i;
        defined $str ? return $str : return '';
    }


    Can't think of a way to make it shorter?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Los Angeles
    Posts
    116
    Rep Power
    16
    Originally Posted by fuzzybunny
    perl Code:
     
    sub trim {
        my($str) = shift =~ m!^\s*(.+?)\s*$!i;
        defined $str ? return $str : return '';
    }


    Can't think of a way to make it shorter?
    It's not about making code elegant and short. It's about making it readable. We're not playing golf here. Keeping the code simple goes a longer way than trying to be clever.

    Comments on this post

    • Axweildr agrees : Golf reference, I like that
  12. #7
  13. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    862
    Originally Posted by fuzzybunny
    perl Code:
     
    sub trim {
        my($str) = shift =~ m!^\s*(.+?)\s*$!i;
        defined $str ? return $str : return '';
    }


    Can't think of a way to make it shorter?
    That will also be slower than the original code.
  14. #8
  15. C Neophyte.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    Melbourne Australia
    Posts
    405
    Rep Power
    46
    What's the determinant for speed? What's the best way to measure it? I know there are ways of calculating execution time.

    When he said 'short' I thought he meant concise. I couldn't get it down to a one-liner I think it's readable, but if it's slower I guess that's unforgiveable!



    For the record I have the original code with the two replace regexes in my object definitions.
  16. #9
  17. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    862
    You can use the Benchmark module to test code.
  18. #10
  19. No Profile Picture
    PerlGuy
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2001
    Posts
    720
    Rep Power
    42
    - dsb -
    Perl Guy
  20. #11
  21. 11
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jul 2001
    Location
    Lynn, MA
    Posts
    4,635
    Rep Power
    83
    Don't know if this'll be any faster, but you can operate on the string "in place," without copying it into memory in your subroutine.

    When you operate directly on the default array (aka "@_") that's passed into a subroutine, you operate *directly on the value you passed in, by reference.*

    Example:

    Code:
    #!/usr/bin/perl
    
    my @strings=('asdfl asdflj   ',
                 ' asdf sd sdf sdfsdf   ',
                 '         asdfasdfas            asdf',
                 'a      sdfsdf        ');
    
    foreach my $string(@strings){
        print "Original:\n|".$string."|\n";
        trim($string);
        print "Trimmed:\n|".$string."|\n\n";
    }
    
    sub trim{
        $_[0]=~s/^\s+//;
        $_[0]=~s/\s+$//;
        return;
    }
    begets:

    Code:
    Original:
    |asdfl asdflj   |
    Trimmed:
    |asdfl asdflj|
    
    Original:
    | asdf sd sdf sdfsdf   |
    Trimmed:
    |asdf sd sdf sdfsdf|
    
    Original:
    |         asdfasdfas            asdf|
    Trimmed:
    |asdfasdfas            asdf|
    
    Original:
    |a      sdfsdf        |
    Trimmed:
    |a      sdfsdf|
    as output.

    This, theoretically, should save you the extra work of allocating/destroying memory, which could be significant if you're operating on large strings. But it's all about the Benchmark.

    Comments on this post

    • ishnid agrees
    • Conundrum agrees
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    25
    Rep Power
    0
    I use this trim function for myself.

    Code:
    sub trim {
        $_[0] = (($_[0] =~ /^[ \t]*(.*?)[ \t]*$/s)[0]);
    }
  24. #13
  25. Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Posts
    1,137
    Rep Power
    173
    Originally Posted by exp101t
    I use this trim function for myself.

    Code:
    sub trim {
        $_[0] = (($_[0] =~ /^[ \t]*(.*?)[ \t]*$/s)[0]);
    }
    My tests show this method is 3 to 4 times slower than Hero's version....
  26. #14
  27. Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2004
    Location
    Sunny Southern California
    Posts
    2,687
    Rep Power
    862
    Originally Posted by exp101t
    I use this trim function for myself.

    Code:
    sub trim {
        $_[0] = (($_[0] =~ /^[ \t]*(.*?)[ \t]*$/s)[0]);
    }
    and the question is why?
  28. #15
  29. C Neophyte.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Location
    Melbourne Australia
    Posts
    405
    Rep Power
    46
    Originally Posted by KevinADC
    and the question is why?
    The answer is in DSB's docs... two search and replace statements are faster than a search extract return.

    The thing I don't like about exp101t's trim is that if your variable is undefined...
    Last edited by fuzzybunny; May 9th, 2006 at 03:00 AM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo