04-22-2012 04:25 PM - edited 04-22-2012 04:27 PM
my approach to count how many times is a character in a string.
Faster than other methods, as with "Match pattern + loop" or with "Search and Replace"
04-22-2012 04:36 PM
I tpyically don't believe claims of "faster" or "slower" unless I see some benchmarking code and benchmarking results. 😄
04-22-2012 04:49 PM
For a string of 8M chars, search&replace is 15% faster. (67ms vs 82ms). Make sure to wire the character to both the search and replace inputs.
04-22-2012 05:00 PM
04-22-2012 05:12 PM - edited 04-22-2012 05:12 PM
I am very suspicions about your benchmarking code, you compare apples to oranges: remove all these terminals from inside the loop. Watch out for constant folding. If all inputs ot a function are constants, it might get calculated only once and reused for later iterations, giving you a false very fast speed.
For small strings like in your example, performance is irrelevant, so do it once with a 8M long string input instead. Here you should see a difference.
Here is some code that is 8x faster for a 8M string input:

04-22-2012 05:13 PM
04-22-2012 05:25 PM
04-22-2012 07:00 PM - edited 04-22-2012 07:09 PM
I redid the test with a string of 10e6 char
I confirm your results, your "count-method" is the fastest
"There are probably even faster ways" ...
Faster Than the count-method (??) ... with assembler and a Call Library Function ?
I'd be interested to know how to do that, really.
count : 20 ms
replace : 88ms
spreadsheet : 134 ms
(Q6600 Core2 Quad 3GHz- XP sp3)
I learned something today
(about how to do properly a benchmark test)
thank you altenbach
04-22-2012 09:59 PM
count : 10 ms
spreadsheet : 43 ms
replace : 49 ms
When the substring to search is longer, the best method will not work.
Spreadsheet time seems to depend on the number of matches
Lenovo w520 (i7, 8GB, win7 64bit)
04-23-2012 04:51 AM