![]() |
![]() |
10 Einträge, 1 Seite |
1
2
3
4
my @chars = split //, $string;
my $h = ();
$h{$_}++ foreach @chars;
print "True" if $h{k} >= 2 || $h{r} == 2 && $h{w} == 2 || $h{k} == 1 && $h{r} == 1 && $h{w} == 2;
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
use strict;
use warnings;
use Benchmark;
my @strings;
my @chars = 'a'..'z';
for (1..10) {
my $len = int ( rand(100)) + 10;
my $string;
for(1 .. $len) {
$string .= $chars[int ( rand(@chars) )];
}
push @strings, $string;
}
print scalar @strings, " strings created !\n";
my $iterations = 10000;
my $count = 0;
timethese(
$iterations,
{
"esskar" => sub {
foreach (@strings) {
my @chars = split //, $_;
my %h = ();
$h{$_}++ foreach @chars;
$count += is_true($h{k}, $h{r}, $h{w});
}
}
}
);
print "esskar: $count\n";
$count = 0;
timethese(
$iterations,
{
"pq" => sub {
foreach (@strings) {
$count += is_true( tr/k//, tr/r//, tr/w// );
}
}
}
);
print "pq: $count\n";
sub is_true {
my ($k, $r, $w) = @_;
$k ||= 0;
$r ||= 0;
$w ||= 0;
return $k >= 2 || $r == 2 && $w == 2 || $k == 1 && $r == 1 && $w == 2;
}
^Z
10 strings created !
Benchmark: timing 10000 iterations of esskar...
esskar: 15 wallclock secs (14.47 usr + 0.00 sys = 14.47 CPU) @ 691.13/s (n=10000)
esskar: 60000
Benchmark: timing 10000 iterations of pq...
pq: 0 wallclock secs ( 0.34 usr + 0.00 sys = 0.34 CPU) @ 29069.77/s (n=10000)
(warning: too few iterations for a reliable count)
pq: 60000
![]() |
![]() |
10 Einträge, 1 Seite |