speedup.scm 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ;; Fibers: cooperative, event-driven user-space threads.
  2. ;;;; Copyright (C) 2016 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 3 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;;
  18. (define-module (tests speedup)
  19. #:use-module (ice-9 threads)
  20. #:use-module (fibers))
  21. (define failed? #f)
  22. (define-syntax-rule (do-times n exp)
  23. (let lp ((count n))
  24. (let ((count (1- count)))
  25. exp
  26. (unless (zero? count) (lp count)))))
  27. (define (time thunk)
  28. (let ((start (get-internal-real-time)))
  29. (thunk)
  30. (/ (- (get-internal-real-time) start)
  31. 1.0 internal-time-units-per-second)))
  32. (define-syntax-rule (measure-speedup exp)
  33. (begin
  34. (format #t "speedup for ~s: " 'exp)
  35. (force-output)
  36. (let ((thunk (lambda () exp)))
  37. (let ((t1 (time (lambda ()
  38. (run-fibers thunk #:parallelism 1 #:drain? #t)))))
  39. (format #t "~a s" t1)
  40. (let ((t2 (time (lambda () (run-fibers thunk #:drain? #t)))))
  41. (format #t " / ~a s = ~ax (~a cpus)\n" t2 (/ t1 t2)
  42. (current-processor-count)))))))
  43. (define (loop-to n) (let lp ((i 0)) (when (< i n) (lp (1+ i)))))
  44. (define (alloc-to words n)
  45. (let lp ((i 0) (x #f))
  46. (if (< i n)
  47. (lp (1+ i) (make-vector (- words 2) #f))
  48. x)))
  49. (measure-speedup
  50. (do-times 100000 (spawn-fiber (lambda () #t) #:parallel? #t)))
  51. (measure-speedup
  52. (do-times 40000 (spawn-fiber (lambda () (sleep 1)) #:parallel? #t)))
  53. (measure-speedup
  54. (do-times 100000 (spawn-fiber (lambda () (loop-to #e1e4)) #:parallel? #t)))
  55. (measure-speedup
  56. (do-times 10000 (spawn-fiber (lambda () (loop-to #e1e5)) #:parallel? #t)))
  57. (measure-speedup
  58. (do-times 1000 (spawn-fiber (lambda () (loop-to #e1e6)) #:parallel? #t)))
  59. (measure-speedup
  60. (do-times 100000 (spawn-fiber (lambda () (alloc-to 4 #e1e3)) #:parallel? #t)))
  61. (measure-speedup
  62. (do-times 10000 (spawn-fiber (lambda () (alloc-to 4 #e1e4)) #:parallel? #t)))
  63. (measure-speedup
  64. (do-times 1000 (spawn-fiber (lambda () (alloc-to 4 #e1e5)) #:parallel? #t)))