Skip to content

Commit eafa39f

Browse files
committed
Fifth implementation, bang patterns
❯ time .stack-work/install/x86_64-osx/lts-13.21/8.6.5/bin/bingo-sim 100000 Trials: 100000 Bingos: 3670 Hit rate: 0.0367 0.06s user 0.01s system 92% cpu 0.070 total
1 parent 4048469 commit eafa39f

4 files changed

Lines changed: 110 additions & 10 deletions

File tree

bingo-sim.cabal

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ cabal-version: 1.12
44
--
55
-- see: https://github.com/sol/hpack
66
--
7-
-- hash: 722efc688c7d34bdbe0a8a50324f74bfab1fc251f16b7f108844ae6a3f28e9ab
7+
-- hash: 052104d9a316b9076263967db2ae6fd443e0ce6bfaa88176ce1b9db55b7b3c02
88

99
name: bingo-sim
10-
version: 0.0.4.0
10+
version: 0.0.5.0
1111
synopsis: A small playground to learn about profiling Haskell.
1212

1313
description: This package simulates the probability of scoring a bingo at a particular children's carnival game. I've been using it to learn how profiling in Haskell works.

package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: bingo-sim
2-
version: 0.0.4.0
2+
version: 0.0.5.0
33
homepage: https://github.com/jez/bingo-sim
44
author: Jake Zimmerman
55
maintainer: zimmerman.jake@gmail.com

prof/bingo-sim.5.prof

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
Mon May 20 02:39 2019 Time and Allocation Profiling Report (Final)
2+
3+
bingo-sim +RTS -p -RTS 100000
4+
5+
total time = 0.46 secs (458 ticks @ 1000 us, 1 processor)
6+
total alloc = 556,512,224 bytes (excludes profiling overheads)
7+
8+
COST CENTRE MODULE SRC %time %alloc
9+
10+
next BingoSim.Prng src/BingoSim/Prng.hs:(79,1)-(91,40) 44.1 55.3
11+
swapBits BingoSim.Simulation src/BingoSim/Simulation.hs:(143,1)-(147,12) 15.1 0.0
12+
shuffleBits.withRand.bs' BingoSim.Simulation src/BingoSim/Simulation.hs:129:9-49 10.3 9.2
13+
shuffleBits.withRand BingoSim.Simulation src/BingoSim/Simulation.hs:(127,3)-(131,14) 9.4 0.0
14+
shuffleBits BingoSim.Simulation src/BingoSim/Simulation.hs:(123,1)-(131,14) 8.1 0.0
15+
shuffleBits.withRand.result BingoSim.Simulation src/BingoSim/Simulation.hs:130:9-49 6.6 15.4
16+
next.result BingoSim.Prng src/BingoSim/Prng.hs:80:7-41 0.9 10.1
17+
swapBits.result BingoSim.Simulation src/BingoSim/Simulation.hs:146:7-56 0.7 9.2
18+
19+
20+
individual inherited
21+
COST CENTRE MODULE SRC no. entries %time %alloc %time %alloc
22+
23+
MAIN MAIN <built-in> 195 0 0.0 0.0 100.0 100.0
24+
CAF GHC.Conc.Signal <entire-module> 292 0 0.0 0.0 0.0 0.0
25+
CAF GHC.Float <entire-module> 284 0 0.0 0.0 0.0 0.0
26+
CAF GHC.IO.Encoding <entire-module> 274 0 0.0 0.0 0.0 0.0
27+
CAF GHC.IO.Encoding.Iconv <entire-module> 272 0 0.0 0.0 0.0 0.0
28+
CAF GHC.IO.Handle.FD <entire-module> 263 0 0.0 0.0 0.0 0.0
29+
CAF Text.Printf <entire-module> 226 0 0.0 0.0 0.0 0.0
30+
CAF Text.Read.Lex <entire-module> 224 0 0.0 0.0 0.0 0.0
31+
CAF:board_r5qp BingoSim.Simulation <no location info> 317 0 0.0 0.0 0.0 0.0
32+
runSimulation BingoSim.Simulation src/BingoSim/Simulation.hs:(69,1)-(85,41) 405 0 0.0 0.0 0.0 0.0
33+
randomBoard BingoSim.Simulation src/BingoSim/Simulation.hs:(109,1)-(112,15) 406 0 0.0 0.0 0.0 0.0
34+
randomBoard.board BingoSim.Simulation src/BingoSim/Simulation.hs:110:7-28 407 1 0.0 0.0 0.0 0.0
35+
CAF:col1 BingoSim.Board src/BingoSim/Board.hs:148:1-4 349 0 0.0 0.0 0.0 0.0
36+
col1 BingoSim.Board src/BingoSim/Board.hs:148:1-45 428 1 0.0 0.0 0.0 0.0
37+
CAF:col2 BingoSim.Board src/BingoSim/Board.hs:151:1-4 348 0 0.0 0.0 0.0 0.0
38+
col2 BingoSim.Board src/BingoSim/Board.hs:151:1-45 429 1 0.0 0.0 0.0 0.0
39+
CAF:col3 BingoSim.Board src/BingoSim/Board.hs:154:1-4 347 0 0.0 0.0 0.0 0.0
40+
col3 BingoSim.Board src/BingoSim/Board.hs:154:1-45 430 1 0.0 0.0 0.0 0.0
41+
CAF:col4 BingoSim.Board src/BingoSim/Board.hs:157:1-4 346 0 0.0 0.0 0.0 0.0
42+
col4 BingoSim.Board src/BingoSim/Board.hs:157:1-45 431 1 0.0 0.0 0.0 0.0
43+
CAF:col5 BingoSim.Board src/BingoSim/Board.hs:160:1-4 345 0 0.0 0.0 0.0 0.0
44+
col5 BingoSim.Board src/BingoSim/Board.hs:160:1-45 432 1 0.0 0.0 0.0 0.0
45+
CAF:col6 BingoSim.Board src/BingoSim/Board.hs:163:1-4 343 0 0.0 0.0 0.0 0.0
46+
col6 BingoSim.Board src/BingoSim/Board.hs:163:1-45 433 1 0.0 0.0 0.0 0.0
47+
CAF:dia1 BingoSim.Board src/BingoSim/Board.hs:124:1-4 358 0 0.0 0.0 0.0 0.0
48+
dia1 BingoSim.Board src/BingoSim/Board.hs:124:1-45 420 1 0.0 0.0 0.0 0.0
49+
CAF:dia2 BingoSim.Board src/BingoSim/Board.hs:127:1-4 357 0 0.0 0.0 0.0 0.0
50+
dia2 BingoSim.Board src/BingoSim/Board.hs:127:1-45 421 1 0.0 0.0 0.0 0.0
51+
CAF:lvl1_r5qk BingoSim.Simulation <no location info> 314 0 0.0 0.0 0.0 0.0
52+
CAF:lvl3_r5qm BingoSim.Simulation <no location info> 315 0 0.0 0.0 0.0 0.0
53+
CAF:lvl5_r5qo BingoSim.Simulation <no location info> 316 0 0.0 0.0 0.0 0.0
54+
CAF:lvl6_r5qq BingoSim.Simulation <no location info> 318 0 0.0 0.0 0.0 0.0
55+
runSimulation BingoSim.Simulation src/BingoSim/Simulation.hs:(69,1)-(85,41) 399 0 0.0 0.0 0.0 0.0
56+
mkState BingoSim.Prng src/BingoSim/Prng.hs:(56,1)-(58,39) 400 1 0.0 0.0 0.0 0.0
57+
CAF:main1 Main <no location info> 388 0 0.0 0.0 0.0 0.0
58+
main Main app/Main.hs:(10,1)-(12,30) 390 1 0.0 0.0 0.0 0.0
59+
CAF:main4 Main <no location info> 385 0 0.0 0.0 0.0 0.0
60+
CAF:row1 BingoSim.Board src/BingoSim/Board.hs:130:1-4 355 0 0.0 0.0 0.0 0.0
61+
row1 BingoSim.Board src/BingoSim/Board.hs:130:1-45 422 1 0.0 0.0 0.0 0.0
62+
CAF:row2 BingoSim.Board src/BingoSim/Board.hs:133:1-4 354 0 0.0 0.0 0.0 0.0
63+
row2 BingoSim.Board src/BingoSim/Board.hs:133:1-45 423 1 0.0 0.0 0.0 0.0
64+
CAF:row3 BingoSim.Board src/BingoSim/Board.hs:136:1-4 353 0 0.0 0.0 0.0 0.0
65+
row3 BingoSim.Board src/BingoSim/Board.hs:136:1-45 424 1 0.0 0.0 0.0 0.0
66+
CAF:row4 BingoSim.Board src/BingoSim/Board.hs:139:1-4 352 0 0.0 0.0 0.0 0.0
67+
row4 BingoSim.Board src/BingoSim/Board.hs:139:1-45 425 1 0.0 0.0 0.0 0.0
68+
CAF:row5 BingoSim.Board src/BingoSim/Board.hs:142:1-4 351 0 0.0 0.0 0.0 0.0
69+
row5 BingoSim.Board src/BingoSim/Board.hs:142:1-45 426 1 0.0 0.0 0.0 0.0
70+
CAF:row6 BingoSim.Board src/BingoSim/Board.hs:145:1-4 350 0 0.0 0.0 0.0 0.0
71+
row6 BingoSim.Board src/BingoSim/Board.hs:145:1-45 427 1 0.0 0.0 0.0 0.0
72+
main Main app/Main.hs:(10,1)-(12,30) 391 0 0.0 0.0 100.0 100.0
73+
main.\ Main app/Main.hs:11:13-37 392 1 0.0 0.0 100.0 100.0
74+
runSimulation BingoSim.Simulation src/BingoSim/Simulation.hs:(69,1)-(85,41) 393 1 0.2 0.0 100.0 100.0
75+
hasBingo BingoSim.Board src/BingoSim/Board.hs:(82,1)-(96,32) 419 100000 0.7 0.0 0.7 0.0
76+
randomBoard BingoSim.Simulation src/BingoSim/Simulation.hs:(109,1)-(112,15) 394 100000 0.2 0.9 99.1 100.0
77+
randomBoard.result BingoSim.Simulation src/BingoSim/Simulation.hs:111:7-40 395 100000 0.4 0.0 98.9 99.1
78+
shuffleBits BingoSim.Simulation src/BingoSim/Simulation.hs:(123,1)-(131,14) 396 3600000 8.1 0.0 98.5 99.1
79+
next BingoSim.Prng src/BingoSim/Prng.hs:(79,1)-(91,40) 398 3500000 44.1 55.3 90.2 99.1
80+
next.result BingoSim.Prng src/BingoSim/Prng.hs:80:7-41 401 3500000 0.9 10.1 0.9 10.1
81+
shuffleBits.withRand BingoSim.Simulation src/BingoSim/Simulation.hs:(127,3)-(131,14) 402 3500000 9.4 0.0 43.7 33.7
82+
shuffleBits.withRand.bs' BingoSim.Simulation src/BingoSim/Simulation.hs:129:9-49 404 3500000 10.3 9.2 26.9 18.3
83+
swapBits BingoSim.Simulation src/BingoSim/Simulation.hs:(143,1)-(147,12) 408 3500000 15.1 0.0 16.6 9.2
84+
swapBits.result BingoSim.Simulation src/BingoSim/Simulation.hs:146:7-56 410 3182752 0.7 9.2 0.7 9.2
85+
swapBits.x BingoSim.Simulation src/BingoSim/Simulation.hs:145:7-59 409 3182752 0.9 0.0 0.9 0.0
86+
shuffleBits.withRand.i BingoSim.Simulation src/BingoSim/Simulation.hs:128:9-45 403 3500000 0.9 0.0 0.9 0.0
87+
shuffleBits.withRand.result BingoSim.Simulation src/BingoSim/Simulation.hs:130:9-49 411 3500000 6.6 15.4 6.6 15.4
88+
next.s0' BingoSim.Prng src/BingoSim/Prng.hs:87:7-27 418 3499999 0.2 0.0 0.2 0.0
89+
next.s1' BingoSim.Prng src/BingoSim/Prng.hs:86:7-27 417 3499999 0.4 0.0 0.4 0.0
90+
next.s2' BingoSim.Prng src/BingoSim/Prng.hs:84:7-26 414 3499999 0.0 0.0 0.0 0.0
91+
next.s2'' BingoSim.Prng src/BingoSim/Prng.hs:89:7-26 416 3499999 0.0 0.0 0.0 0.0
92+
next.s3' BingoSim.Prng src/BingoSim/Prng.hs:85:7-26 412 3499999 0.2 0.0 0.2 0.0
93+
next.s3'' BingoSim.Prng src/BingoSim/Prng.hs:90:7-31 413 3499999 0.7 0.0 0.7 0.0
94+
next.t BingoSim.Prng src/BingoSim/Prng.hs:82:7-35 415 3499999 0.0 0.0 0.0 0.0
95+
shuffleBits.n' BingoSim.Simulation src/BingoSim/Simulation.hs:126:3-12 397 3500000 0.2 0.0 0.2 0.0
96+
runSimulation.rate BingoSim.Simulation src/BingoSim/Simulation.hs:82:7-58 434 1 0.0 0.0 0.0 0.0

src/BingoSim/Simulation.hs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE BangPatterns #-}
12
{-# LANGUAGE BinaryLiterals #-}
23

34
-- |
@@ -106,8 +107,9 @@ runSimulation trials = do
106107
-- intuition for how this game works in the real world.
107108
randomBoard :: Prng.State -> IO (Board, Prng.State)
108109
randomBoard gen = do
109-
let board = Board 0x7fff
110-
return $ shuffleBits gen board 36
110+
let board = Board 0x7fff
111+
let !result = shuffleBits gen board 36
112+
return result
111113

112114
-- | Implements [Fisher-Yates](https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle) at the bit level for a 'Board'.
113115
--
@@ -123,9 +125,10 @@ shuffleBits gen (Board bs) n = next gen withRand
123125
where
124126
n' = n - 1
125127
withRand rand gen' =
126-
let i = rand `mod` (fromIntegral n)
127-
bs' = swapBits bs n' (fromIntegral i)
128-
in shuffleBits gen' (Board bs') n'
128+
let !i = rand `mod` (fromIntegral n)
129+
!bs' = swapBits bs n' (fromIntegral i)
130+
!result = shuffleBits gen' (Board bs') n'
131+
in result
129132

130133
-- | Helper for swapping two specific bits.
131134
--
@@ -139,6 +142,7 @@ swapBits
139142
-> Word64 -- ^ Swapped result
140143
swapBits bs i j | i == j = bs
141144
swapBits bs i j =
142-
let x = ((shiftR bs i) `xor` (shiftR bs j)) .&. 0x1
143-
in bs `xor` ((shiftL x i) .|. (shiftL x j))
145+
let !x = ((shiftR bs i) `xor` (shiftR bs j)) .&. 0x1
146+
!result = bs `xor` ((shiftL x i) .|. (shiftL x j))
147+
in result
144148

0 commit comments

Comments
 (0)