开发者

How to get biggest char from string

开发者 https://www.devze.com 2023-04-01 15:18 出处:网络
How do I get the char with the biggest ascii value from a string? $str = \'abcb\' # extract biggest char somehow

How do I get the char with the biggest ascii value from a string?

$str = 'abcb'
# extract biggest char somehow
# as a resu开发者_如何学JAVAlt $char contains "c"


use List::Util 'maxstr';

my $maxChar = maxstr split //, 'abcb';  # 'c'


The trick with these things is to know your other constraints. How often will you need to do this in your program and how big is the string? With a single shot short string, you can get away with waste. If you have to do it repeatedly or for large strings, that waste matters much more.

I test it for 5 CPU seconds on (Mac Pro, OS X.6.8, Perl 5.14.1) for increasing orders of magnitude in string length:

                  +-------------------------------------------------+
                  |                    Iterations                   |
+-----------------+---------+---------+---------+---------+---------+
| Sub name        |   10**1 |   10**2 |   10**3 |   10**4 |   10**5 |
+-----------------+---------+---------+---------+---------+---------+
| iterate_foreach |  983840 |  141997 |   16333 |    1686 |     168 |
| iterate_for     | 1041713 |  146608 |   15370 |    1538 |     155 |
| sorter          | 1194656 |  120608 |   10490 |     888 |      82 |
| maxstr          | 1505280 |  180923 |   19764 |    2120 |     202 |
| control         | 8019246 | 8019246 | 7719377 | 5041132 |  710027 |
+-----------------+---------+---------+---------+---------+---------+

List::Util's maxstr dominates in all cases. For the first order, a string of 10 characters, sorter does okay. After that, sorter gets worse and worse, as you should expect. The iterate_foreach and iterate_for degrade much better.

The code:

use 5.010;
use strict;
use warnings;

use Benchmark;
use List::Util;

my $count = -5;

foreach my $order ( 1 .. 5 ) {
    say "-" x 50, "\nTiming for order $order";

    my $string = make_string( $order );

    timethese( $count, {
        'sorter'          => sub { my $c = sorter( $string ) },
        'maxstr'          => sub { my $c = maxstr( $string ) },
        'iterate_for'     => sub { my $c = iterate_for( $string ) },
        'iterate_foreach' => sub { my $c = iterate_foreach( $string ) },
        'control'         => sub { my $c = control( $string ) },
    });
    }

sub make_string {
    my $order = shift;
    my $string;

    for( my $i = 0; $i < 10**$order; $i++ ) {
        $string .= chr int rand 256;
        }

    $string;
    }

sub sorter {
    my $str = shift;
    my $char = (sort split //, $str)[-1];
    }

sub maxstr {
    my $str = shift;
    my $char = List::Util::maxstr( split //, $str );
    }

sub iterate_for {
    my $str = shift;
    my $length = length $str;
    my $max = '';
    for( my $i = 0; $i < $length; $i++ ){
        my $chr = substr( $str, $i, 1 );
        $max = $chr if $chr gt $max;
        }
    return $max;
    }

sub iterate_foreach {
    my $str = shift;
    my $max = '';
    foreach ( split //, $str ){
        $max = $_ if $_ gt $max;
        }
    return $max;
    }

sub control {
    my $str = shift;
    return 'a';
    }


$str = 'abcb';
@a = sort split(//, $str);
$char = pop(@a);
0

精彩评论

暂无评论...
验证码 换一张
取 消