vlists.bm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. ;;; -*- mode: scheme; coding: iso-8859-1; -*-
  2. ;;; VLists.
  3. ;;;
  4. ;;; Copyright 2009 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 vlists)
  21. :use-module (srfi srfi-1)
  22. :use-module (ice-9 vlist)
  23. :use-module (benchmark-suite lib))
  24. ;; Note: Use `--iteration-factor' to change this.
  25. (define iterations 2000000)
  26. ;; The size of large lists.
  27. (define %list-size 700000)
  28. (define %big-list (make-list %list-size))
  29. (define %big-vlist (list->vlist %big-list))
  30. (define-syntax comparative-benchmark
  31. (syntax-rules ()
  32. ((_ benchmark-name iterations
  33. ((api ((name value) ...)))
  34. body ...)
  35. (benchmark (format #f "~A (~A)" benchmark-name 'api)
  36. iterations
  37. (let ((name value) ...)
  38. body ...)))
  39. ((_ benchmark-name iterations
  40. ((api bindings) apis ...)
  41. body ...)
  42. (begin
  43. (comparative-benchmark benchmark-name iterations
  44. ((api bindings))
  45. body ...)
  46. (comparative-benchmark benchmark-name iterations
  47. (apis ...)
  48. body ...)))))
  49. (with-benchmark-prefix "constructors"
  50. (comparative-benchmark "cons" 2
  51. ((srfi-1 ((cons cons) (null '())))
  52. (vlist ((cons vlist-cons) (null vlist-null))))
  53. (let loop ((i %list-size)
  54. (r null))
  55. (and (> i 0)
  56. (loop (1- i) (cons #t r)))))
  57. (comparative-benchmark "acons" 2
  58. ((srfi-1 ((acons alist-cons) (null '())))
  59. (vlist ((acons vhash-cons) (null vlist-null))))
  60. (let loop ((i %list-size)
  61. (r null))
  62. (if (zero? i)
  63. r
  64. (loop (1- i) (acons i i r))))))
  65. (define %big-alist
  66. (let loop ((i %list-size) (res '()))
  67. (if (zero? i)
  68. res
  69. (loop (1- i) (alist-cons i i res)))))
  70. (define %big-vhash
  71. (let loop ((i %list-size) (res vlist-null))
  72. (if (zero? i)
  73. res
  74. (loop (1- i) (vhash-cons i i res)))))
  75. (with-benchmark-prefix "iteration"
  76. (comparative-benchmark "fold" 2
  77. ((srfi-1 ((fold fold) (lst %big-list)))
  78. (vlist ((fold vlist-fold) (lst %big-vlist))))
  79. (fold (lambda (x y) y) #t lst))
  80. (comparative-benchmark "assoc" 70
  81. ((srfi-1 ((assoc assoc) (alst %big-alist)))
  82. (vhash ((assoc vhash-assoc) (alst %big-vhash))))
  83. (let loop ((i (quotient %list-size 3)))
  84. (and (> i 0)
  85. (begin
  86. (assoc i alst)
  87. (loop (- i 5000)))))))