Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: C-style for loop

by zentara (Archbishop)
on Nov 15, 2008 at 19:18 UTC ( #723830=note: print w/replies, xml ) Need Help??


in reply to C-style for loop

The C-style loop can be useful in cases where you want to skip over some values, but for other reasons(possibly ease of remembering range) want to keep your range 0..100.
#!/usr/bin/perl for (my $i = 0; $i <= 100; $i += 5) { print($i, "\n"); } print "\n"; for (0..20){ print 5 * $_,"\n"; }
Someone good at setting up efficiency tests, may find one better than the other in terms of what is fastest.

I'm not really a human, but I play one on earth Remember How Lucky You Are

Replies are listed 'Best First'.
Re^2: C-style for loop
by zentara (Archbishop) on Nov 15, 2008 at 19:31 UTC
    Looks like Perl is faster in this case.
    #!/usr/bin/perl use warnings; use strict; use Benchmark; #Outputs: #Benchmark: timing 1000000 iterations of c-loop, perl-loop... # c-loop: 5 wallclock secs ( 5.64 usr + 0.02 sys = 5.66 CPU) @ 176678.45/s (n=1000000) # perl-loop: 3 wallclock secs ( 3.58 usr + 0.01 sys = 3.59 CPU) @ 278551.53/s (n=1000000) timethese(1000000, { 'c-loop' => sub { for (my $i = 0; $i <= 100; $i += 5) { #print("$i "); } }, 'perl-loop' => sub { for (0..20){ # print 5 * $_." "; } }, });

    I'm not really a human, but I play one on earth Remember How Lucky You Are

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://723830]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2021-02-28 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?