File: demo-code.pl

package info (click to toggle)
libdevel-nytprof-perl 5.06%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,396 kB
  • ctags: 1,413
  • sloc: perl: 4,905; ansic: 101; sh: 54; makefile: 17
file content (78 lines) | stat: -rw-r--r-- 1,377 bytes parent folder | download | duplicates (6)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use strict 0.1;   # use UNIVERSAL::VERSION
use English;      # demo detection of $& et al
use Benchmark;
use File::Find;

my $count = shift || 100;
my $do_io = shift || (not -t STDIN);

sub add {
    $a = $a + 1;
    foo();
}

sub foo {
    1;
    for (1..1000) {
        ++$a;
        ++$a;
    }
    1;
}

BEGIN { add() }
BEGIN { add() }

sub inc {
    1;
    # call foo and then execute a slow expression *in the same statement*
    # With all line profilers except NYTProf, the time for that expression gets
    # assigned to the previous statement, i.e., the last statement executed in foo()!
    # XXX this doesn't seem to be slow in 5.12+ - need a better example
    foo() && 'aaaaaaaaaaa' =~ /((a{0,5}){0,5})*[c]/;

    1;
}

timethese( $count, {
    add => \&add,
    bar => \&inc,
});

END {
    warn "ENDING\n";
    add()
}


# --- recursion ---

sub fib {
    my $n = shift;
    return $n if $n < 2;
    fib($n-1) + fib($n-2);
}
fib(7);

# --- File::Find ---

sub wanted {
    return 1;
}

find( \&wanted, '.');


# --- while with slow conditional ---

if ($do_io) {
    print "Enter text. Enter empty line to end.\n" if -t STDIN;
    # time waiting for the second and subsequent inputs
    # should get assigned to the condition statement
    # not the last statement executed in the loop
    while (<>) {
        chomp;
        last if not $_;
        1;
    }
}