ruby-prof says Ruby increment operator (+=) takes 25 seconds

170 Views Asked by At

I'm trying to profile some Ruby code I wrote using ruby-prof gem and see that basic operations like i += 1 (listed as Fixnum#+ in the table below) take over 24 seconds to run (in this particular test, the operation is performed 2,199,978 times). Is this normal?

Thread 582936

%Total   %Self   Total   Self  Wait  Child       Calls              Name

                 203.93  81.72   0.00  122.21    100001/100001    InputFile#parse
46.96%   18.82%  203.93  81.72   0.00  122.21    100001           InputFile#split_on_semicolon
                  24.59  24.59   0.00    0.00   2199978/3200094   Fixnum#+
                  16.02  16.02   0.00    0.00    100001/399998    String#split
                  14.72  14.72   0.00    0.00    999990/999991    String#[]
                  13.12  13.12   0.00    0.00   1199988/1199990   Fixnum#<
                  10.97  10.97   0.00    0.00    999990/2239978   String#empty?
                  10.49  10.49   0.00    0.00   1199988/1199988   String#<<
                   9.75   9.75   0.00    0.00   1199988/1200074   Array#[]
                   7.77   7.77   0.00    0.00    999990/999990    String#eql?
                   6.76   6.76   0.00    0.00    599994/599994    Fixnum#-
                   4.62   4.62   0.00    0.00    599994/599994    Array#delete_at
                   1.25   1.25   0.00    0.00    100001/1339989   Kernel#nil?
                   1.14   1.14   0.00    0.00    100001/300003    Array#size
                   1.01   1.01   0.00    0.00    100001/300002    Fixnum#>
1

There are 1 best solutions below

1
On BEST ANSWER

Your results don't say += takes 25 seconds. They say that 2199978 calls to + took 24.59 seconds, which comes to 89.5 calls per ms. That's a bit slow, but probably only because it's being profiled. I don't see anything unusual in that.