Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Great Computer Language Shootout

12 views
Skip to first unread message

ig...@yahoo.com

unread,
Mar 11, 2005, 7:12:46 PM3/11/05
to
We've added some new micro-benchmarks and deprecated some of the old
ones.
So now Ruby is missing a dozen programs!

http://shootout.alioth.debian.org/

If you have a few moments please contribute stylish Ruby programs.

best wishes, Isaac

William James

unread,
Mar 12, 2005, 5:26:48 AM3/12/05
to

Here's a shorter and, I think, slightly faster program to count
word-frequencies.

freq = Hash.new(0)
loop {
data = (STDIN.read(4095) or break) + (STDIN.gets || "")
for word in data.downcase!.tr!('^a-z',' ').split
freq[word] += 1
end
}
print freq.to_a.map{|x| sprintf("%7d %s\n",x[1],x[0])}.sort.reverse

Can anyone explain why the Perl version is 4 times as fast as the
Ruby one?

Message has been deleted

ig...@yahoo.com

unread,
Mar 12, 2005, 10:54:45 AM3/12/05
to

William James wrote:
> ig...@yahoo.com wrote:
> > We've added some new micro-benchmarks and deprecated some of the
old
> > ones.
> > So now Ruby is missing a dozen programs!
> >
> > http://shootout.alioth.debian.org/
> >
> > If you have a few moments please contribute stylish Ruby programs.
> >
> > best wishes, Isaac
>
> Here's a shorter and, I think, slightly faster program to count
> word-frequencies.
>
> freq = Hash.new(0)
> loop {
> data = (STDIN.read(4095) or break) + (STDIN.gets || "")
> for word in data.downcase!.tr!('^a-z',' ').split
> freq[word] += 1
> end
> }
> print freq.to_a.map{|x| sprintf("%7d %s\n",x[1],x[0])}.sort.reverse

Excellent!

Before we can use it I'm afraid you have to contribute it to the
shootout, we can't just skim programs from other places.

Please send programs to the mailing list or send them using the message
form or email them to igouy2

http://shootout.alioth.debian.org/faq.php?sort=fullcpu

best wishes, Isaac

William James

unread,
Mar 12, 2005, 1:58:33 PM3/12/05
to

Ruby gurus: before this is sent to the shootout, would you check
it to see if it can be made faster? (It has already been checked
for correctness.)

Carlos

unread,
Mar 16, 2005, 7:14:15 AM3/16/05
to
[William James <w_a_...@yahoo.com>, 2005-03-15 14.56 CET]
> > William James wrote:
[...]

> > > freq = Hash.new(0)
> > > loop {
> > > data = (STDIN.read(4095) or break) + (STDIN.gets || "")
> > > for word in data.downcase!.tr!('^a-z',' ').split

But "a".downcase! => nil ... And the same with most !-methods (they return
nil when nothing was changed).

Daniel Amelang

unread,
Mar 16, 2005, 7:16:26 PM3/16/05
to
> But "a".downcase! => nil ... And the same with most !-methods (they return
> nil when nothing was changed).

That always bothered me, since I lose my method chaining with !
methods. Like this:

line.strip!.downcase!

must be

line.strip.downcase

to work properly. But then you lose some efficiency.

I'd prefer that ! methods returned 'self'. In the *rare* case that I
need to know if something changed, I'll use == or something. RCR in
the making?

Dan


Michael Campbell

unread,
Mar 16, 2005, 7:41:57 PM3/16/05
to
It's been suggested many times. I agree, but my vote hardly counts. =)

Hal Fulton

unread,
Mar 16, 2005, 7:42:23 PM3/16/05
to

It's been discussed a lot over the years. My impression is that
Matz likes this behavior, in which case it would be unlikely to
change.


Hal


Isaac Gouy

unread,
Mar 29, 2005, 1:38:15 PM3/29/05
to
We've made it somewhat easier to contribute programs.

No need to subscribe to the mailing-list.
No need for a user-id or login.

See the FAQ "How can I contribute a program?"
http://shootout.alioth.debian.org/faq.php?sort=fullcpu

0 new messages