Last active
June 19, 2018 10:24
-
-
Save kuribas/c482217b48b8012dc67cb036f7805a60 to your computer and use it in GitHub Desktop.
speedtest
This file contains 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
#include <stdio.h> | |
#include <stdlib.h> | |
inline long f(long x, long y) { | |
return x + (y*2); | |
} | |
long g(long n) { | |
long s = 0; | |
for (long i = 0; i < n; i++) { | |
s += f(i, i+1); | |
} | |
return s; | |
} | |
void main(int argc, char *argv[]) { | |
long n = atol(argv[1]); | |
printf("%ld", g(n)); | |
} |
This file contains 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
module Main where | |
import Data.List | |
import Control.Monad.ST | |
import Data.STRef | |
import Data.Foldable | |
import System.Environment (getArgs) | |
f :: Int -> Int -> Int | |
f x y = x + (y * 2) | |
sum' :: [Int] -> Int | |
sum' = foldl' (+) 0 | |
g :: Int -> Int | |
g n = sum $ map (\x -> f x (x+1)) [0..n-1] | |
h :: Int -> Int | |
h n = runST $ do | |
r <- newSTRef (0::Int) | |
forM_ [(0::Int)..n-1] $ \i -> | |
modifySTRef' r (\x -> x + f i (i+1)) | |
readSTRef r | |
main = do | |
[n] <- fmap read <$> getArgs | |
print $ g n |
This file contains 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
(defun f (x y) | |
(declare (type (signed-byte 32) x y)) | |
(+ x (* y 2))) | |
(defun g (n) | |
(declare (type (signed-byte 32) n)) | |
(let ((x 0)) | |
(dotimes (y n) | |
(setq x (+ x (f y (+ y 1))))) | |
x)) |
Author
kuribas
commented
Jun 18, 2018
•
(defun f (x y)
(declare (type fixnum x y)
(optimize (speed 3) (safety 1)))
(+ x (* y 2)))
(defun g (n)
(declare (type (signed-byte 32) n)
(optimize (speed 3) (safety 1)))
(let ((x 0))
(dotimes (y n) (declare (type (signed-byte 32) y) (type fixnum x) )
(setq x (+ x (f y (+ y 1)))))
x))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment