Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Fast way to strip characters

 [1/12] from: kpeters:otaksoft at: 6-Oct-2009 21:02


Hi guys, what is a *fast* way to strip chars unwanted from a string? TIA Kai

 [2/12] from: compkarori:gma:il at: 7-Oct-2009 1:03


Use parse On Wed, Oct 7, 2009 at 5:02 PM, Kai Peters <kpeters-otaksoft.com> wrote:
> Hi guys, > what is a *fast* way to strip chars unwanted from a string?
<<quoted lines omitted: 3>>
> To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject.
-- Graham Chiu http://www.synapsedirect.com Synapse - the use from anywhere EMR.

 [3/12] from: Tom:Conlin:g:mail at: 6-Oct-2009 21:23


what he said Graham Chiu wrote:

 [4/12] from: izkata:gm:ail at: 7-Oct-2009 0:26


If all you're doing is stripping characters, I suggest 'trim -
>> trim/with {I'm so not here} {' }
== "Imsonothere" Is parse faster in this case? On Tue, Oct 6, 2009 at 11:23 PM, Tom <Tom.Conlin-gmail.com> wrote:
> what he said > Graham Chiu wrote:
<<quoted lines omitted: 18>>
> To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject.
-- =E5=A5=8F=E3=81=A7=E3=81=A6=E5=A4=A2

 [5/12] from: pwawood:gmai:l at: 7-Oct-2009 13:51


2009/10/7 Izkata <izkata-gmail.com>
> If all you're doing is stripping characters, I suggest 'trim - > > >> trim/with {I'm so not here} {' } > == "Imsonothere" > > Is parse faster in this case? >
Trim is much faster than parse for your example. It is a native function. speedtest: func [][ st: now/precise loop 10000 [stripped: trim/with teststring "' "] et: now/precise print ["10000 trim/with took:" difference et st] st: now/precise strip: charset [#" " #"'"] not-strip: complement strip loop 10000 [ stripped: copy "" parse teststring [any [copy str some not-strip (insert tail stripped str) | some strip]] ] et: now/precise print ["10000 parse took:" difference et st] ]
>> speedtest
10000 trim/with took: 0:00:00.007205 10000 parse took: 0:00:00.05729 Regards Peter

 [6/12] from: compkarori::gmail at: 7-Oct-2009 1:07


Peter Trim modifies the string in situ. Your trim code only does it once but parse is working 10,000 x. On Wed, Oct 7, 2009 at 6:51 PM, Peter Wood <pwawood-gmail.com> wrote:
> 2009/10/7 Izkata <izkata-gmail.com> >>
<<quoted lines omitted: 31>>
> To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject.
-- Graham Chiu http://www.synapsedirect.com Synapse - the use from anywhere EMR.

 [7/12] from: pwawood::gmail at: 7-Oct-2009 16:00


> Peter > > Trim modifies the string in situ. > > Your trim code only does it once but parse is working 10,000 x.
Oops, I forgot about Rebol's side effects for a moment. However the trim code did it 10,000 times but didn't have so much to do 9999 of them. This is a better test and still indicates trim is faster for this example: speedtest: func [][ st: now/precise loop 10000 [ teststring: copy "I'm so not here" stripped: trim/with teststring "' " ] et: now/precise print ["10000 trim/with took:" difference et st] st: now/precise strip: charset [#" " #"'"] not-strip: complement strip loop 10000 [ teststring: copy "I'm so not here" stripped: copy "" parse teststring [any [copy str some not-strip (insert tail stripped str)| some strip]] ] et: now/precise print ["10000 parse took:" difference et st] ]
>> speedtest
10000 trim/with took: 0:00:00.013725 10000 parse took: 0:00:00.073276 Regards Peter

 [8/12] from: petr:krenzelok:seznam:cz at: 7-Oct-2009 10:27


Hi list, far from being a parse guru, but here's a bit more hopefully optimised and easier version :-) speedtest: func [][ st: now/precise loop 10000 [ teststring: copy "I'm so not here" stripped: trim/with teststring "' " ] et: now/precise print ["10000 trim/with took:" difference et st] st: now/precise strip: charset [#" " #"'"] loop 10000 [ teststring: copy "I'm so not here" parse/all teststring [any [here: strip (remove here) | skip]] ] et: now/precise print ["10000 parse took:" difference et st] ] ->> speedtest 10000 trim/with took: 0:00:00.018 10000 parse took: 0:00:00.046 PS: without the /all refinement, spaces were not even considered .... however - by consecutively running speedtest, result times jump here and there ... Cheers, -pekr-

 [9/12] from: pwawood::gmail at: 7-Oct-2009 21:49


Here's a slightly extended test including Pekr's stripping the unwanted characters in place. The results show for this test: 1. Trim/with is almost 10 times faster than parse. 2. Parse is roughly 10 percent faster than parse/all. 3. Skip is slower than using the complement technique. Here are the results for 500 speedchecks: Average for 10000 trim/with was 0:00:00.007487802 Average for 10000 parse creating new string was 0:00:00.064615664 Average for 10000 parse/all in-situ with skip was 0:00:00.064453348 Average for 10000 parse/all in-situ with complement was 0:00:00.064148258 Average for 10000 parse in-situ with skip was 0:00:00.05863122 Average for 10000 parse in-situ with complement was 0:00:00.049951718 The test script follows. Regards Peter REBOL[] NUMBER_Of_RUNS: 500 speedtest: func [][ st: now/precise loop 10000 [ teststring: copy "I'm so not here" stripped: trim/with teststring "' " ] et: now/precise a: difference et st st: now/precise strip: charset [#" " #"'"] not-strip: complement strip loop 10000 [ teststring: copy "I'm so not here" stripped: copy "" parse teststring [any [copy str some not-strip (insert tail stripped str)| some strip]] ] et: now/precise b: difference et st st: now/precise strip: charset [#" " #"'"] loop 10000 [ teststring: copy "I'm so not here" parse/all teststring [any [here: strip (remove here) | skip]] ] et: now/precise c: difference et st st: now/precise strip: charset [#" " #"'"] not-strip: complement strip loop 10000 [ teststring: copy "I'm so not here" parse/all teststring [any [some not-strip | here: strip (remove here)]] ] et: now/precise d: difference et st st: now/precise strip: charset [#" " #"'"] loop 10000 [ teststring: copy "I'm so not here" parse teststring [any [here: strip (remove here) | skip]] ] et: now/precise e: difference et st st: now/precise strip: charset [#" " #"'"] not-strip: complement strip loop 10000 [ teststring: copy "I'm so not here" parse teststring [any [some not-strip | here: strip (remove here)]] ] et: now/precise f: difference et st ] aa: 0 bb: 0 cc: 0 dd: 0 ee: 0 ff: 0 loop NUMBER_Of_RUNS [ speedtest aa: aa + a bb: bb + b cc: cc + c dd: dd + d ee: ee + e ff: ff + f recycle ] print ["Average for 10000 trim/with was " aa / NUMBER_Of_RUNS] print ["Average for 10000 parse creating new string was " bb / NUMBER_Of_RUNS] print ["Average for 10000 parse/all in-situ with skip was " cc / NUMBER_Of_RUNS] print ["Average for 10000 parse/all in-situ with complement was" dd / NUMBER_Of_RUNS] print ["Average for 10000 parse in-situ with skip was " ee / NUMBER_Of_RUNS] print ["Average for 10000 parse in-situ with complement was " ff / NUMBER_Of_RUNS]

 [10/12] from: petr:krenzelok:seznam:cz at: 7-Oct-2009 17:26


Peter W A Wood napsal(a):
> Here's a slightly extended test including Pekr's stripping the > unwanted characters in place. The results show for this test: > > 1. Trim/with is almost 10 times faster than parse. >
I would stop here, and not read further on. What are such comparisons good for at all? replace or trim, both are just small functions, handy for just few cases. Add one small more complicated bit, and you are out of luck. -pekr-

 [11/12] from: dhsunanda:gmai:l at: 7-Oct-2009 16:50


> what is a *fast* way to strip chars unwanted from a string?
The replies so far assume you have an explicit list of the unwanted chars. If instead you are starting with a list of conforming chars, and you want to remove anything that does not conform, you need a slightly different approach. One way is to use 'trim twice: good-chars: " abcdefhgijklmnopqrstuvwxyz" target: "this: is a string!" trim/with target trim/with copy target good-chars == "this is a string" Other ways include using parse with the complement of a bit set. But that may not be forward compatible with R3 Sunanda.

 [12/12] from: semseddinm:bircom at: 8-Oct-2009 9:56


This is brilliant :)

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted