public
Created

Benchmarks for ImplicitCAD STL Output

  • Download Gist
benchmark.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
{-# OPTIONS_GHC -threaded -O2 #-}
{-# LANGUAGE OverloadedStrings #-}
 
-- This benchmark was executed with the following options:
-- -s 100 +RTS -N1 -RTS
 
import Criterion.Main
import Graphics.Implicit
 
import Graphics.Implicit.Export.TextBuilderUtils
import Graphics.Implicit.Definitions
import Graphics.Implicit.Export.Definitions
import Graphics.Implicit.Export.SymbolicObj2
import Graphics.Implicit.Export.SymbolicObj3
 
import qualified Data.Text.Lazy.IO as TextIO
 
 
formatWithLazyText triangles = toLazyText $ stlHeader <> mconcat (map triangle triangles) <> stlFooter
where
stlHeader = "solid ImplictCADExport\n"
stlFooter = "endsolid ImplictCADExport\n"
vertex :: 3 -> Builder
vertex (x,y,z) = mconcat ["vertex "
,bf x , " "
,bf y , " "
,bf z]
triangle :: (3, 3, 3) -> Builder
triangle (a,b,c) = "facet normal 0 0 0\nouter loop\n"
<> vertex a <> "\n"
<> vertex b <> "\n"
<> vertex c
<> "\nendloop\nendfacet\n"
 
formatWithString triangles = text
where
stlHeader = "solid ImplictCADExport\n"
stlFooter = "endsolid ImplictCADExport\n"
vertex :: 3 -> String
vertex (x,y,z) = "vertex " ++ show x ++ " " ++ show y ++ " " ++ show z
stlTriangle :: (3, 3, 3) -> String
stlTriangle (a,b,c) =
"facet normal 0 0 0\n"
++ "outer loop\n"
++ vertex a ++ "\n"
++ vertex b ++ "\n"
++ vertex c ++ "\n"
++ "endloop\n"
++ "endfacet\n"
text = stlHeader
++ (concat $ map stlTriangle triangles)
++ stlFooter
 
model = union [
rect3R 0 (0,0,0) (20,20,20),
translate (30,30,30) (rect3R 0 (0,0,0) (40,40,40))
]
 
 
main = defaultMain [
bgroup "writing stl" [
bench "with Text.Lazy" $ nfIO $ TextIO.writeFile "/dev/null" $ formatWithLazyText $ discreteAprox 1 model,
bench "with String" $ nfIO $ writeFile "/dev/null" $ formatWithString $ discreteAprox 1 model
]
]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.