Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Shootout/Nsieve
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Old entry == Studying the Core shows the the mapM was preventing things from being unboxed properly. Big speedup. The extra `seqs` also help the unboxing. Should be around the fastest entry now. ''Is anyone else bothered by the usage of unsafeWrite? I agree that excellent speed is being gained, but this speed is gained at the cost of functional-style coding. I'm a novice Haskeller, but using unsafeWrite seems like an imperative hack to gain speed.'' -- AlsonKemp ''Response: unsafeWrite is just the same as a normal array update, without an extraneous bounds check (since it's already performed in the outer loop). We're just avoiding redundant computation (as we do in all entries for this problem). SPJ actually recommends unsafe* ops in arrays, see the "loop performance bug" thread in the haskell mail archives from 2005. In summary, its not a hack -- the name just makes clear that you have to do your own bounds check (Haskell is good at letting you know when there are extra proof considerations for the programmer to take). Since this program runs [http://shootout.alioth.debian.org/gp4/benchmark.php?test=nsieve&lang=all faster than GCC], then I think we should not be worried about avoiding a redundant bounds check :)'' -- DonStewart <haskell> -- -- $Id: nsieve-ghc.code,v 1.27 2006/01/08 22:44:56 igouy-guest Exp $ -- Written by Einar Karttunen, optimised further by Don Stewart -- import Data.Array.IO; import Data.Array.Base; import Data.Bits; import System; import Text.Printf main = (\n -> mapM_ (sieve.(10000 *).shiftL 1) [n,n-1,n-2]) . read . head =<< getArgs sieve m = do c <- newArray (2,m) True >>= \a -> loop a m 2 0 printf "Primes up to %8d %8d\n" (m::Int) (c::Int) :: IO () loop arr m n c | arr `seq` m `seq` n `seq` c `seq` False = undefined loop arr m n c = if n == m then return c else do el <- unsafeRead arr n if el then let loop' j | j > m = loop arr m (n+1) (c + 1) | otherwise = unsafeWrite arr j False >> loop' (j+n) in loop' (n+n) else loop (arr :: IOUArray Int Bool) m (n+1) c </haskell>
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width