From: Jason Roelofs Date: 2008-10-10T11:02:03+09:00 Subject: [ruby-core:19256] Re: performance of C function calls in 1.8 vs 1.9 On Thu, Oct 9, 2008 at 9:30 PM, Michael Selig wrote: > On Wed, Oct 8, 2008 at 3:52 PM, Paul Brannan wrote: > >> It seems it is more expensive to call a C function in 1.9 than it used >> to be in 1.8: >> >> [pbrannan@zem tmp]$ ruby test.rb >> user system total real >> Ruby method 7.170000 0.010000 7.180000 ( 7.177720) >> C function 3.950000 0.000000 3.950000 ( 3.953411) >> [pbrannan@zem tmp]$ ruby1.9 test.rb >> user system total real >> Ruby method 8.570000 0.000000 8.570000 ( 8.580137) >> C function 8.580000 0.000000 8.580000 ( 8.591668) > > I am not too sure what is going on here, because my results are: > > ruby1.8 test.rb > user system total real > Ruby method 11.540000 4.260000 15.800000 ( 16.032414) > C function 6.380000 1.470000 7.850000 ( 7.989895) > > ruby1.9 test.rb > user system total real > Ruby method 6.170000 0.010000 6.180000 ( 6.348794) > C function 5.710000 0.010000 5.720000 ( 5.835988) > > which looks quite respectable, and is a testament to the improvements made > in 1.9 that the Ruby method runs nearly as fast as the C method. > > The other important thing is that the bulk of the time in this benchmark is > actually executing the for loop itself, not the method you are trying to > measure (especially so in 1.9). For example, simply by changing the "for i > in 1..N" to "N.times", I get: > > ruby1.8 test1.rb > user system total real > Ruby method 9.720000 3.150000 12.870000 ( 13.071435) > C function 4.800000 0.690000 5.490000 ( 5.576923) > > ruby1.9 test1.rb: > user system total real > Ruby method 2.440000 0.000000 2.440000 ( 2.648241) > C function 1.880000 0.000000 1.880000 ( 1.984703) > > So you really should subtract the loop overhead to get a true comparison. > One way of doing this is to run the benchmark again with 11 calls to the > method inside the loop. If you then subtract the time of the original > benchmark from this, you should be left with the time for executing the > method 10 * N times. > > Cheers > Mike > > > Mike, I think you misunderstand what Paul is pointing out. Compare the ration between the Ruby method and C function call times of a test, not between 1.8 to 1.9. Using your numbers: > ruby1.8 test1.rb > user system total real > Ruby method 9.720000 3.150000 12.870000 ( 13.071435) > C function 4.800000 0.690000 5.490000 ( 5.576923) > C function invocation is about twice as fast than Ruby method invocation > ruby1.9 test1.rb: > user system total real > Ruby method 2.440000 0.000000 2.440000 ( 2.648241) > C function 1.880000 0.000000 1.880000 ( 1.984703) C function invocation is about a quarter faster than Ruby method evocation, a speed loss of 50%. Using Paul's initial numbers: 1.8 >> user system total real >> Ruby method 7.170000 0.010000 7.180000 ( 7.177720) >> C function 3.950000 0.000000 3.950000 ( 3.953411) Again, here it's about twice as fast to call C functions as Ruby methods 1.9 >> user system total real >> Ruby method 8.570000 0.000000 8.570000 ( 8.580137) >> C function 8.580000 0.000000 8.580000 ( 8.591668) Here it's actually slower to call C functions over Ruby methods. So the question is: why are C functions now twice as expensive to call with 1.9 than with 1.8? Jason