This reminds me of an earlier topic.
Ruby out performs Crystal significantly on this numerical algorithm
Programs that use functions from external C libraries often spend a lot of time calling C functions. C is fast, but we like to apply time-consuming processes to C, so C takes most of our time. If TruffleRuby is slower than Ruby, then TruffleRuby may have a higher overhead cost in calling C functions compared to CRuby.
JP Camara’s blog on compiling Ruby with libgmp. I think this is about linking libgmp to CRuby.
brew install gmp
rvm reinstall ruby-head --with-gmp-dir=$(brew --prefix gmp)
So we may need to be aware that calling the GMP function in the gmp gem’s C extension library and CRuby itself calling the GMP function are slightly different in the way the GMP function is called, I think.
I think that if you compile CRuby with the GMP library specified, some operations will be delegated to GMP without requiring any special handling.
The use cases for the GMP gem are likely: (1) calling GMP functions directly to perform operations even if CRuby is not compiled with GMP, and (2) identifying operations in CRuby that are not delegated to GMP and having GMP handle them instead.
In general, there is a conversion cost when calling C functions from Ruby. For example, Ruby arrays and C pointers need to be converted back and forth. I’ve heard that simple numbers don’t incur much conversion cost due to immediate values, but how about large numbers?
However, if you continue the calculation using C types to avoid the conversion between Ruby and C every time you call a function, it will be the same thing as if we were writing in C, and the advantage of using Ruby will diminish.
I’m not really sure about the difficult cases.
I’ve heard that in Crystal there is no cost to calling C functions, which is what I like about Crystal.