LoginSignup
6
7

More than 5 years have passed since last update.

Data.Vector.UnboxedとData.Vector.Storableの速度比較

Posted at

高速処理が必要なデータ解析ソフトウェアパッケージを開発している関係でいろいろなデータ列の処理速度を調べている。ベクトル演算や行列演算処理が整っているhmatrixを用いることが多く、そのVector型の効率をData.Vector.Unboxedと比較してみた。
hmatrixのVector型のご本尊はData.Vector.Storableなので、結局はそれとの比較になる。
以下が評価に用いたコード

-- testSpeedVector.hs
import qualified Data.Vector.Unboxed as UV
import qualified Data.Vector.Storable as SV
import Data.Time
import System.Random

main = do
  let x = take 10000000 $ randomRs (-10, 10) $ mkStdGen 1 :: [Double]

  t1 <- getCurrentTime
  -- for Data.Vector.Unboxed
  print $ UV.head $ UV.reverse $ UV.fromList x
  t2 <- getCurrentTime
  print $ diffUTCTime t2 t1

  t3 <- getCurrentTime
  -- for Data.Vector.Storable
  print $ SV.head $ SV.reverse $ SV.fromList x
  t4 <- getCurrentTime
  print $ diffUTCTime t4 t3

これを
ghc --make testSpeedVector.hs
でコンパイルし、実行してみたところ、
-3.0045005415246084
25.137353s
-3.0045005415246084
0.575353s

と、約50倍ほどData.VectorStorableの方が速くなってしまった。
完全に予想外である。せいぜいコンマ何秒の差かと予想していたのだが。

どこでこんなどでかい差が生まれているのだろう???

6
7
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7