Look and think good things about D

Ary Borenszweig ary at esperanto.org.ar
Fri Nov 15 05:13:50 PST 2013


On 11/15/13 10:07 AM, Chris wrote:
> 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"?

Absolutely. You are a human and you spend a lot of time reading code. 
The more human the code looks to you, the better, I think, as long as it 
doesn't become too long or too annoying to read, like:

for every number between 2 and max do
   ...
end

:-P



More information about the Digitalmars-d mailing list