Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Why is this code so much slower than the same algorithm in C?

by snowhare (Friar)
on Dec 10, 2008 at 16:58 UTC ( [id://729457]=note: print w/replies, xml ) Need Help??


in reply to Re: Why is this code so much slower than the same algorithm in C?
in thread Why is this code so much slower than the same algorithm in C?

With a little analysis to omit test values that don't affect the answer and some improvement on the if..next..if..next logic, I was able to tighten that up slightly:
#!/usr/bin/perl use integer; my $i = 0; while ($i += 20) { last unless ($i % 3 or $i % 6 or $i % 7 or $i % 8 or $i % 9 or $i % 11 or $i % 12 or $i % 13 or $i % 14 or $i % 15 or $i % 16 or $i % 17 or $i % 18 or $i % 19 or $i % 20); } print "Number: $i\n";
  • Comment on Re^2: Why is this code so much slower than the same algorithm in C?
  • Download Code

Replies are listed 'Best First'.
Re^3: Why is this code so much slower than the same algorithm in C?
by Limbic~Region (Chancellor) on Dec 10, 2008 at 20:08 UTC
    snowhare,
    Can this be reduced even further?
    for values of x > 20 if x % 12 == 0 then x % 6 == 0
    Is there a reason to have them both?

    Update: In other words, order the list in descending order removing any exact multiples of previous items. When you reach 16, one of two things will happen. Either there will be a remainder and it will exit the loop or there won't be a remainder and it will continue. There is no need to test 8 after 16.

    Cheers - L~R

      perl -le "do { $i += 20 } while( $i%3||$i%7||$i%11||$i%13||$i%16||$i%1 +7||$i%19 ); print $i" 77597520
      or just
      perl -le "print 3*5*7*11*13*16*17*19"

      Update: Replace 3 with 9 in both samples above to get the correct answer, as noted and further explained in the replies below.

      - tye        

        You've got an extra factor of 3. The correct answer is 232792560. ;)
        Why do that much analysis and then throw away so much speed by not using a lexical? Also why not finish the analysis?
        perl -le 'my $i = 0;do { $i += 20 } while( $i%46558512 ); print $i' 232792560
        Performance is essentially the same if you write 46558512 as (9*7*11*13*16*17*19).
      Good catch.
      #!/usr/bin/perl use integer; my $i = 0; while ($i += 20) { last unless ( $i % 19 or $i % 18 or $i % 17 or $i % 16 or $i % 15 or $i % 14 or $i % 13 or $i % 12 or $i % 11 ); } print "Number: $i\n";
      Is 17% faster than my original version.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-20 04:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found