ports.bm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. ;;; ports.bm --- Port I/O. -*- mode: scheme; coding: utf-8; -*-
  2. ;;;
  3. ;;; Copyright (C) 2010-2014 Free Software Foundation, Inc.
  4. ;;;
  5. ;;; This program is free software; you can redistribute it and/or
  6. ;;; modify it under the terms of the GNU Lesser General Public License
  7. ;;; as published by the Free Software Foundation; either version 3, or
  8. ;;; (at your option) any later version.
  9. ;;;
  10. ;;; This program is distributed in the hope that it will be useful,
  11. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;;; GNU Lesser General Public License for more details.
  14. ;;;
  15. ;;; You should have received a copy of the GNU Lesser General Public
  16. ;;; License along with this software; see the file COPYING.LESSER. If
  17. ;;; not, write to the Free Software Foundation, Inc., 51 Franklin
  18. ;;; Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (benchmarks ports)
  20. #:use-module (ice-9 rdelim)
  21. #:use-module (benchmark-suite lib))
  22. (define-syntax sequence
  23. (lambda (s)
  24. ;; Create a sequence `(begin EXPR ...)' with COUNT occurrences of EXPR.
  25. (syntax-case s ()
  26. ((_ expr count)
  27. (number? (syntax->datum #'count))
  28. (cons #'begin
  29. (make-list (syntax->datum #'count) #'expr))))))
  30. (define (large-string s)
  31. (string-concatenate (make-list (* iteration-factor 10000) s)))
  32. (define %latin1-port
  33. (let ((p (open-input-string (large-string "hello, world"))))
  34. (set-port-encoding! p "ISO-8859-1")
  35. p))
  36. (define %utf8/ascii-port
  37. (open-input-string (large-string "hello, world")))
  38. (define %utf8/wide-port
  39. (open-input-string (large-string "안녕하세요")))
  40. (with-benchmark-prefix "peek-char"
  41. (benchmark "latin-1 port" 700
  42. (sequence (peek-char %latin1-port) 1000))
  43. (benchmark "utf-8 port, ascii character" 700
  44. (sequence (peek-char %utf8/ascii-port) 1000))
  45. (benchmark "utf-8 port, Korean character" 700
  46. (sequence (peek-char %utf8/wide-port) 1000)))
  47. (with-benchmark-prefix "char-ready?"
  48. (benchmark "latin-1 port" 10000
  49. (sequence (char-ready? %latin1-port) 1000))
  50. (benchmark "utf-8 port, ascii character" 10000
  51. (sequence (char-ready? %utf8/ascii-port) 1000))
  52. (benchmark "utf-8 port, Korean character" 10000
  53. (sequence (char-ready? %utf8/wide-port) 1000)))
  54. ;; Keep the `read-char' benchmarks last as they consume input from the
  55. ;; ports.
  56. (with-benchmark-prefix "read-char"
  57. (benchmark "latin-1 port" 10000
  58. (sequence (read-char %latin1-port) 1000))
  59. (benchmark "utf-8 port, ascii character" 10000
  60. (sequence (read-char %utf8/ascii-port) 1000))
  61. (benchmark "utf-8 port, Korean character" 10000
  62. (sequence (read-char %utf8/wide-port) 1000)))
  63. (with-benchmark-prefix "rdelim"
  64. (let ((str (string-concatenate (make-list 1000 "one line\n"))))
  65. (benchmark "read-line" 1000
  66. (let ((port (open-input-string str)))
  67. (sequence (read-line port) 1000))))
  68. (let ((str (large-string "Hello, world.\n")))
  69. (benchmark "read-string" 200
  70. (let ((port (open-input-string str)))
  71. (read-string port)))))