arithmetic.bm 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ;;; -*- mode: scheme; coding: utf-8; -*-
  2. ;;; Integer arithmetic.
  3. ;;;
  4. ;;; Copyright 2010, 2012 Free Software Foundation, Inc.
  5. ;;;
  6. ;;; This program is free software; you can redistribute it and/or
  7. ;;; modify it under the terms of the GNU Lesser General Public License
  8. ;;; as published by the Free Software Foundation; either version 3, or
  9. ;;; (at your option) any later version.
  10. ;;;
  11. ;;; This program is distributed in the hope that it will be useful,
  12. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU Lesser General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU Lesser General Public
  17. ;;; License along with this software; see the file COPYING.LESSER. If
  18. ;;; not, write to the Free Software Foundation, Inc., 51 Franklin
  19. ;;; Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. (define-module (benchmarks arithmetic)
  21. #:use-module (benchmark-suite lib))
  22. (define-syntax repeat
  23. (lambda (s)
  24. ;; Construct an expression of the form `(OP (OP (OP SEED)))', with a
  25. ;; depth of COUNT.
  26. (syntax-case s (<>)
  27. ((_ (op x <>) seed count) ;; binary OP
  28. (number? (syntax->datum #'count))
  29. (let loop ((count (syntax->datum #'count))
  30. (result #'seed))
  31. (if (= 0 count)
  32. result
  33. (loop (1- count)
  34. (with-syntax ((result result))
  35. #'(op x result))))))
  36. ((_ (op <>) seed count) ;; unary OP
  37. (number? (syntax->datum #'count))
  38. (let loop ((count (syntax->datum #'count))
  39. (result #'seed))
  40. (if (= 0 count)
  41. result
  42. (loop (1- count)
  43. (with-syntax ((result result))
  44. #'(op result)))))))))
  45. (with-benchmark-prefix "fixnum"
  46. (benchmark "1+" #e1e7
  47. (repeat (1+ <>) 2 100))
  48. (benchmark "1-" #e1e7
  49. (repeat (1- <>) 2 100))
  50. (benchmark "+" #e1e7
  51. (repeat (+ 2 <>) 7 100))
  52. (benchmark "-" #e1e7
  53. (repeat (- 2 <>) 7 100))
  54. (benchmark "*" #e1e7
  55. (repeat (* 1 <>) 1 100))
  56. (benchmark "/" #e1e7
  57. (repeat (/ 2 <>) 1 100)))