On 11/15/13 7:54 AM, bearophile wrote:
Jacob Carlborg:

What does this show, that ranges is slow in D?

It shows that D is awesome. Do you know other languages that allow you
to write two programs to solve that problem as short/simple and as fast
as those two? :-)

Bye,
bearophile

Probably Crystal.

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.

And with Crystal you could do the second version as well, because you have access to low level stuff like pointers. And also, the language is pretty new so there's still a lot of optimizations to be done.

I also thought ranges were pretty fast because of their nature. Why are they slow in this example?

Reply via email to