Skip to content

Benchmarks of conversions

Dmitry Gritsay edited this page Sep 15, 2016 · 2 revisions

See docs for VerifiedObject trait for more information about safe conversions.

Safe vs unsafe conversions for built-in Ruby types

unsafe { object.to::<Fixnum>() };

vs

object.try_convert_to::<Fixnum>();

Results

Calculating -------------------------------------
              unsafe      8.903M (± 5.8%) i/s -     88.871M in  10.020585s
                safe      8.568M (± 5.7%) i/s -     85.599M in  10.027649s

Comparison:
              unsafe:  8902992.1 i/s
                safe:  8568230.0 i/s - same-ish: difference falls within error

Different kinds of safe conversions for custom types

object.respond_to("listen");

vs

object.class() == Class::from_existing("Server");

vs

object.class().ancestors().iter().any(|class| *class == Class::from_existing("Listener"));

Results

Calculating -------------------------------------
         respond_to?      1.984M (± 4.2%) i/s -     19.877M in  10.038876s
               class      2.501M (± 4.1%) i/s -     25.065M in  10.037617s
            ancestor    681.271k (± 3.2%) i/s -      6.824M in  10.027656s

Comparison:
               class:  2501478.2 i/s
         respond_to?:  1983634.6 i/s - 1.26x  slower
            ancestor:   681271.4 i/s - 3.67x  slower