Merge commit '47ca15c7dffd14a82e75c1a0aeeaf2e77f3fa5b4'
[bpt/guile.git] / benchmark-suite / benchmarks / ports.bm
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
20 (define-module (benchmarks ports)
21 #:use-module (ice-9 rdelim)
22 #:use-module (benchmark-suite lib))
23
24 (define-syntax sequence
25 (lambda (s)
26 ;; Create a sequence `(begin EXPR ...)' with COUNT occurrences of EXPR.
27 (syntax-case s ()
28 ((_ expr count)
29 (number? (syntax->datum #'count))
30 (cons #'begin
31 (make-list (syntax->datum #'count) #'expr))))))
32
33 (define (large-string s)
34 (string-concatenate (make-list (* iteration-factor 10000) s)))
35
36 (define %latin1-port
37 (let ((p (open-input-string (large-string "hello, world"))))
38 (set-port-encoding! p "ISO-8859-1")
39 p))
40
41 (define %utf8/ascii-port
42 (open-input-string (large-string "hello, world")))
43
44 (define %utf8/wide-port
45 (open-input-string (large-string "안녕하세요")))
46
47 \f
48 (with-benchmark-prefix "peek-char"
49
50 (benchmark "latin-1 port" 700
51 (sequence (peek-char %latin1-port) 1000))
52
53 (benchmark "utf-8 port, ascii character" 700
54 (sequence (peek-char %utf8/ascii-port) 1000))
55
56 (benchmark "utf-8 port, Korean character" 700
57 (sequence (peek-char %utf8/wide-port) 1000)))
58
59 (with-benchmark-prefix "char-ready?"
60
61 (benchmark "latin-1 port" 10000
62 (sequence (char-ready? %latin1-port) 1000))
63
64 (benchmark "utf-8 port, ascii character" 10000
65 (sequence (char-ready? %utf8/ascii-port) 1000))
66
67 (benchmark "utf-8 port, Korean character" 10000
68 (sequence (char-ready? %utf8/wide-port) 1000)))
69
70 ;; Keep the `read-char' benchmarks last as they consume input from the
71 ;; ports.
72
73 (with-benchmark-prefix "read-char"
74
75 (benchmark "latin-1 port" 10000
76 (sequence (read-char %latin1-port) 1000))
77
78 (benchmark "utf-8 port, ascii character" 10000
79 (sequence (read-char %utf8/ascii-port) 1000))
80
81 (benchmark "utf-8 port, Korean character" 10000
82 (sequence (read-char %utf8/wide-port) 1000)))
83
84 \f
85 (with-benchmark-prefix "rdelim"
86
87 (let ((str (string-concatenate (make-list 1000 "one line\n"))))
88 (benchmark "read-line" 1000
89 (let ((port (open-input-string str)))
90 (sequence (read-line port) 1000))))
91
92 (let ((str (large-string "Hello, world.\n")))
93 (benchmark "read-string" 200
94 (let ((port (open-input-string str)))
95 (read-string port)))))