[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to count the number of occurrences of a character in a string?
From: |
Kaushal Modi |
Subject: |
Re: How to count the number of occurrences of a character in a string? |
Date: |
Tue, 13 Oct 2015 13:43:05 -0400 |
Thanks Eli. That's neat! I was over-engineering :)
I am, though, unable to quantify which approach is the fastest using
`benchmark`.
I ran the below wrapper progn block couple of times and I got
different results each time. Oddly enough, I even got negative
execution times.
What am I doing wrong?
RESULT:
100000 executions of string-match-p approach:
Elapsed time: 0.000330s
100000 executions of cl-count approach:
Elapsed time: 0.000357s
100000 executions of cdr approach:
Elapsed time: -0.000378s <<<<<<<<<<<<<<<<<<< negative time??
nil
100000 executions of string-match-p approach:
Elapsed time: 0.001972s
100000 executions of cl-count approach:
Elapsed time: 0.000728s
100000 executions of cdr approach:
Elapsed time: 0.000638s
nil
100000 executions of string-match-p approach:
Elapsed time: -0.000199s <<<<<<<<<<<<<<<<<<< negative time??
100000 executions of cl-count approach:
Elapsed time: 0.000766s
100000 executions of cdr approach:
Elapsed time: 0.001562s
nil
CODE:
(progn
(message "100000 executions of string-match-p approach: ")
(benchmark
100000
(progn
(defun my/count-char-in-string (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))
(my/count-char-in-string ?a "abcda")
(my/count-char-in-string ?z "abcda")
(my/count-char-in-string ?f "falalala")
(my/count-char-in-string ?l "falalalaabcds")
(my/count-char-in-string ?^ "f^la^lala^dabra^^")
(my/count-char-in-string ?\\ "\\falalala\\")))
(message "100000 executions of cl-count approach: ")
(benchmark
100000
(progn
(cl-count ?a "abcda")
(cl-count ?z "abcda")
(cl-count ?f "falalala")
(cl-count ?l "falalalaabcds")
(cl-count ?^ "f^la^lala^dabra^^")
(cl-count ?\\ "\\falalala\\")))
(message "100000 executions of cdr approach: ")
(benchmark
100000
(progn
(defun my2/count-char-in-string (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))
(my2/count-char-in-string ?a "abcda")
(my2/count-char-in-string ?z "abcda")
(my2/count-char-in-string ?f "falalala")
(my2/count-char-in-string ?l "falalalaabcds")
(my2/count-char-in-string ?^ "f^la^lala^dabra^^")
(my2/count-char-in-string ?\\ "\\falalala\\")))
nil)
--
Kaushal Modi
- How to count the number of occurrences of a character in a string?, Marcin Borkowski, 2015/10/12
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/12
- Re: How to count the number of occurrences of a character in a string?, Nick Dokos, 2015/10/12
- Re: How to count the number of occurrences of a character in a string?, Stefan Monnier, 2015/10/12
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/15
- Re: How to count the number of occurrences of a character in a string?, Eli Zaretskii, 2015/10/15
- Re: How to count the number of occurrences of a character in a string?, Eli Zaretskii, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?,
Kaushal Modi <=
- Re: How to count the number of occurrences of a character in a string?, Eli Zaretskii, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Eli Zaretskii, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/13
- Message not available
- Re: How to count the number of occurrences of a character in a string?, Brendan Halpin, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Kaushal Modi, 2015/10/13
- Message not available
- Re: How to count the number of occurrences of a character in a string?, Joost Kremers, 2015/10/13
- Re: How to count the number of occurrences of a character in a string?, Oleh Krehel, 2015/10/14
- Re: How to count the number of occurrences of a character in a string?, Eli Zaretskii, 2015/10/14
- Re: How to count the number of occurrences of a character in a string?, Michael Heerdegen, 2015/10/13