本帖最后由 523066680 于 2017-8-5 09:08 编辑
时间占用对比,用 Devel::NYTProf 模块分析效率
perl -d:NYTProf normal.pl
nytprofhtml
perl -d:NYTProf InlineC.pl
nytprofhtml
两份完整的测试代码:- use IO::Handle;
- STDOUT->autoflush(1);
-
- my $nums = "4109";
- my $guess = "0124";
- my $AB = "00";
-
- for ( 1 .. 100000 )
- {
- $AB = "00";
- bullcow($nums, $guess, \$AB);
- print "$AB\n";
- }
-
- sub bullcow
- {
- my ($nums, $guess, $AB) = @_;
- my ($A, $B) = (0, 0);
- my $t;
-
- for my $i ( 0 .. 3 )
- {
- if ( substr($nums, $i, 1) eq substr($guess, $i, 1) )
- {
- $A++;
- }
- else
- {
- $t = substr($guess, $i, 1);
- $B++ if ( $nums =~/$t/ );
- }
- }
-
- $$AB = "$A$B";
- }
复制代码 内联C的版本:- use Inline C;
- use IO::Handle;
- STDOUT->autoflush(1);
-
- my $nums = "4109";
- my $guess = "0124";
- my $AB = "00";
-
- for ( 1 .. 100000 )
- {
- $AB = "00";
- bullcow($nums, $guess, $AB);
- print "$AB\n";
- }
-
- __END__
- __C__
- void bullcow(char *stra, char *strb, char *AB)
- {
- int idx;
- char a = '0';
- char b = '0';
-
- for ( idx = 0; idx < 4; idx++ )
- {
- if ( stra[idx] == strb[idx] )
- a++;
- else
- if ( strchr(stra, strb[idx]) != 0 )
- {
- b++;
- }
- }
-
- AB[0] = a;
- AB[1] = b;
- }
复制代码
|