Am Mittwoch, 16. Juni 2004 16:13 schrieb Ralf M?ller:
> Hi,
>
> I made a comparison:
>
> 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    0m1.023s
> user    0m0.970s
> sys     0m0.010s
>
>
> and
>
>
> ram@lilith:~/src/ruby$time gawk '{i=0;j=length($0);while( i <= j) {i++;
> z=substr($0,i,1); {count=count+ 1; a[z]=a[z]+1; }}}END{ for ( i in a )
> {print i ":" a[i];}print "Digits: " count;}' < pi.out  | sort
> 0:16095
> 1:16107
> 2:15965
> 3:15915
> 4:15967
> 5:16169
> 6:15934
> 7:16091
> 8:15999
> 9:15790
>
> :1
>
> Digits: 160033
>
> real    0m0.305s
> user    0m0.310s
> sys     0m0.000s
>
>
> I'm really new to ruby. So I would like to know, if there is any possible
> improvement of the ruby code.
> pi.out contains about 160000 digits of PI and is made by "ruby sample/pi.rb
> > pi.out' inside the src-dir of the ruby-*.tgz
>
>
> Any hints???
>
>
> thanks
> ralf

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
0 16095
1 16107
2 15965
3 15915
4 15967
5 16169
6 15934
7 16091
8 15999
9 15790

real    0m0.920s
user    0m0.910s
sys     0m0.010s

gains 10%.