It seems as if there is no function in the standard library of type char -> string -> string
, which insert a char
in front of (or at the end of)
I made a comparison of the efficiency of different approaches:
I wrote a simple test:
let append_escaped s c = s ^ Char.escaped c
let append_make s c = s ^ String.make 1 c
let append_sprintf s c = Printf.sprintf "%s%c" s c
let _ =
let s = "some text" in
let c = 'a' in
for i = 1 to 100000000 do
let _ = append_(*escaped|make|sprintf*) s c in ()
done
I compiled it natively (Intel Core 2 Duo).
I ran the test three times for each option, timing it with time
, and calculating the mean real time elapsed.
Here are the results:
s ^ String.make 1 c
: 7.75s (100%)
s ^ Char.escaped c
: 8.30s (107%)
Printf.sprintf "%s%c" s c
: 68.57s (885%)