Hi,
while solving a coding competition problem with julia I experienced extreme performance issues with the String type.
This program:
function deleteCount(target::String, input::String)
if length(target) > length(input)
return "IMPOSSIBLE"
end
offset::Int = 0
for i = 1:length(target)
while target[i] != input[i+offset]
offset += 1
if length(target) > length(input) - offset
return "IMPOSSIBLE"
end
end
end
return length(input) - length(target)
end
function processTest(io::IO=stdin)
numCases = parse(Int, readline(io))
for i = 1:numCases
target = readline(io)
actual = readline(io)
println("Case #", i, ": ", deleteCount(target, actual))
end
end
processTest()
gets a timeout on the test set with over 20s execution time,
while the following seemingly similar version of it
function deleteCount(target::Vector{Char}, input::Vector{Char})
if length(target) > length(input)
return "IMPOSSIBLE"
end
offset::Int = 0
for i = 1:length(target)
while target[i] != input[i+offset]
offset += 1
if length(target) > length(input) - offset
return "IMPOSSIBLE"
end
end
end
return length(input) - length(target)
end
function processTest(io::IO=stdin)
numCases = parse(Int, readline(io))
for i = 1:numCases
target = readline(io)
actual = readline(io)
println("Case #", i, ": ", deleteCount(collect(target), collect(actual)))
end
end
processTest()
finishes in next to no time.
Why are Strings so slow compared to char arrays?