Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: foldl' vs seq (Stephen Tetley)
2. Re: Performance of Idiomatic lazy Haskell (Daniel Fischer)
3. Re: Performance of Idiomatic lazy Haskell (Markus B?hm)
4. Re: foldl' vs seq (Stephen Tetley)
5. Re: Performance of Idiomatic lazy Haskell (Daniel Fischer)
6. Re: Performance of Idiomatic lazy Haskell (Markus B?hm)
7. Re: Performance of Idiomatic lazy Haskell (Markus B?hm)
8. Re: Performance of Idiomatic lazy Haskell (Daniel Fischer)
----------------------------------------------------------------------
Message: 1
Date: Mon, 1 Feb 2010 13:15:16 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] foldl' vs seq
To: Gabi <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I
On 1 February 2010 11:54, Gabi <[email protected]> wrote:
> Hi Stephen,
> Thanks for the answer. Now I got confused :)
>
> 1. What is seq used for if it doesn't force strictness ?
> 2. Why the accumulator is important ? In other words why the following
> is much slower ? Isn't it RT too ?
>
> slowSum :: [Integer] -> Integer
> slowSum[] = 0
> slowSum (x:xs) = x `seq` x + slowSum xs
Hi Gabi
It seems to be classified as 'almost tail recursion', see here:
http://www.haskell.org/haskellwiki/Performance/Accumulating_parameter
In slowSum although x gets 'forced' it still has to be added to the
result of the recursive call [ slowSum xs ], so there isn't much that
can actually be done with it at each recursive step until you get an
answer back (what it actually does is build up the notorious [x + ..]
thunks).
Best wishes
Stephen
------------------------------
Message: 2
Date: Mon, 1 Feb 2010 14:16:59 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
To: Markus B?hm <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Montag 01 Februar 2010 10:37:41 schrieb Markus Böhm:
> Daniel, Stephen, Felipe thanks for Your answers/advice, which I
> studied this morning. Helps me a lot on my learning curve. I guess it
> takes some time to get a feeling for the performance/runtime behaviour
> of abstractions in Haskell.
>
> Just for fun and w/o knowing Lua: I translated my Haskell
> "loops-while"-version into Lua
That's not quite a translation of the loop you posted. Not really
important, though, small difference.
> and ran on LuaJit Beta 2.0.0. Seems to
> beat our best Haskell version.
So LuaJit Beta 2.0.0 beats gcc -O3 on such a simple loop?
I'm not going to install Lua to test, but if that's really the case, I'm
sure the folks at gnu.org would like to hear about it.
Or don't you consider -fvia-C compiled loops to be true Haskell versions?
>
> print("EPS: ")
> EPS = io.read("*number")
> pi2 = 1
> i = 3
> s = false
> repeat
> pi1 = pi2
> if s then
> pi2 = pi1 + 1.0/i
> s = false
> else
> pi2 = pi1 - 1.0/i
> s = true
> end
> i = i+2
>
> until EPS > 4.0 * math.abs(pi2-pi1)
>
> print("PI mit EPS", EPS," = ",4*pi1," n= ",(i-3)/2)
>
>
> -- Markus
------------------------------
Message: 3
Date: Mon, 1 Feb 2010 14:31:40 +0100
From: Markus B?hm <[email protected]>
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Daniel, with LuaJIT it needs 1.45 sec cpu-time on my machine with
attached variant. I compiled all our Haskell variants with ghc --make
-O2. I don't know about -fvia-C, have to find out. I hope I didn't
distract You with my Lua variant. In any case thank You very much for
your advice. Markus.
print("EPS: ")
EPS = io.read("*number")
local x = os.clock()
pi2 = 1
i = 3
s = false
repeat
pi1 = pi2
if s then
pi2 = pi1 + 1.0/i
s = false
else
pi2 = pi1 - 1.0/i
s = true
end
i = i+2
until EPS > 4.0 * math.abs(pi2-pi1)
print("PI mit EPS", EPS," = ",4*pi1," n= ",(i-3)/2)
print(string.format("elapsed time: %.2f\n", os.clock() -x))
-- Markus
On Mon, Feb 1, 2010 at 2:16 PM, Daniel Fischer <[email protected]> wrote:
> Am Montag 01 Februar 2010 10:37:41 schrieb Markus Böhm:
>> Daniel, Stephen, Felipe thanks for Your answers/advice, which I
>> studied this morning. Helps me a lot on my learning curve. I guess it
>> takes some time to get a feeling for the performance/runtime behaviour
>> of abstractions in Haskell.
>>
>> Just for fun and w/o knowing Lua: I translated my Haskell
>> "loops-while"-version into Lua
>
> That's not quite a translation of the loop you posted. Not really
> important, though, small difference.
>
>> and ran on LuaJit Beta 2.0.0. Seems to
>> beat our best Haskell version.
>
> So LuaJit Beta 2.0.0 beats gcc -O3 on such a simple loop?
> I'm not going to install Lua to test, but if that's really the case, I'm
> sure the folks at gnu.org would like to hear about it.
> Or don't you consider -fvia-C compiled loops to be true Haskell versions?
>
>>
>> print("EPS: ")
>> EPS = io.read("*number")
>> pi2 = 1
>> i = 3
>> s = false
>> repeat
>> pi1 = pi2
>> if s then
>> pi2 = pi1 + 1.0/i
>> s = false
>> else
>> pi2 = pi1 - 1.0/i
>> s = true
>> end
>> i = i+2
>>
>> until EPS > 4.0 * math.abs(pi2-pi1)
>>
>> print("PI mit EPS", EPS," = ",4*pi1," n= ",(i-3)/2)
>>
>>
>> -- Markus
>
------------------------------
Message: 4
Date: Mon, 1 Feb 2010 13:32:17 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] foldl' vs seq
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Gabi
Furthermore, a bit of rewriting might make a better explanation.
Ignoring the seq, the right-hand side is:
fn (x:xs) = x + fn xs
Although the recursive step is the rightmost part, (fn xs) must return
back to (x + ...) to perform the summing. If the arguments are
swapped, the function is the same but the recursive step is obviously
not in the tail position:
fn (x:xs) = (fn xs) + x
Best wishes
Stephen
------------------------------
Message: 5
Date: Mon, 1 Feb 2010 14:52:41 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
To: Markus B?hm <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Montag 01 Februar 2010 14:31:40 schrieb Markus Böhm:
> Daniel, with LuaJIT it needs 1.45 sec cpu-time on my machine with
> attached variant.
That's pretty fast.
> I compiled all our Haskell variants with ghc --make -O2.
Well, GHC isn't as good at loop-optimising as gcc is. Depending on the
loop, the via-C compiled binaries are between 1.4 and 2.3 times faster than
the NCG compiled.
> I don't know about -fvia-C, have to find out. I hope I didn't
> distract You with my Lua variant.
No sweat.
> In any case thank You very much for
> your advice. Markus.
>
You're welcome.
Can you try the below with
ghc -O2 -fexcess-precision -fvia-C -optc-O3 -o luaLoop --make Whatever.hs
and run with
echo '0.00000001' | time ./luaLoop
?
It's a fairly direct translation of the Lua code, and it runs here more or
less equally fast as (gcc compiled) C-loops.
=================================
module Main (main) where
main :: IO ()
main = do
putStrLn "EPS:"
eps <- readLn :: IO Double
print $ 4*calcPi eps
calcPi :: Double -> Double
calcPi eps = go False 1 3
where
go bl p1 i
| 4*abs(p2-p1) < eps = p1
| otherwise = go (not bl) p2 (i+2)
where
p2 | bl = p1+1/i
| otherwise = p1-1/i
==================================
------------------------------
Message: 6
Date: Mon, 1 Feb 2010 15:08:58 +0100
From: Markus B?hm <[email protected]>
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Daniel, I use GHC 6.12.1 and Windows XP. The time command doesn't seem
to work. It says in German: specified time can't be read. Give a new
time.
-- Markus
On Mon, Feb 1, 2010 at 2:52 PM, Daniel Fischer <[email protected]> wrote:
> Am Montag 01 Februar 2010 14:31:40 schrieb Markus Böhm:
>> Daniel, with LuaJIT it needs 1.45 sec cpu-time on my machine with
>> attached variant.
>
> That's pretty fast.
>
>> I compiled all our Haskell variants with ghc --make -O2.
>
> Well, GHC isn't as good at loop-optimising as gcc is. Depending on the
> loop, the via-C compiled binaries are between 1.4 and 2.3 times faster than
> the NCG compiled.
>
>> I don't know about -fvia-C, have to find out. I hope I didn't
>> distract You with my Lua variant.
>
> No sweat.
>
>> In any case thank You very much for
>> your advice. Markus.
>>
>
> You're welcome.
>
> Can you try the below with
>
> ghc -O2 -fexcess-precision -fvia-C -optc-O3 -o luaLoop --make Whatever.hs
>
> and run with
>
> echo '0.00000001' | time ./luaLoop
>
> ?
> It's a fairly direct translation of the Lua code, and it runs here more or
> less equally fast as (gcc compiled) C-loops.
>
> =================================
> module Main (main) where
>
> main :: IO ()
> main = do
> putStrLn "EPS:"
> eps <- readLn :: IO Double
> print $ 4*calcPi eps
>
> calcPi :: Double -> Double
> calcPi eps = go False 1 3
> where
> go bl p1 i
> | 4*abs(p2-p1) < eps = p1
> | otherwise = go (not bl) p2 (i+2)
> where
> p2 | bl = p1+1/i
> | otherwise = p1-1/i
>
> ==================================
>
>
------------------------------
Message: 7
Date: Mon, 1 Feb 2010 15:22:45 +0100
From: Markus B?hm <[email protected]>
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Daniel, here another feedback:
F:\MeineUebungen>ghc -O2 -fexcess-precision -fvia-C -optc-O3 -o luaLoop --make p
085-pi_lualoop.hs
[1 of 1] Compiling Main ( p085-pi_lualoop.hs, p085-pi_lualoop.o )
Linking luaLoop.exe ...
F:\MeineUebungen>echo '0.00000001' | time ./luaLoop
Eingegebene Zeit kann nicht übernommen werden.
Geben Sie die neue Zeit ein:
F:\MeineUebungen>luaLoop +RTS -sstderr -RTS
luaLoop +RTS -sstderr
EPS:
0.00000001
3.1415926485894725
61,428 bytes allocated in the heap
1,316 bytes copied during GC
4,564 bytes maximum residency (1 sample(s))
11,820 bytes maximum slop
1 MB total memory in use (0 MB lost due to fragmentation)
Generation 0: 0 collections, 0 parallel, 0.00s, 0.00s elapsed
Generation 1: 1 collections, 0 parallel, 0.00s, 0.00s elapsed
INIT time 0.02s ( 0.00s elapsed)
MUT time 8.06s ( 16.83s elapsed)
GC time 0.00s ( 0.00s elapsed)
EXIT time 0.00s ( 0.00s elapsed)
Total time 8.08s ( 16.83s elapsed)
%GC time 0.0% (0.0% elapsed)
Alloc rate 7,604 bytes per MUT second
Productivity 99.8% of total user, 47.9% of total elapsed
-- Markus
On Mon, Feb 1, 2010 at 3:08 PM, Markus Böhm <[email protected]> wrote:
> Daniel, I use GHC 6.12.1 and Windows XP. The time command doesn't seem
> to work. It says in German: specified time can't be read. Give a new
> time.
>
> -- Markus
>
>
>
>
> On Mon, Feb 1, 2010 at 2:52 PM, Daniel Fischer <[email protected]>
> wrote:
>> Am Montag 01 Februar 2010 14:31:40 schrieb Markus Böhm:
>>> Daniel, with LuaJIT it needs 1.45 sec cpu-time on my machine with
>>> attached variant.
>>
>> That's pretty fast.
>>
>>> I compiled all our Haskell variants with ghc --make -O2.
>>
>> Well, GHC isn't as good at loop-optimising as gcc is. Depending on the
>> loop, the via-C compiled binaries are between 1.4 and 2.3 times faster than
>> the NCG compiled.
>>
>>> I don't know about -fvia-C, have to find out. I hope I didn't
>>> distract You with my Lua variant.
>>
>> No sweat.
>>
>>> In any case thank You very much for
>>> your advice. Markus.
>>>
>>
>> You're welcome.
>>
>> Can you try the below with
>>
>> ghc -O2 -fexcess-precision -fvia-C -optc-O3 -o luaLoop --make Whatever.hs
>>
>> and run with
>>
>> echo '0.00000001' | time ./luaLoop
>>
>> ?
>> It's a fairly direct translation of the Lua code, and it runs here more or
>> less equally fast as (gcc compiled) C-loops.
>>
>> =================================
>> module Main (main) where
>>
>> main :: IO ()
>> main = do
>> putStrLn "EPS:"
>> eps <- readLn :: IO Double
>> print $ 4*calcPi eps
>>
>> calcPi :: Double -> Double
>> calcPi eps = go False 1 3
>> where
>> go bl p1 i
>> | 4*abs(p2-p1) < eps = p1
>> | otherwise = go (not bl) p2 (i+2)
>> where
>> p2 | bl = p1+1/i
>> | otherwise = p1-1/i
>>
>> ==================================
>>
>>
>
------------------------------
Message: 8
Date: Mon, 1 Feb 2010 15:40:52 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Performance of Idiomatic lazy Haskell
To: Markus B?hm <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Montag 01 Februar 2010 15:22:45 schrieb Markus Böhm:
> Daniel, I use GHC 6.12.1 and Windows XP.
*sigh*
Have you a CygWin installed? It should run as given from a CygWin shell, I
believe.
> The time command doesn't seem
> to work. It says in German: specified time can't be read. Give a new
> time.
Replace "time" with however you get the CPU time used by a process on
Windows.
But I think Windows doesn't allow piping, and I'm not sure whether it has
echo, so see below.
> Daniel, here another feedback:
>
> F:\MeineUebungen>ghc -O2 -fexcess-precision -fvia-C -optc-O3 -o luaLoop
> --make p 085-pi_lualoop.hs
> [1 of 1] Compiling Main ( p085-pi_lualoop.hs,
> p085-pi_lualoop.o ) Linking luaLoop.exe ...
>
> F:\MeineUebungen>echo '0.00000001' | time ./luaLoop
> Eingegebene Zeit kann nicht übernommen werden.
> Geben Sie die neue Zeit ein:
>
> F:\MeineUebungen>luaLoop +RTS -sstderr -RTS
> luaLoop +RTS -sstderr
> EPS:
> 0.00000001
> 3.1415926485894725
> 61,428 bytes allocated in the heap
> 1,316 bytes copied during GC
> 4,564 bytes maximum residency (1 sample(s))
> 11,820 bytes maximum slop
> 1 MB total memory in use (0 MB lost due to fragmentation)
>
> Generation 0: 0 collections, 0 parallel, 0.00s, 0.00s
> elapsed Generation 1: 1 collections, 0 parallel, 0.00s, 0.00s
> elapsed
>
> INIT time 0.02s ( 0.00s elapsed)
> MUT time 8.06s ( 16.83s elapsed)
> GC time 0.00s ( 0.00s elapsed)
> EXIT time 0.00s ( 0.00s elapsed)
> Total time 8.08s ( 16.83s elapsed)
>
> %GC time 0.0% (0.0% elapsed)
>
> Alloc rate 7,604 bytes per MUT second
>
> Productivity 99.8% of total user, 47.9% of total elapsed
What?????
Please try the following:
1) hardcode eps = 1e-8 in the source (or get it via getArgs and pass it as
a command line argument)
2) ghc -O2 -fforce-recomp -fexcess-precision -fvia-C -optc-O3 -o cloop --
make Source.hs
3) ghc -O2 -fforce-recomp -o nloop --make Source.hs
4) cloop +RTS -sstderr
5) nloop +RTS -sstderr
>
> -- Markus
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 20, Issue 2
****************************************