Ary Borenszweig:
Here's what I was able to do in some minutes:
---
if ARGV.length != 1
puts "missing argument: n"
exit 1
end
n = ARGV[0].to_i
str = "1"
buffer = String::Buffer.new(20)
n.times do
puts str.length
str.each_chunk do |digit, count|
buffer << '0' + count
buffer << digit
end
str = buffer.to_s
buffer.clear
end
With n=70 it takes about 4.89s. With n=45 it takes about 0.012s.
This program is much longer in number of tokens to the first D
program. You can write a D program about as fast as this in about
the same number of tokens.
Perhaps I should add an intermediate third version that shows
code that's not as extreme as the two versions there. Thank you
for the implicit suggestion.
And with Crystal you could do the second version as well,
because you have access to low level stuff like pointers.
In Crystal do you have final switches, gotos, etc too?
And also, the language is pretty new so there's still
a lot of optimizations to be done.
And LDC2 will improve in the meantime.
I also thought ranges were pretty fast because of their nature.
It also matters a lot how you use them, this is normal in
computer programming.
Why are they slow in this example?
Just because the first example is not written for speed, I didn't
even add run-time timings for it at first. And it's not that slow.
Bye,
bearophile