I often need to concatenate a number of strings, say, X,Y and Z. Which is the better practice than the other. And why?
Code: Select all
string::concat(X,Y,Z)
Code: Select all
string::write(X,Y,Z)
Code: Select all
string::concat(X,Y,Z)
Code: Select all
string::write(X,Y,Z)
Code: Select all
string::format("X=%-20.20s, Y=%-20.20s, Z=%-20.20s.",X,Y,Z).
Code: Select all
clauses
run() :-
N = 1000,
S = list1(N),
stdio::write(S).
class predicates
list1 : (unsigned N) -> string Tree.
clauses
list1(0) = "0" :-
!.
list1(N) = T2 :-
T1 = list1(N-1),
T2 = string::format("%, %", T1, N).
Code: Select all
class predicates
list2 : (unsigned N) -> string Tree.
clauses
list2(N) = S:getString() :-
S = outputStream_string::new(),
list2b(S, N).
class predicates
list2b : (outputStream S, unsigned N).
clauses
list2b(S, 0) :-
!,
S:write("0").
list2b(S, N) :-
list2b(S, N - 1),
S:writef(", %", N).
Code: Select all
clauses
run() :-
profileTime::init(),
foreach N in [100, 1000, 10000] do
C1 = convert(profileTime::costName, string::format("list1(%)", N)),
profileTime::start_pr(C1),
_S1 = list1(N),
profileTime::stop_pr(C1),
C2 = convert(profileTime::costName, string::format("list2(%)", N)),
profileTime::start_pr(C2),
_S2 = list2(N),
profileTime::stop_pr(C2)
end foreach,
profileTime::printAndReset().
Code: Select all
string::concat
string::write
string::format
sting::concatWithDelimeter
outputstream_str::write, writef