On Friday, 15 November 2013 at 12:47:21 UTC, bearophile wrote:
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

Slightly OT: Why do languages like Ruby (and now Crystal) have to state the obvious in an awkward way?

(2...max).each do

Of course you _do_ _each one_ from 2 to max. Is it to make it more "human"? If anything, human languages and thinking get rid of the superfluous (i.e. the obvious). Just like

x = x + 1 (Pythronizing)
x++; (obvious, concise, all you need).

Buy one, get one free.

(= If you buy one (of these), you will get one (another one of these) for free.)

Sorry that was just a rant-om comment :)

Reply via email to