On Friday, 15 November 2013 at 13:13:51 UTC, Ary Borenszweig
wrote:
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
Well, that was exactly my point. As a human being you don't need
the patronizing (and highly annoying) "for every number ...".
This is what you say when you explain it to a newbie. But there
is no need to spell this out in the syntax. Syntax of programming
languages is (or should be) like road signs, or any other signs.
Concise and expressive. Else, what's the point? I know that
languages like Lua have the philosophy that non-programmers
should be able to use it. But every human being is capable of
abstracting things. There is no need for this terrible syntax
(2..max).each do:
end
It doesn't add anything to the code except for useless
characters. Humans have used signs and acronyms for ages. We can
cope with it. I once saw the most beautiful encrypted message in
Arabic, which when read properly unfolds into an array of
characters and meaning. We humans can deal with it. I still don't
see why x++; is a problem and has to be spelled out as x = x + 1,
or even x += 1 (slightly better).
If Ruby programmers had invented spelling, you would "Double U Ar
I Tee ee" like this. Ha ha ha! :-)