Am Donnerstag, 17. Juni 2004 00:00 schrieb Relm:
> On Thu, 17 Jun 2004, why the lucky stiff wrote:
> > Ralf M?ller wrote:
> > >tried with Hash instead of Array:
> > >ram@lilith:~/src/ruby$time ruby -e 'str=STDIN.gets; h=Hash.new(); 
> > > (0..9).each {|i| h[i.to_s] = 0}; (0..str.length-1).each {|i|
> > > h[str[i..i]] += 1 }; h.each {|k,v| print "#{k} #{v}\n"} ' < pi.out |
> > > sort
> >
> > why@stungun$ time ruby -n -e 'c={};$_.each_byte{|b|c[b]||=0; c[b]+=1};
> > c.sort.each {|k,v| puts "#{k - ?0} #{v}"}' < pi.out
> >
> > same idea as yours.  just runs 3x swift.
>
> I typo a lot, so no command line.  Maybe 150-170x speed of OP.
>
>   n = STDIN.gets
>   tot = 0
>   9.times do |i|
>     c = n.count(i.to_s)
>     tot += c
>     puts "#{i} #{c}"
>   end
>   puts "9 #{n.size - tot}"

I tried this

ram@lilith:~/src/ruby$cat trcount-simple.rb 
#!/usr/local/bin/ruby
#num=STDIN.gets
num=STDIN.gets
# prepare an array
a = Array.new;
(0..9).each {|i| a[i] = 0}
# for num in an single line
#(0..num.length).each {|n| a[num[n..n].to_i] += 1 }
(0..num.length-1).each {|n| a[num[n..n].to_i] += 1 }
# print the result
a.each_index  {|index| print "#{index}     #{a[index]}\n" }
ram@lilith:~/src/ruby$time ruby trcount-simple.rb < pi.out 
0     16095
1     16107
2     15965
3     15915
4     15967
5     16169
6     15934
7     16091
8     15999
9     15790

real    0m0.964s
user    0m0.950s
sys     0m0.010s
ram@lilith:~/src/ruby$time ruby -e 'num=STDIN.gets; a=Array.new; (0..9).each 
{|i| a[i] = 0
}; (0..num.length).each {|i| a[num[i..i].to_i] += 1 }; a.each_index {|i| print 
"#{i} #{a[i
]}\n"} ' < pi.out
0 16096
1 16107
2 15965
3 15915
4 15967
5 16169
6 15934
7 16091
8 15999
9 15790

real    0m0.955s
user    0m0.940s
sys     0m0.000s

It does not seem to make any difference if the source i given at the command 
line or in a separate file: 
ram@lilith:~/src/ruby$cat rec_pi.rb 
n = STDIN.gets
  tot = 0
  9.times do |i|
    c = n.count(i.to_s)
    tot += c
    puts "#{i} #{c}"
  end
  puts "9 #{n.size - tot}"
ram@lilith:~/src/ruby$time ruby rec_pi.rb < pi.out
0 16095
1 16107
2 15965
3 15915
4 15967
5 16169
6 15934
7 16091
8 15999
9 15790

real    0m0.048s
user    0m0.040s
sys     0m0.010s
ram@lilith:~/src/ruby$time ruby -e 'n = STDIN.gets; tot = 0; 9.times do |i| c 
= n.count(i.to_s); tot += c; puts "#{i} #{c}"; end; puts "9 #{n.size - tot}"' 
<pi.out 
0 16095
1 16107
2 15965
3 15915
4 15967
5 16169
6 15934
7 16091
8 15999
9 15790

real    0m0.043s
user    0m0.010s
sys     0m0.030s

But maybe, you're right and these conditions do just not lead to that 
difference. Under which circumstances does the difference occur?

regards
ralf