foreach (1..100) { push @_,$_; foreach (@_[1..($#_-1)]) { pop @_ and last unless ($_[-1] % $_); } } print join(" ",@_),"\n";
|
---|
Replies are listed 'Best First'. | |||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
RE: sieve of Eratosthenes
by ase (Monk) on Jul 01, 2000 at 11:49 UTC | |||||||||||||||||||||||||||||||||||||
Here's a table summarizing the data:
It seems Mavericks code is only very slightly slower with the benefit of being slightly shorter. This was done on a 300 mHz amd K6 running win 98 and perl 5.005_02 (activestate 509) as always your mileage may vary.
Update 7/13/2000: I modified Adam's code per this node and re-benchmarked accordingly. | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||
by maverick (Curate) on Jul 01, 2000 at 17:43 UTC | |||||||||||||||||||||||||||||||||||||
I went back and uncomment the print statements to see what kind of overhead the grep might cause. At N=100000 I end up with: I would have expected us to end up with closer times, but your's ends up being 24 seconds faster? wow. So, I removed the prints and got: I'm running this on a PII 400, using Red Hat Linux and the stock perl RPM (5.005_03). It would seem that vec is much faster under Linux than under windows. Hmmm...I wonder how these benchmark out on other platforms... /\/\averick | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||
by ase (Monk) on Jul 02, 2000 at 06:50 UTC | |||||||||||||||||||||||||||||||||||||
I would also be interested in benchmarks on other platforms. | [reply] | ||||||||||||||||||||||||||||||||||||
by cforde (Monk) on May 19, 2001 at 04:23 UTC | |||||||||||||||||||||||||||||||||||||
On my WinNT PIII 800Mhz machine with ActiveState 522 both of these are faster than ase's Bit Vector implementation.
One thing that I found interesting is that for n less than about 10,000, Carl1 is significantly faster than Carl2. The loop control overhead must be pretty significant.
Have fun, | [reply] [d/l] | ||||||||||||||||||||||||||||||||||||
RE: sieve of erothenes
by ferrency (Deacon) on Jul 08, 2000 at 01:01 UTC | |||||||||||||||||||||||||||||||||||||
This takes a number as a parameter, and checks to see whether it's prime or not. I modified it a bit to stuff @_ with primes, and got this:
I think that's shorter, no? As I said, I can't claim credit for this! But it's definitely worth noting. There's an explanation of how it works on his talk web site. Alan | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||
by ferrency (Deacon) on Jul 08, 2000 at 01:29 UTC | |||||||||||||||||||||||||||||||||||||
First, we build a string: This is a string of 1's as long as the number you're testing. Then we test that against the regexp: The first half of the alternation just takes care of matching an empty string (the number 0) and a single 1 (the number 1) which are prime. I'll break out the second half for clarity: In the parens, we match 2 or more ones in succession, in a non-greedy fashion. That means, it'll first try the regex matching "11" in the parens, and if the rest of the regex fails, it'll backtrack and try "111" in the parens, and so on. (sounds like a for loop...) After the parens, it looks for at least one more occurance of \1, which is what it just captured inside the parens. So, it's basically matching for an integral number of "11"'s in the string, and if that fails, it backtracks and tries matching an integral number of "111"'s in the string, and so on. Because of the anchors, there is no room for extra 1's at the end of the string. And since it needs to match the string of ones twice (once in the parens, and at least one more time after that), if the regex succeeds, you know that the number is divisible by a smaller integer, and is therefore not prime. I hope this helps... since it was already described in public at least once, I don't feel bad giving this particular magician's secret away. I'm sorry I don't know who originally wrote the code; I'm not sure if it's Abigail's or not. Alan | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||
by maverick (Curate) on Jul 10, 2000 at 01:05 UTC | |||||||||||||||||||||||||||||||||||||
and ended up with: You have the shortest but ase still has the fastest. :) This started off being about the seive of "the always mispelled guy", but now I'm curious about other ways of doing this. Perhaps on some rainy afternoon I'll go dig up few more interesting snippets to throw at the monk collective... /\/\averick | [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||
RE: sieve of erothenes
by Adam (Vicar) on Jul 01, 2000 at 03:05 UTC | |||||||||||||||||||||||||||||||||||||
########## My spacing:
| [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||
by maverick (Curate) on Jul 01, 2000 at 03:24 UTC | |||||||||||||||||||||||||||||||||||||
I kept trying to think of how do this with maps and maybe come out with something different, but nothing has come to me yet /\/\averick ##Update## just saw how to squish out a 3 more characters
| [reply] [d/l] [select] | ||||||||||||||||||||||||||||||||||||
by Adam (Vicar) on Jul 01, 2000 at 03:52 UTC | |||||||||||||||||||||||||||||||||||||
I don't understand why yours is faster because you use two for loops and I use a while loop the second time, dropping the overhead of making the temp array that foreach generates. Now I'm curious about the efficiencies of the two looping mechanisms... | [reply] | ||||||||||||||||||||||||||||||||||||
by maverick (Curate) on Jul 01, 2000 at 08:44 UTC | |||||||||||||||||||||||||||||||||||||
by husker (Chaplain) on Jul 05, 2000 at 18:05 UTC | |||||||||||||||||||||||||||||||||||||
by Adam (Vicar) on Jul 01, 2000 at 22:12 UTC | |||||||||||||||||||||||||||||||||||||
RE: sieve of Erathostenes
by Corion (Patriarch) on Jul 02, 2000 at 02:16 UTC | |||||||||||||||||||||||||||||||||||||
[reply] | |||||||||||||||||||||||||||||||||||||
by plaid (Chaplain) on Jul 02, 2000 at 02:47 UTC | |||||||||||||||||||||||||||||||||||||
| [reply] |