| 1 |
;; |
| 2 |
;; testing mutable and immutable char-set lookup speed |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.time) |
| 6 |
(use data.random) |
| 7 |
(use gauche.sequence) |
| 8 |
(use scheme.list) |
| 9 |
(use scheme.charset) |
| 10 |
(use srfi.42) |
| 11 |
|
| 12 |
(define *cats* '(Lu Ll Lt Lm Lo Mn Nd Nl No Po Sm Zs Cc Co)) |
| 13 |
|
| 14 |
;; Returns a map |
| 15 |
(define (setup-char-sets) |
| 16 |
(let1 vec (vector-tabulate (length *cats*) (^_ (char-set))) |
| 17 |
(do-ec (: c 0 #x20000) |
| 18 |
(let1 ch (ucs->char c) |
| 19 |
(if-let1 j (list-index (cute eq? <> (char-general-category ch)) |
| 20 |
*cats*) |
| 21 |
(char-set-adjoin! (~ vec j) ch)))) |
| 22 |
(map (^[cs] (cons cs (char-set-freeze cs))) vec))) |
| 23 |
|
| 24 |
(define (setup-input-data) |
| 25 |
(take (generator->lseq (chars$ #[\x0000;-\x1ffff;])) 2000000)) |
| 26 |
|
| 27 |
(define (runner-1 cs input) |
| 28 |
(^[] (dolist [i input] (char-set-contains? cs i)))) |
| 29 |
|
| 30 |
;; returns (cat <time-result-mutable> <time-result-immutable>) |
| 31 |
(define (run-benchmark csss input) |
| 32 |
($ map (^x (list (caar x) (cdar x) (cdadr x))) |
| 33 |
$ (cut slices <> 2) |
| 34 |
$ cdr $ time-these 1000 |
| 35 |
$ append-map (^[cat css] |
| 36 |
`((,cat . ,(runner-1 (car css) input)) |
| 37 |
(,#"i~cat" . ,(runner-1 (cdr css) input)))) |
| 38 |
*cats* csss)) |
| 39 |
|
| 40 |
(define (report-benchmark res) |
| 41 |
(define (row title ta tb) |
| 42 |
(format #t "~6a ~8@a ~8@a ~8@a ~8@a ~8@a ~8@a ~8@a\n" |
| 43 |
title |
| 44 |
(fmt (time-result-real ta)) |
| 45 |
(fmt (time-result-user ta)) |
| 46 |
(fmt (time-result-sys ta)) |
| 47 |
(fmt (time-result-real tb)) |
| 48 |
(fmt (time-result-user tb)) |
| 49 |
(fmt (time-result-sys tb)) |
| 50 |
(fmt (* 100 (/ (time-result-user ta) (time-result-user tb)))))) |
| 51 |
(format #t " mutable immutable\n") |
| 52 |
(format #t "category real user sys real user sys speed ratio\n") |
| 53 |
(dolist [x res] (apply row x))) |
| 54 |
|
| 55 |
(define (report-charset-stats charsets) |
| 56 |
(define (cs-chunks cs) |
| 57 |
(length (filter (^p (>= (cdr p) #x80)) |
| 58 |
((with-module gauche.internal %char-set-ranges) cs)))) |
| 59 |
(define (cs-report-1 category css) |
| 60 |
(format #t "~6a ~4@a ranges, ~6@a chars\n" |
| 61 |
category (cs-chunks (car css)) (char-set-size (car css)))) |
| 62 |
(for-each cs-report-1 *cats* charsets)) |
| 63 |
|
| 64 |
(define (fmt num) |
| 65 |
(let* ([integral (floor->exact num)] |
| 66 |
[fraction (- num integral)] |
| 67 |
[base (* fraction 1000)] |
| 68 |
[base1 (floor->exact base)] |
| 69 |
[bfraction (- base base1)]) |
| 70 |
(when (>= bfraction 0.5) (inc! base1)) |
| 71 |
(when (>= base1 1000) (inc! integral) (dec! base1 1000)) |
| 72 |
(format "~d.~3,'0d" integral base1))) |
| 73 |
|
| 74 |
(define (main args) |
| 75 |
(let1 charsets (setup-char-sets) ; ((<mutable> . <immutable>) ...) |
| 76 |
(report-benchmark (run-benchmark charsets (setup-input-data))) |
| 77 |
(print) |
| 78 |
(report-charset-stats charsets))) |