Do you know where your variables are? PerlMonks

### Comment on

 Need Help??

I believe

Benchmark knows.

```Benchmark: timing 1000000 iterations of iterative_c, iterative_p, recu
+rsive_c, recursive_p...
iterative_c:  1 wallclock secs ( 1.10 usr +  0.00 sys =  1.10 CPU) @ 9
+07441.02/s (n=1000000)
iterative_p: 19 wallclock secs (19.52 usr +  0.00 sys = 19.52 CPU) @ 5
+1234.76/s (n=1000000)
recursive_c:  1 wallclock secs ( 1.16 usr +  0.00 sys =  1.16 CPU) @ 8
+60585.20/s (n=1000000)
recursive_p: 36 wallclock secs (36.26 usr +  0.00 sys = 36.26 CPU) @ 2
+7576.32/s (n=1000000)
Rate recursive_p iterative_p recursive_c iterative_c
recursive_p  27576/s          --        -46%        -97%        -97%
iterative_p  51235/s         86%          --        -94%        -94%
recursive_c 860585/s       3021%       1580%          --         -5%
iterative_c 907441/s       3191%       1671%          5%          --
```use Inline 'C';

use Benchmark 'cmpthese';

cmpthese( 1_000_000, { iterative_c => sub{guess_it_c(4999,1,10000)},
recursive_c => sub{guess_rec_c(4999,1,10000)},
iterative_p => sub{guess_it_p(4999,1,10000)},
recursive_p => sub{guess_rec_p(4999,1,10000)},}
+);

sub guess_it_p {
my ( \$ans, \$lower, \$higher ) = @_;
my \$guess;
while(1) {
\$guess = int((\$lower + \$higher)/2);
last if \$ans == \$guess;
if ( \$guess > \$ans ) {
\$higher = \$guess -1;
}
else {
\$lower = \$guess +1;
}
}
return \$guess;
}

sub guess_rec_p {
my ( \$ans, \$lower, \$higher ) = @_;
my \$guess = int((\$lower + \$higher)/2);
if (\$ans == \$guess) {
return \$guess;
}
if ( \$guess > \$ans ) {
guess_rec_p( \$ans, \$lower, \$guess -1 );
}
else {
guess_rec_p( \$ans, \$guess +1, \$higher );
}
}

__END__
__C__
int guess_it_c( int ans, int lower, int higher ) {
int guess;
for(;;) {
guess = (int)((lower + higher)/2);
if ( ans == guess ) break;
if ( guess > ans )
higher = guess -1;
else
lower = guess +1;
}
return guess;
}

int guess_rec_c( int ans, int lower, int higher ) {
int guess;
guess = (int)((lower + higher)/2);
if (ans == guess) return guess;
if ( guess > ans ) {
guess_rec_c( ans, lower, guess -1 );
}
else {
guess_rec_c( ans, guess +1, higher );
}
}

cheers

tachyon

In reply to Re^3: Behold! The power of recursion. by tachyon
in thread Behold! The power of recursion. by DigitalKitty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-02-21 03:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When it is dark outside I am happiest to see ...

Results (274 votes). Check out past polls.

Notices?