From: Michael Selig Date: 2008-10-10T10:30:31+09:00 Subject: [ruby-core:19255] Re: performance of C function calls in 1.8 vs 1.9 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