I decided to compare the performance of using if
-based execution against actual polymorphic method calls on the big 3 Ruby implementations. The results on MRI were what I expected across all of them — that is, polymorphism being much faster. The results are across 1M objects in an array.
In the case of MRI 1.9, dynamic dispatch is 58% faster than explicit if-based branching. On JRuby, the runtimes are nearly identical. And on Rubinius, polymorphic calls are slightly faster (about 25%).
user system total real
if 0.200000 0.000000 0.200000 ( 0.202083)
polymorphic 0.130000 0.000000 0.130000 ( 0.127721)
user system total real
if 0.080000 0.010000 0.090000 ( 0.075000)
polymorphic 0.110000 0.020000 0.130000 ( 0.074000)
user system total real
if 0.206464 0.000040 0.206504 ( 0.206511)
polymorphic 0.164807 0.000022 0.164829 ( 0.164837)