Skip to content

Instantly share code, notes, and snippets.

@hiiamboris
Created February 9, 2023 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiiamboris/c29f1d4f315a760a103b4bb46b48c183 to your computer and use it in GitHub Desktop.
Save hiiamboris/c29f1d4f315a760a103b4bb46b48c183 to your computer and use it in GitHub Desktop.
R3/Red cross profiling parse test
REBOL [] Red []
using-replace: func[str][
replace/all str "??" #"¿"
replace/all str "!!" #"¡"
replace/all str "a'" #"á"
replace/all str "e'" #"é"
replace/all str "i'" #"í"
replace/all str "o'" #"ó"
replace/all str "u'" #"ú"
replace/all str "u:" #"ü"
replace/all str {u"} #"ü"
replace/all str "n'" #"ñ"
replace/all str "n~" #"ñ"
str
]
start-ch: charset "?!aeioun"
using-parse-collect: func[str /local out][
out: make string! length? str
parse str [
collect into out any [
keep to start-ch [
"??" keep (#"¿")
| "!!" keep (#"¡")
| "a'" keep (#"á")
| "e'" keep (#"é")
| "i'" keep (#"í")
| "o'" keep (#"ó")
| "u'" keep (#"ú")
| "u:" keep (#"ü")
| {u"} keep (#"ü")
| "n'" keep (#"ñ")
| "n~" keep (#"ñ")
| keep 1 skip
]
keep to end
]
]
out
]
;; With some longer input data...
st: {??Quie'n me librara' de la vergu:enza y la muerte? Cada man~ana alabare' al Sen'or. !!Aleluya!}
st1000: make string! 1000 * length? st
insert/dup st1000 st 1000
either rebol [
profile/times [[using-replace copy st1000]] 100
profile/times [[using-parse-collect st1000]] 5000
][
clock/times [using-replace copy st1000] 10
clock/times [using-parse-collect st1000] 500
]
@Oldes
Copy link

Oldes commented Feb 9, 2023

@hiiamboris
Copy link
Author

There is a bug in the original test (missing | before keep to end) that made it stop after 1st replacement.
In fairer comparison code regardless of GC state Red (100ms) is only about 6x slower than R3 (14ms), which is acceptable for now:

REBOL [] Red [] 
; recycle/off
start-ch: charset "?!aeioun"
using-parse-collect: func[str /local out][
	out: clear {}
	parse str [collect after out [
		any [
			keep to start-ch [
				"??" keep (#"¿")
			|	"!!" keep (#"¡")
			|	"a'" keep (#"á")
			|	"e'" keep (#"é")
			|	"i'" keep (#"í")
			|	"o'" keep (#"ó")
			|	"u'" keep (#"ú")
			|	"u:" keep (#"ü")
			|	{u"} keep (#"ü")
			|	"n'" keep (#"ñ")
			|	"n~" keep (#"ñ")
			|	keep skip
			]
		]
		keep to end
	]]
	out
]
;; With some longer input data...
st: {??Quie'n me librara' de la vergu:enza y la muerte? Cada man~ana alabare' al Sen'or. !!Aleluya!}
st1000: make string! 4000 * length? st
take append st1000 "¿"
insert/dup st1000 st 1000

either rebol [
	profile/times [[loop 10 [using-parse-collect st1000]]] 10
][
	clock/times [using-parse-collect st1000] 10
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment