Skip to content

Instantly share code, notes, and snippets.

@kuribas
Last active June 19, 2018 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kuribas/c482217b48b8012dc67cb036f7805a60 to your computer and use it in GitHub Desktop.
Save kuribas/c482217b48b8012dc67cb036f7805a60 to your computer and use it in GitHub Desktop.
speedtest
#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));
}
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
(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))
@kuribas
Copy link
Author

kuribas commented Jun 18, 2018

(declaim (inline f))
(declaim (optimize (speed 3) (safety 1)))
(defun f (x y)
  (declare (type fixnum x y))
  (+ x (* y 2)))

(defun g (n)
  (declare (type fixnum n))
  (let ((x 0))
    (dotimes (y n)
      (setq x (+ x (f y (+ y 1)))))
    x))

@varjagg
Copy link

varjagg 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