[Haskell-cafe] Optimization fun

2007-02-10 Thread Creighton Hogg
Hello Haskell-ers, So a friend and I were thinking about making code faster in Haskell, and I was wondering if there was a way to improve the following method of generating the list of all prime numbers. It takes about 13 seconds to run, meanwhile my friend's C version took 0.1. I'd love to

Re: [Haskell-cafe] Optimization fun

2007-02-10 Thread Lennart Augustsson
There are many things that makes your code slow. * The default for Haskell is to compute with Integer, not Int. So that makes from Integral and floor very slow. * foldl' is a bad choice, because it is too strict, you want to abort the loop as soon as possible. * your take is really wrong.

Re: [Haskell-cafe] Optimization fun

2007-02-10 Thread Creighton Hogg
On 2/10/07, Lennart Augustsson [EMAIL PROTECTED] wrote: There are many things that makes your code slow. * The default for Haskell is to compute with Integer, not Int. So that makes from Integral and floor very slow. * foldl' is a bad choice, because it is too strict, you want to abort the

Fwd: [Haskell-cafe] Optimization fun

2007-02-10 Thread Peter Berry
Gah! Gmail has really broken defaults for posting to lists. On 10/02/07, Creighton Hogg [EMAIL PROTECTED] wrote: Hello Haskell-ers, So a friend and I were thinking about making code faster in Haskell, and I was wondering if there was a way to improve the following method of generating the list

Re: [Haskell-cafe] Optimization fun

2007-02-10 Thread Creighton Hogg
On 2/10/07, Creighton Hogg [EMAIL PROTECTED] wrote: On 2/10/07, Lennart Augustsson [EMAIL PROTECTED] wrote: There are many things that makes your code slow. * The default for Haskell is to compute with Integer, not Int. So that makes from Integral and floor very slow. * foldl' is a bad

Re: [Haskell-cafe] Optimization fun

2007-02-10 Thread Creighton Hogg
On 2/10/07, Peter Berry [EMAIL PROTECTED] wrote: Gah! Gmail has really broken defaults for posting to lists. On 10/02/07, Creighton Hogg [EMAIL PROTECTED] wrote: Hello Haskell-ers, So a friend and I were thinking about making code faster in Haskell, and I was wondering if there was a way to

Re: [Haskell-cafe] Optimization fun

2007-02-10 Thread Lennart Augustsson
Yeah, the 1 first primes takes about 0.1 seconds in Haskell too using the code I posted. On Feb 10, 2007, at 21:49 , Creighton Hogg wrote: On 2/10/07, Peter Berry [EMAIL PROTECTED] wrote: Gah! Gmail has really broken defaults for posting to lists. On 10/02/07, Creighton Hogg [EMAIL

Re: [Haskell-cafe] Optimization fun

2007-02-10 Thread Bulat Ziganshin
Hello Creighton, Sunday, February 11, 2007, 12:02:09 AM, you wrote: import Data.List primes = 2:(foldr (\x y - if isPrime x then x:y else y) [] [3..])     where isPrime x = foldl' (\z y - z (if x `mod` y == 0 then False else True)) True (take (floor $ sqrt $ fromIntegral x) primes) my