R
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 = 0}; (0..num.length).each {|i| a[num[i..i].to_i] += 1 };
a.each_index {|i| print "#{i} #{a}\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;}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
I made a comparison:
ram@lilith:~/src/ruby$time ruby -e 'num=STDIN.gets; a=Array.new; (0..9).each
{|i| a = 0}; (0..num.length).each {|i| a[num[i..i].to_i] += 1 };
a.each_index {|i| print "#{i} #{a}\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;}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