~groovestomp/micro-benchmarks

31a41b3de157828ee0f182f075eccab743f04a20 — Aaron Oman 2 years ago
First commit
A  => common-lisp/project-euler-01.cl +31 -0
@@ 1,31 @@
(defun multiple-of (x)
  (lambda (y)
    (= 0 (mod y x))))

(defun compute ()
  (let ((sum 0))
    (loop for i from 1 below 1000
          when (or (funcall (multiple-of 3) i)
                   (funcall (multiple-of 5) i))
          do (setq sum (+ sum i)))
    ;;;(format t "~S" sum)))
    ))

(defun timeit (function)
  (let ((real-base (get-internal-real-time)))
    (funcall function)
    (values (/ (- (get-internal-real-time) real-base) internal-time-units-per-second))))

(defun main ()
  (let ((times '())
        (tot '())
        (avg '()))
    (loop for i from 1 below 10000 do
          (setq times (nconc times (list (timeit #'compute)))))
    (setq tot (reduce #'+ times))
    (setq avg (/ tot (list-length times)))
    (format t "~S" (coerce avg 'float))))

(main)

;(format t "~S" (* 10 (timeit #'compute)))

A  => go/project-euler-01.go +44 -0
@@ 1,44 @@
package main

import (
	"fmt"
	"time"
)

func ism5(i int) bool {
	return (i % 5) == 0
}

func ism3(i int) bool {
	return (i % 3) == 0
}

func compute() {
	sum := 0
	for i := 0; i < 1000; i++ {
		if ism3(i) || ism5(i) {
			sum += i
		}
	}

	//fmt.Printf("%v\n", sum)
}

func main() {
	totals := make([]float64, 0)

	for i := 0; i < 10000; i++ {
		start := time.Now()
		compute()
		duration := time.Now().Sub(start)
		totals = append(totals, duration.Seconds())
	}

	sum := 0.0
	for _, tot := range totals {
		sum += tot
	}

	avg := float64(sum) / float64(len(totals))
	fmt.Printf("%v\n", avg)
}

A  => python/project-euler-01.py +30 -0
@@ 1,30 @@
import functools
import time

def ism3(x):
    return (x % 3) == 0

def ism5(x):
    return (x % 5) == 0

def compute():
    sum = 0
    for i in range(1000):
        if ism3(i) or ism5(i):
            sum += i

    print(sum)

def loop():
    times = []
    for i in range(10000):
        start = time.perf_counter()
        compute
        duration = time.perf_counter() - start
        times.append(duration)

    tot = functools.reduce(lambda a,b: a + b, times)
    avg = tot / len(times)
    print(avg)

loop()

A  => scheme/project-euler-01.scm +45 -0
@@ 1,45 @@
(define is-m-3
  (lambda (x)
    (zero? (modulo x 3))))

(define is-m-5
  (lambda (x)
    (zero? (modulo x 5))))

(define compute
  (lambda ()
    (let ((sum 0))
      (define compute-internal
        (lambda (n sum)
          (cond ((> n 999) sum)
                ((or (is-m-3 n) (is-m-5 n))
                 (compute-internal (+ n 1) (+ sum n)))
                (#t
                 (compute-internal (+ n 1) sum)))))
      (compute-internal 0 0))))

(define timeit-guile
  (lambda (func)
    (let ((start (get-internal-real-time)))
      (func)
      (/ (- (get-internal-real-time) start) internal-time-units-per-second))))

(define main
  (lambda (timer)
    (define inner
      (lambda (n times)
        (if (> n 10000)
             times
             (inner (+ n 1) (append times (timer compute))))))
    (inner 0 '())))

(begin
  (define result (compute))
  (display result)
  (newline))

(begin
  (display (timeit-guile compute))
  (newline))

(main timeit-guile)