Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Why doesnt ths work ??
#!/usr/bin/perl use Test::Simple tests => 1; use Test::Benchmark; is_faster(1,sub {$a=9; print "enter a name \n"; $b=<stdin>; print "$b\n";},sub { print "give input"; $b=<stdin>; chomp($b) ; print "$b \n";});
O/p is 1..1 Name "main::a" used only once: possible typo at bench.pl line 6. enter a name rr rr (warning: too few iterations for a reliable count) give input22 22 (warning: too few iterations for a reliable count) not ok 1 # Failed test (/usr/lib/perl5/5.6.0/Test/Benchmark.pm at line 76) # code1 was not faster than code 2 # 1 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) # 5 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) # Looks like you failed 1 tests of 1.

20040722 Edit by castaway: Changed title from 'Help', added code tags

Replies are listed 'Best First'.
Re: Problem using Test::Simple
by reneeb (Chaplain) on Jul 22, 2004 at 10:54 UTC
    It will not work, because you want to test, which sub is faster with one iteration. In the documentation of Test::Benchmark it is recommended to use at least 10000 iterations. try this one (due to the Test::Benchmark-docu):
    #!/usr/bin/perl use Test::More tests => 1; use Test::Benchmark; is_faster(10000,sub {$a=9; print "enter a name \n"; $b=<STDIN>; print "$b\n";}, sub { print "give input"; $b=<STDIN>; chomp($b) ; print "$b \n";},"res1 is faster than res2");
    Name "main::a" used only once: possible typo at bench.pl line 6.
    You just define and initialise this variable so perl givs you a warning!
Re: Problem using Test::Simple
by beable (Friar) on Jul 22, 2004 at 11:06 UTC

    It looks to me like it does "work". You'd have to say what you were expecting, and how the output differs from what you wanted to see.

    The first warning about "main::a" being used only once, is because in the first subroutine you have the line $a=9; but $a is never used again. This is a standard Perl warning, because using a variable only once is often a sign of a typo, in that you spelled a variable name wrong. If you put use warnings; use strict at the start of your program, it will greatly reduce errors from this.

    As for the warnings about "too few iterations", if you look at the documentation for Test::Benchmark, you'll see that the first argument to is_faster is the number of times to run each sub. Once is clearly not enough iterations to determine which is faster. Secondly, it is rather pointless to be timing code which is waiting on user input. If you type quickly for "enter a name" and type slowly for "give input", your test will pass, and vice-versa.

Re: Problem using Test::Simple
by integral (Hermit) on Jul 22, 2004 at 10:52 UTC

    The first warning is because on the third line of the main bit of code you've used $a, but that's the only place you've used it. You can find out more about perl's warnings by checking in perldiag or using the diagnostics pragma.

    The problem with the benchmarking part of the code looks like two things: a) you're using 'stdin' and not 'STDIN'; b) you're only looping once. The first is because the four builtin filehandles (STDIN, STDOUT, STDERR, and DATA) are all in uppercase not lowercase, so your code perhaps should be $b = <STDIN>. The warning is produced by Test::Benchmark because you've only specified one iteration to is_faster which means the code executes faster than the resolution of the system clock.

    --
    integral, resident of freenode's #perl
    

      stderr is an obsolate alias for STDERR. It should work, but give a warning.