From eb99cddc92580c347ea0beff43ac2fc1f3e0341f Mon Sep 17 00:00:00 2001 From: Abastro Date: Sun, 23 Mar 2025 17:14:24 +0900 Subject: [PATCH] First measurement --- app/Main.hs | 32 ++++++++++++++++++++++++++------ mathematical-algorithms.cabal | 2 ++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 7dd6dff..d75498a 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,13 +1,33 @@ module Main (main) where +import Control.Exception import Data.Vector qualified as V import Poly +import System.Random +import System.TimeIt main :: IO () main = do - let f :: Poly Int = Poly (V.fromList [1, 2, 3]) - let g :: Poly Int = Poly (V.fromList [4, 5]) - putStrLn $ "f: " <> show f <> ", g: " <> show g - putStrLn $ "f + g: " <> show (f + g) - putStrLn $ "f * g: " <> show (f * g) - putStrLn $ "Karatsuba f * g: " <> show (normalize $ karatsubaMult f g) + do + let f :: Poly Int = Poly (V.fromList [1, 2, 3]) + let g :: Poly Int = Poly (V.fromList [4, 5]) + putStrLn $ "f: " <> show f <> ", g: " <> show g + putStrLn $ "f + g: " <> show (f + g) + putStrLn $ "Naive f * g: " <> show (f * g) + putStrLn $ "Karatsuba f * g: " <> show (normalize $ karatsubaMult f g) + + putStrLn "" + experimentFor 500 + experimentFor 1000 + where + experimentFor n = do + setStdGen $ mkStdGen 10 + let randomPoly size = Poly <$> V.replicateM size (randomRIO (-100, 100)) + putStrLn $ "Size " <> show n + f :: Poly Int <- randomPoly n + g :: Poly Int <- randomPoly n + putStrLn "naive:" + _ <- timeIt $ evaluate (f * g) + putStrLn "Karatsuba:" + _ <- timeIt $ evaluate (f * g) + putStrLn "Finished" diff --git a/mathematical-algorithms.cabal b/mathematical-algorithms.cabal index a98d258..e3474cf 100644 --- a/mathematical-algorithms.cabal +++ b/mathematical-algorithms.cabal @@ -20,6 +20,8 @@ common deps build-depends: base ^>=4.17.2.1, vector, + random, + timeit, library import: warnings, deps