# How to increase speed/performance of this function

**URL:** <https://forum.crystal-lang.org/t/how-to-increase-speed-performance-of-this-function/1976>\
**Category:** Help & Support\
**Created:** [April 19, 2020, 8:10am UTC](https://forum.crystal-lang.org/t/how-to-increase-speed-performance-of-this-function/1976 "2020-04-19T08:10:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kingsleyh](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kingsleyh/32/151_2.png) [@kingsleyh](https://forum.crystal-lang.org/u/kingsleyh)\
**Post date:** [April 19, 2020, 8:10am UTC](https://forum.crystal-lang.org/t/how-to-increase-speed-performance-of-this-function/1976/1 "2020-04-19T08:10:43Z")

</div>

Hi

This ec\_mod\_inv function is too slow and I would like to make it the fastest it can possibly be but I’m not sure if there is any room for improvement?

Does anyone know if this is the most performant implementation possible in Crystal?

[https://play.crystal-lang.org/#/r/8wxj](https://play.crystal-lang.org/#/r/8wxj)

In release mode it runs at:

```
           user system total real
mod_inv: 0.003515 0.002847 0.006362 (0.003714)

```

Any help would be greatly appreciated

Thanks

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [April 19, 2020, 10:45am UTC](https://forum.crystal-lang.org/t/how-to-increase-speed-performance-of-this-function/1976/2 "2020-04-19T10:45:22Z")

</div>

Someone still needs to implement this: [https://github.com/crystal-lang/crystal/issues/6646](https://github.com/crystal-lang/crystal/issues/6646)

PRs are welcome!

The mutating API is a bit more verbose but you get the best performance out of it because you don’t need to allocate unnecessary GMP instances.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [April 19, 2020, 10:50am UTC](https://forum.crystal-lang.org/t/how-to-increase-speed-performance-of-this-function/1976/3 "2020-04-19T10:50:22Z")

</div>

Actually, it seems the discussion concluded in moving big numbers outside of the stdlib so I’m not sure. Also implementing all of these methods is a huge amount of work, and there are some subtle differences.

For starters, you can try using some of the code posted in the issue, apply it to your code and see what performance you get. If that speeds up thing, we can consider doing it (I could do it but it will take me some time).
