Last active
August 29, 2015 14:15
-
-
Save maoe/f1010aa456e6da131a8f to your computer and use it in GitHub Desktop.
Memory allocation benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Exception | |
import Data.Functor (void) | |
import Foreign hiding (void) | |
import Criterion.Main | |
main :: IO () | |
main = defaultMain | |
[ bench "alloca" $ nfIO benchAlloca | |
, bench "malloc" $ nfIO benchMalloc | |
, bench "malloc/free" $ nfIO benchMallocFree | |
, bench "mallocForeignPtr" $ nfIO benchMallocForeignPtr | |
, bench "mallocForeignPtr/touchForeignPtr" $ nfIO benchMallocForeignPtrTouch | |
] | |
benchAlloca :: IO () | |
benchAlloca = allocaBytes 16 $ \_ -> return () | |
benchMalloc :: IO () | |
benchMalloc = void $ mallocBytes 16 | |
benchMallocFree :: IO () | |
benchMallocFree = do | |
ptr <- mallocBytes 16 | |
free ptr | |
benchMallocForeignPtr :: IO () | |
benchMallocForeignPtr = | |
void $ mallocForeignPtrBytes 16 | |
benchMallocForeignPtrTouch :: IO () | |
benchMallocForeignPtrTouch = | |
bracket (mallocForeignPtrBytes 16) touchForeignPtr $ \_ -> return () | |
{- | |
% ./bench -o bench.html --resamples 10000 | |
benchmarking alloca | |
time 6.601 ns (6.523 ns .. 6.683 ns) | |
0.999 R² (0.997 R² .. 0.999 R²) | |
mean 6.608 ns (6.525 ns .. 6.725 ns) | |
std dev 322.5 ps (242.6 ps .. 482.4 ps) | |
variance introduced by outliers: 74% (severely inflated) | |
benchmarking malloc | |
time 33.21 ns (33.04 ns .. 33.40 ns) | |
1.000 R² (0.999 R² .. 1.000 R²) | |
mean 33.36 ns (33.15 ns .. 33.66 ns) | |
std dev 828.6 ps (620.8 ps .. 1.119 ns) | |
variance introduced by outliers: 39% (moderately inflated) | |
benchmarking malloc/free | |
time 46.04 ns (45.44 ns .. 46.76 ns) | |
0.999 R² (0.998 R² .. 0.999 R²) | |
mean 46.74 ns (46.17 ns .. 47.56 ns) | |
std dev 2.298 ns (1.632 ns .. 3.287 ns) | |
variance introduced by outliers: 71% (severely inflated) | |
benchmarking mallocForeignPtr | |
time 8.438 ns (8.388 ns .. 8.497 ns) | |
0.999 R² (0.999 R² .. 1.000 R²) | |
mean 8.532 ns (8.436 ns .. 8.677 ns) | |
std dev 395.6 ps (275.2 ps .. 516.3 ps) | |
variance introduced by outliers: 71% (severely inflated) | |
benchmarking mallocForeignPtr/touchForeignPtr | |
time 24.11 ns (23.87 ns .. 24.34 ns) | |
0.999 R² (0.998 R² .. 1.000 R²) | |
mean 24.23 ns (23.96 ns .. 24.75 ns) | |
std dev 1.216 ns (724.6 ps .. 1.967 ns) | |
variance introduced by outliers: 73% (severely inflated) | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment