LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How much is a char in a string

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"

 

original5.png

0 Kudos
Message 1 of 27
(4,003 Views)

I tpyically don't believe claims of "faster" or "slower" unless I see some benchmarking code and benchmarking results. 😄

0 Kudos
Message 2 of 27
(3,998 Views)

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.

0 Kudos
Message 3 of 27
(3,994 Views)
0 Kudos
Message 4 of 27
(3,991 Views)

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:

 

0 Kudos
Message 5 of 27
(3,987 Views)

 

oops, a mistake ... the error clusters inside the For Loop  Smiley Embarassed

 

Bench_3.png

 

 

0 Kudos
Message 6 of 27
(3,985 Views)

Here's a quick comparison of a few methods. There are probably even faster ways.

 

 

Message 7 of 27
(3,976 Views)

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  Smiley Happy  (about how to do properly a benchmark test)
thank you altenbach

0 Kudos
Message 8 of 27
(3,962 Views)

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)


0 Kudos
Message 9 of 27
(3,945 Views)

not better  Smiley Frustrated    with asm  (call dll library)

 

count : 21ms

asm : 29 ms

 

code_asm_bench.png

 

 

0 Kudos
Message 10 of 27
(3,926 Views)