Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 676281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:54:38+00:00 2026-05-14T00:54:38+00:00

I’ve done a small experiment as will be shown below and it looks like

  • 0

I’ve done a small experiment as will be shown below and it looks like that a while loop is faster than a for loop in Perl. But since the experiment was rather crude, and the subject might be a lot more complicated than it seems, I’d like to hear what you have to say about this.
Thanks as always for any comments/suggestions 🙂

In the following two small scripts, I’ve tried while and for loops separately to calculate the factorial of 100,000. The one that has the while loop took 57 minutes 17 seconds to finish while the for loop equivalent took 1 hour 7 minutes 54 seconds.

Script that has while loop:

use strict;
use warnings;
use bigint;

my $now = time;

my $n = shift;
my $s = 1;

while(1){
$s *= $n;
$n--;
last if $n==2;
}

print $s*$n;
$now = time - $now;
printf("\n\nTotal running time: %02d:%02d:%02d\n\n", int($now / 3600),
            int(($now % 3600) / 60), int($now % 60));

Script that has for loop:

use strict;
use warnings;
use bigint;

my $now = time;

my $n =shift;
my $s=1;

for (my $i=2; $i<=$n;$i++) {
$s = $s*$i;
}

print $s;
$now = time - $now;
printf("\n\nTotal running time: %02d:%02d:%02d\n\n", int($now / 3600),
            int(($now % 3600) / 60), int($now % 60));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T00:54:39+00:00Added an answer on May 14, 2026 at 12:54 am

    The loops are not equivalent, and you are primarily thrashing the bigint package and it has nothing to do with for vs while per se.

    The while loop uses the notation ‘$s *= $i‘ but the for loop uses ‘$s = $s * $i‘.
    It is simple enough to demonstrate that these are not identical. Also, one loop counts up; the other counts down. This affects how big the numbers to be multiplied are. It is a second order effect – but not completely negligible.

    [Update: revised to show just one version of the code, with sub-second timings. There is room to think that the printing should be excluded from the timing calculations; that makes things messier though, so I haven’t bothered. I have fixed the bug in the previous version: loop 4 was the same as loop 3 – now it isn’t. I’ve also tarted up the output formatting (though the sub-second handling could be improved – an exercise for the reader), and there is better ‘progress reporting’.]

    The timing results on a Mac Mini (Snow Leopard 10.6.2) were:

    Count up   $s *= $i:      00:00:12.663337
    Count up   $s  = $s * $i: 00:00:20.686111
    Count down $s *= $i:      00:00:14.201797
    Count down $s  = $s * $i: 00:00:23.269874
    

    The script:

    use Time::HiRes qw(gettimeofday);
    use strict;
    use warnings;
    use bigint;
    use constant factorial_of => 13000;
    
    sub delta_t
    {
        my($tag, $t1, $t2) = @_;
        my($d) = int($t2 - $t1);
        my($f) = ($t2 - $t1) - $d;
        my($s) = sprintf("%.6f", $f);
        $s =~ s/^0//;
        printf "%-25s %02d:%02d:%02d%s\n",
               $tag, int($d/3600), int(($d % 3600) / 60), int($d % 60), $s;
    }
    
    my $t1 = gettimeofday;
    
    {
        my $n = factorial_of;
        my $s = 1;
        for (my $i = 2; $i <= $n; $i++)
        {
            $s *= $i;
        }
        print "$s\n: Loop 1\n";
    }
    
    my $t2 = gettimeofday;
    delta_t('Count up   $s *= $i:',      $t1, $t2);
    
    {
        my $n = factorial_of;
        my $s = 1;
        for (my $i = 2; $i <= $n; $i++)
        {
            $s = $s * $i;
        }
        print "$s\n: Loop 2\n";
    }
    
    my $t3 = gettimeofday;
    delta_t('Count up   $s *= $i:',      $t1, $t2);
    delta_t('Count up   $s  = $s * $i:', $t2, $t3);
    
    {
        my $n = factorial_of;
        my $s = 1;
        for (my $i = $n; $i > 1; $i--)
        {
            $s *= $i;
        }
        print "$s\n: Loop 3\n";
    }
    
    my $t4 = gettimeofday;
    delta_t('Count up   $s *= $i:',      $t1, $t2);
    delta_t('Count up   $s  = $s * $i:', $t2, $t3);
    delta_t('Count down $s *= $i:',      $t3, $t4);
    
    {
        my $n = factorial_of;
        my $s = 1;
        for (my $i = $n; $i > 1; $i--)
        {
            $s = $s * $i;
        }
        print "$s\n: Loop 4\n";
    }
    
    my $t5 = gettimeofday;
    delta_t('Count up   $s *= $i:',      $t1, $t2);
    delta_t('Count up   $s  = $s * $i:', $t2, $t3);
    delta_t('Count down $s *= $i:',      $t3, $t4);
    delta_t('Count down $s  = $s * $i:', $t4, $t5);
    

    And here’s a much more compact version of the code above, extended to test ‘while’ loops as well as ‘for’ loops. It also deals with most of the timing issues. The only thing that isn’t ideal (to me) is that it uses a couple of global variables, and I scrunched the code in the code refs slightly so it all fits on one line without triggering a scroll bar (on my display, anyway). Clearly, with a bit more work, the testing could be wrapped up into an array, so that the testing would be done iteratively – a loop through the array running the timer function on the information in the array. Etc…it’s a SMOP – Simple Matter of Programming. (It prints the MD5 hash of the factorial, rather than the factorial itself, because it is easier to compare the results, etc. It did point out a couple of errors as I was refactoring the code above. Yes, MD5 is not secure – but I’m not using it for security; just to spot unintentional changes.)

    use Time::HiRes qw(gettimeofday);
    use Digest::MD5 qw(md5_hex);
    use strict;
    use warnings;
    use bigint;
    use constant factorial_of => 13000;
    
    my ($s, $i);
    
    my $l1 = sub {my($n) = @_; for ($i = 2;  $i <= $n; $i++) { $s *= $i;     }};
    my $l2 = sub {my($n) = @_; for ($i = 2;  $i <= $n; $i++) { $s = $s * $i; }};
    my $l3 = sub {my($n) = @_; for ($i = $n; $i > 1;   $i--) { $s *= $i;     }};
    my $l4 = sub {my($n) = @_; for ($i = $n; $i > 1;   $i--) { $s = $s * $i; }};
    my $l5 = sub {my($n) = @_; $i = 2;  while ($i <= $n) { $s *= $i;     $i++; }};
    my $l6 = sub {my($n) = @_; $i = 2;  while ($i <= $n) { $s = $s * $i; $i++; }};
    my $l7 = sub {my($n) = @_; $i = $n; while ($i > 1)   { $s *= $i;     $i--; }};
    my $l8 = sub {my($n) = @_; $i = $n; while ($i > 1)   { $s = $s * $i; $i--; }};
    
    sub timer
    {
        my($n, $code, $tag) = @_;
        my $t1 = gettimeofday;
        $s = 1;
        &$code(factorial_of);
        my $t2 = gettimeofday;
        my $md5 = md5_hex($s);
        printf "Loop %d: %-33s %09.6f (%s)\n", $n, $tag, $t2 - $t1, $md5;
    }
    
    my $count = 1;
    timer($count++, $l1, 'for   - Count up   $s *= $i:');
    timer($count++, $l2, 'for   - Count up   $s  = $s * $i:');
    timer($count++, $l3, 'for   - Count down $s *= $i:');
    timer($count++, $l4, 'for   - Count down $s  = $s * $i:');
    timer($count++, $l5, 'while - Count up   $s *= $i:');
    timer($count++, $l6, 'while - Count up   $s  = $s * $i:');
    timer($count++, $l7, 'while - Count down $s *= $i:');
    timer($count++, $l8, 'while - Count down $s  = $s * $i:');
    

    Example output (MD5 checksum compressed to avoid line breaking – the full value is 584b3ab832577fd1390970043efc0ec8):

    Loop 1: for   - Count up   $s *= $i:      12.853630 (584b3ab8...3efc0ec8)
    Loop 2: for   - Count up   $s  = $s * $i: 20.854735 (584b3ab8...3efc0ec8)
    Loop 3: for   - Count down $s *= $i:      14.798155 (584b3ab8...3efc0ec8)
    Loop 4: for   - Count down $s  = $s * $i: 23.699913 (584b3ab8...3efc0ec8)
    Loop 5: while - Count up   $s *= $i:      12.972428 (584b3ab8...3efc0ec8)
    Loop 6: while - Count up   $s  = $s * $i: 21.192956 (584b3ab8...3efc0ec8)
    Loop 7: while - Count down $s *= $i:      14.555620 (584b3ab8...3efc0ec8)
    Loop 8: while - Count down $s  = $s * $i: 23.790795 (584b3ab8...3efc0ec8)
    

    I consistently see a small (<1%) penalty for the ‘while’ loop over the corresponding ‘for’ loop, but I don’t have good explanation for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You probably want to call setlocale() first, "LC_ALL" should do… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Linux Ubuntu Desktop Jaunty Firebug FireCookie Pixel Perfect Web developer… May 14, 2026 at 9:06 am
  • Editorial Team
    Editorial Team added an answer Your code should look like this: var par = [];… May 14, 2026 at 9:06 am

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.