Last active
October 13, 2015 22:39
-
-
Save joostkremers/0e07a35c85758a2fcb52 to your computer and use it in GitHub Desktop.
Emacs: timing count-char-in-string.el implementations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; http://emacs.1067599.n5.nabble.com/How-to-count-the-number-of-occurrences-of-a-character-in-a-string-td371732.html | |
(defvar num-repeats 100000) | |
(defun count-char-in-string-cl-count (char str) | |
(cl-count char str)) | |
(defun count-char-in-string-mapcar (char str) | |
(apply '+ (mapcar (lambda (x) (if (= x char) 1 0)) | |
str))) | |
(defun count-char-in-string-mapc (char str) | |
(let ((num-matches 0)) | |
(mapc (lambda (ascii) | |
(when (eq char ascii) | |
(setq num-matches (1+ num-matches)))) | |
(append str nil)) | |
num-matches)) | |
(defun count-char-in-string-cdr (char str) | |
(let ((str-list (append str nil)) | |
(num-matches 0)) | |
(while str-list | |
(if (= (car str-list) char) | |
(setq num-matches (1+ num-matches))) | |
(setq str-list (cdr str-list))) | |
num-matches)) | |
;; (defun count-char-in-string-split-string (char str) | |
;; (let ((c (concat "[" (regexp-quote (char-to-string char)) "]"))) | |
;; (length (split-string c str t)))) | |
(defun count-char-in-string-string-match (char str) | |
"Count the number of times CHAR character appears in STR string." | |
;; (message "\n==========\nstr = %0s" str) | |
(let* ((num-matches 0) | |
(ptr 0) ; initiate pointer for string match | |
match-pos) | |
(while (<= ptr (length str)) | |
;; (message "ptr = %0d" ptr) | |
(setq match-pos (string-match-p | |
(regexp-quote (char-to-string char)) str ptr)) | |
(if match-pos | |
(progn | |
(setq ptr (1+ match-pos)) | |
;; (message "match-pos = %0d ptr = %0d" match-pos ptr) | |
(setq num-matches (1+ num-matches))) | |
(progn | |
(setq ptr (1+ (length str)))))) | |
;; (message "%0d occurrence%0s of `%c' char found in \"%s\"." | |
;; num-matches (if (/= 1 num-matches) "s" "") char str) | |
num-matches)) | |
(defun count-char-in-string-test (fn) | |
(let ((t1 (float-time)) | |
t2) | |
(dotimes (i num-repeats) | |
(funcall fn ?a "abcda") | |
(funcall fn ?z "abcda") | |
(funcall fn ?f "falalala") | |
(funcall fn ?l "falalalaabcds") | |
(funcall fn ?^ "f^la^lala^dabra^^") | |
(funcall fn ?\\ "\\falalala\\")) | |
(setq t2 (float-time)) | |
(list fn (- t2 t1)))) | |
(defun count-char-in-string-run-tests () | |
(insert "\n") | |
(mapc (lambda (fn) | |
(let ((res (count-char-in-string-test fn))) | |
(insert (format "%-35s %f\n" (car res) (cadr res))))) | |
'(cl-count | |
count-char-in-string-cl-count | |
count-char-in-string-mapcar | |
count-char-in-string-mapc | |
count-char-in-string-cdr | |
count-char-in-string-string-match)) | |
(insert "\n")) | |
;; cl-count 0.697451 | |
;; count-char-in-string-cl-count 0.743856 | |
;; count-char-in-string-mapcar 2.196961 | |
;; count-char-in-string-mapc 1.817378 | |
;; count-char-in-string-cdr 1.669147 | |
;; count-char-in-string-string-match 3.815134 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment