srfi-13.bm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. ;;; coding: latin1 -*- mode: scheme; coding: latin-1; -*-
  2. ;;; srfi-13.bm
  3. ;;;
  4. ;;; Copyright (C) 2009 Free Software Foundation, Inc.
  5. ;;;
  6. ;;;
  7. ;;; This program is free software; you can redistribute it and/or
  8. ;;; modify it under the terms of the GNU Lesser General Public License
  9. ;;; as published by the Free Software Foundation; either version 3, or
  10. ;;; (at your option) any later version.
  11. ;;;
  12. ;;; This program is distributed in the hope that it will be useful,
  13. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU Lesser General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU Lesser General Public
  18. ;;; License along with this software; see the file COPYING.LESSER. If
  19. ;;; not, write to the Free Software Foundation, Inc., 51 Franklin
  20. ;;; Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. (define-module (benchmarks strings)
  22. :use-module (benchmark-suite lib))
  23. (seed->random-state 1)
  24. (define short-string "Hi")
  25. (define medium-string
  26. "ARMA virumque cano, Troiae qui primus ab oris
  27. Italiam, fato profugus, Laviniaque venit")
  28. (define long-string
  29. (string-tabulate
  30. (lambda (n) (integer->char (+ 32 (random 90))))
  31. 1000))
  32. (define short-chlist (string->list short-string))
  33. (define medium-chlist (string->list medium-string))
  34. (define long-chlist (string->list long-string))
  35. (define str1 (string-copy short-string))
  36. (define str2 (string-copy medium-string))
  37. (define str3 (string-copy long-string))
  38. (with-benchmark-prefix "strings"
  39. (with-benchmark-prefix "predicates"
  40. (benchmark "string?" 1190000
  41. (string? short-string)
  42. (string? medium-string)
  43. (string? long-string))
  44. (benchmark "null?" 969000
  45. (string-null? short-string)
  46. (string-null? medium-string)
  47. (string-null? long-string))
  48. (benchmark "any" 94000
  49. (string-any #\a short-string)
  50. (string-any #\a medium-string)
  51. (string-any #\a long-string))
  52. (benchmark "every" 94000
  53. (string-every #\a short-string)
  54. (string-every #\a medium-string)
  55. (string-every #\a long-string)))
  56. (with-benchmark-prefix "constructors"
  57. (benchmark "string" 5000
  58. (apply string short-chlist)
  59. (apply string medium-chlist)
  60. (apply string long-chlist))
  61. (benchmark "list->" 4500
  62. (list->string short-chlist)
  63. (list->string medium-chlist)
  64. (list->string long-chlist))
  65. (benchmark "reverse-list->" 5000
  66. (reverse-list->string short-chlist)
  67. (reverse-list->string medium-chlist)
  68. (reverse-list->string long-chlist))
  69. (benchmark "make" 22000
  70. (make-string 250 #\x))
  71. (benchmark "tabulate" 17000
  72. (string-tabulate integer->char 250))
  73. (benchmark "join" 5500
  74. (string-join (list short-string medium-string long-string) "|" 'suffix)))
  75. (with-benchmark-prefix "list/string"
  76. (benchmark "->list" 7300
  77. (string->list short-string)
  78. (string->list medium-string)
  79. (string->list long-string))
  80. (benchmark "split" 60000
  81. (string-split short-string #\a)
  82. (string-split medium-string #\a)
  83. (string-split long-string #\a)))
  84. (with-benchmark-prefix "selection"
  85. (benchmark "ref" 660
  86. (let loop ((k 0))
  87. (if (< k (string-length short-string))
  88. (begin
  89. (string-ref short-string k)
  90. (loop (+ k 1)))))
  91. (let loop ((k 0))
  92. (if (< k (string-length medium-string))
  93. (begin
  94. (string-ref medium-string k)
  95. (loop (+ k 1)))))
  96. (let loop ((k 0))
  97. (if (< k (string-length long-string))
  98. (begin
  99. (string-ref long-string k)
  100. (loop (+ k 1))))))
  101. (benchmark "copy" 20000
  102. (string-copy short-string)
  103. (string-copy medium-string)
  104. (string-copy long-string)
  105. (substring/copy short-string 0 1)
  106. (substring/copy medium-string 10 20)
  107. (substring/copy long-string 100 200))
  108. (benchmark "pad" 34000
  109. (string-pad short-string 100)
  110. (string-pad medium-string 100)
  111. (string-pad long-string 100))
  112. (benchmark "trim trim-right trim-both" 60000
  113. (string-trim short-string char-alphabetic?)
  114. (string-trim medium-string char-alphabetic?)
  115. (string-trim long-string char-alphabetic?)
  116. (string-trim-right short-string char-alphabetic?)
  117. (string-trim-right medium-string char-alphabetic?)
  118. (string-trim-right long-string char-alphabetic?)
  119. (string-trim-both short-string char-alphabetic?)
  120. (string-trim-both medium-string char-alphabetic?)
  121. (string-trim-both long-string char-alphabetic?)))
  122. (with-benchmark-prefix "modification"
  123. (set! str1 (string-copy short-string))
  124. (set! str2 (string-copy medium-string))
  125. (set! str3 (string-copy long-string))
  126. (benchmark "set!" 3000
  127. (let loop ((k 1))
  128. (if (< k (string-length short-string))
  129. (begin
  130. (string-set! str1 k #\x)
  131. (loop (+ k 1)))))
  132. (let loop ((k 20))
  133. (if (< k (string-length medium-string))
  134. (begin
  135. (string-set! str2 k #\x)
  136. (loop (+ k 1)))))
  137. (let loop ((k 900))
  138. (if (< k (string-length long-string))
  139. (begin
  140. (string-set! str3 k #\x)
  141. (loop (+ k 1))))))
  142. (set! str1 (string-copy short-string))
  143. (set! str2 (string-copy medium-string))
  144. (set! str3 (string-copy long-string))
  145. (benchmark "sub-move!" 230000
  146. (substring-move! short-string 0 2 str2 10)
  147. (substring-move! medium-string 10 20 str3 20))
  148. (set! str1 (string-copy short-string))
  149. (set! str2 (string-copy medium-string))
  150. (set! str3 (string-copy long-string))
  151. (benchmark "fill!" 230000
  152. (string-fill! str1 #\y 0 1)
  153. (string-fill! str2 #\y 10 20)
  154. (string-fill! str3 #\y 20 30))
  155. (with-benchmark-prefix "comparison"
  156. (benchmark "compare compare-ci" 140000
  157. (string-compare short-string medium-string string<? string=? string>?)
  158. (string-compare long-string medium-string string<? string=? string>?)
  159. (string-compare-ci short-string medium-string string<? string=? string>?)
  160. (string-compare-ci long-string medium-string string<? string=? string>?))
  161. (benchmark "hash hash-ci" 1000
  162. (string-hash short-string)
  163. (string-hash medium-string)
  164. (string-hash long-string)
  165. (string-hash-ci short-string)
  166. (string-hash-ci medium-string)
  167. (string-hash-ci long-string))))
  168. (with-benchmark-prefix "searching" 20000
  169. (benchmark "prefix-length suffix-length" 270
  170. (string-prefix-length short-string
  171. (string-append short-string medium-string))
  172. (string-prefix-length long-string
  173. (string-append long-string medium-string))
  174. (string-suffix-length short-string
  175. (string-append medium-string short-string))
  176. (string-suffix-length long-string
  177. (string-append medium-string long-string))
  178. (string-prefix-length-ci short-string
  179. (string-append short-string medium-string))
  180. (string-prefix-length-ci long-string
  181. (string-append long-string medium-string))
  182. (string-suffix-length-ci short-string
  183. (string-append medium-string short-string))
  184. (string-suffix-length-ci long-string
  185. (string-append medium-string long-string)))
  186. (benchmark "prefix? suffix?" 270
  187. (string-prefix? short-string
  188. (string-append short-string medium-string))
  189. (string-prefix? long-string
  190. (string-append long-string medium-string))
  191. (string-suffix? short-string
  192. (string-append medium-string short-string))
  193. (string-suffix? long-string
  194. (string-append medium-string long-string))
  195. (string-prefix-ci? short-string
  196. (string-append short-string medium-string))
  197. (string-prefix-ci? long-string
  198. (string-append long-string medium-string))
  199. (string-suffix-ci? short-string
  200. (string-append medium-string short-string))
  201. (string-suffix-ci? long-string
  202. (string-append medium-string long-string)))
  203. (benchmark "index index-right rindex" 100000
  204. (string-index short-string #\T)
  205. (string-index medium-string #\T)
  206. (string-index long-string #\T)
  207. (string-index-right short-string #\T)
  208. (string-index-right medium-string #\T)
  209. (string-index-right long-string #\T)
  210. (string-rindex short-string #\T)
  211. (string-rindex medium-string #\T)
  212. (string-rindex long-string #\T))
  213. (benchmark "skip skip-right?" 100000
  214. (string-skip short-string char-alphabetic?)
  215. (string-skip medium-string char-alphabetic?)
  216. (string-skip long-string char-alphabetic?)
  217. (string-skip-right short-string char-alphabetic?)
  218. (string-skip-right medium-string char-alphabetic?)
  219. (string-skip-right long-string char-alphabetic?))
  220. (benchmark "count" 10000
  221. (string-count short-string char-alphabetic?)
  222. (string-count medium-string char-alphabetic?)
  223. (string-count long-string char-alphabetic?))
  224. (benchmark "contains contains-ci" 34000
  225. (string-contains short-string short-string)
  226. (string-contains medium-string (substring medium-string 10 15))
  227. (string-contains long-string (substring long-string 100 130))
  228. (string-contains-ci short-string short-string)
  229. (string-contains-ci medium-string (substring medium-string 10 15))
  230. (string-contains-ci long-string (substring long-string 100 130)))
  231. (set! str1 (string-copy short-string))
  232. (set! str2 (string-copy medium-string))
  233. (set! str3 (string-copy long-string))
  234. (benchmark "upcase downcase upcase! downcase!" 600
  235. (string-upcase short-string)
  236. (string-upcase medium-string)
  237. (string-upcase long-string)
  238. (string-downcase short-string)
  239. (string-downcase medium-string)
  240. (string-downcase long-string)
  241. (string-upcase! str1 0 1)
  242. (string-upcase! str2 10 20)
  243. (string-upcase! str3 100 130)
  244. (string-downcase! str1 0 1)
  245. (string-downcase! str2 10 20)
  246. (string-downcase! str3 100 130)))
  247. (with-benchmark-prefix "readers"
  248. (benchmark "read token, method 1" 1200
  249. (let ((buf (make-string 512)))
  250. (let loop ((i 0))
  251. (if (< i 512)
  252. (begin
  253. (string-set! buf i #\x)
  254. (loop (+ i 1)))
  255. buf))))
  256. (benchmark "read token, method 2" 1200
  257. (let ((lst '()))
  258. (let loop ((i 0))
  259. (set! lst (append! lst (list #\x)))
  260. (if (< i 512)
  261. (loop (+ i 1))
  262. (list->string lst)))))))