我的程序使用GPG签名文件。我在Haskell中使用GPGME,问题是它比从命令行使用GPG慢大约16倍。这是一个示例:
Haskell代码:
module Main where
import qualified Data.ByteString as B
import qualified Crypto.Gpgme as Gpg
main :: IO ()
main = do
fileContents <- B.readFile "randomfile"
eitherSigned <- sign fileContents
case eitherSigned of
Left err -> print err
Right signed ->
B.writeFile "signedfile" signed
sign :: B.ByteString -> IO (Either [Gpg.InvalidKey] B.ByteString)
sign bs =
let
sign' :: Gpg.Ctx -> IO (Either [Gpg.InvalidKey] Gpg.Plain)
sign' ctx = Gpg.sign ctx [] Gpg.Normal bs
in
Gpg.withCtx "/home/t/.gnupg" "C" Gpg.OpenPGP sign'
我生成了一个10MB的垃圾文件
$ dd if=/dev/zero of=randomfile bs=10000000 count=1
我通过GPG从命令行对其进行了签名:
$ time gpg -s randomfile
并获得
gpg -s randomfile 0.14s user 0.00s system 99% cpu 0.136 total
我用Haskell程序用
签名$ time stack exec hasksign
并获得
stack exec hasksign 0.27s user 0.07s system 14% cpu 2.239 total
我尝试再次运行具有分析功能的Haskell代码,并得到以下结果:
hasksign +RTS -p -RTS randomfile
total time = 2.21 secs (2208 ticks @ 1000 us, 1 processor)
total alloc = 115,627,312 bytes (excludes profiling overheads)
COST CENTRE MODULE SRC %time %alloc
newCtx Crypto.Gpgme.Ctx src/Crypto/Gpgme/Ctx.hs:(21,1)-(51,21) 77.0 0.0
signIntern Crypto.Gpgme.Crypto src/Crypto/Gpgme/Crypto.hs:(310,1)-(354,14) 20.4 8.6
collectResult.go.\ Crypto.Gpgme.Internal src/Crypto/Gpgme/Internal.hs:(29,21)-(34,46) 2.1 81.9
main Main src/Main.hs:(7,1)-(13,43) 0.1 8.7
我已经查看了newCtx函数所花费的时间,但是我不清楚花多少钱。