在Perl程序中显示进度条之使用Term::ProgressBar模块

  • A really simple use

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    
    #!/usr/bin/perl -w
     
    use Term::ProgressBar 2.00;
    use constant MAX =100_0000;
     
    my $progress = Term::ProgressBar-new(MAX);
     
    for ( 0 .. MAX ) {
       my $is_power = 0;
       for ( my $i = 0 ; 2**$i= $_ ; $i++ ) {
           $is_power = 1
             if 2**$i == $_;
       }
     
       if ($is_power) {
           $progress-》update($_);
       }
    }
  • A smoother bar update

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    #!/usr/bin/perl -w
     
    use Term::ProgressBar 2.00;
     
    my $max = 100000;
     
    my $progress = Term::ProgressBar-new($max);
     
    for ( 0 .. $max ) {
       my $is_power = 0;
       for ( my $i = 0 ; 2**$i= $_ ; $i++ ) {
           $is_power = 1
             if 2**$i == $_;
       }
     
       $progress-》update($_);
    }

  • A (much) more efficient update

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    
    #!/usr/bin/perl -w
     
    use Term::ProgressBar 2.00;
     
    my $max = 1000000;
     
    my $progress =
     Term::ProgressBar-new( { name ='Powers', count =$max, remove =1 } );
    $progress-》minor(0);
    my $next_update = 0;
     
    for ( 0 .. $max ) {
       my $is_power = 0;
       for ( my $i = 0 ; 2**$i= $_ ; $i++ ) {
           $is_power = 1
             if 2**$i == $_;
       }
     
       $next_update = $progress-》update($_)
         if $_= $next_update;
    }
    $progress-》update($max)
     if $max= $next_update;
  • Using Completion Time Estimation

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    
    #!/usr/bin/perl -w
     
    use Term::ProgressBar 2.00;
     
    my $max = 1000000;
     
    my $progress = Term::ProgressBar-new(
       {
           name  ='Powers',
           count =$max,
           ETA   ='linear',
       }
    );
    $progress-》max_update_rate(1);
    my $next_update = 0;
     
    for ( 0 .. $max ) {
       my $is_power = 0;
       for ( my $i = 0 ; 2**$i= $_ ; $i++ ) {
           if ( 2**$i == $_ ) {
               $is_power = 1;
               $progress-》message( sprintf "Found %8d to be 2 ** %2d", $_, $i );
           }
       }
     
       $next_update = $progress-》update($_)
         if $_$next_update;
    }
    $progress-》update($max)
     if $max= $next_update;
  • Simple code

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    #!/usr/bin/perl -w
     
    use Term::ProgressBar 2.00;
     
    my $max = 100000;
     
    my $progress = Term::ProgressBar-new( { count =$max } );
    my $num = 0;
    for ( 1 .. $max ) {
       $progress-》update( ++$num );
    }