# Performance comparison of a custom gcd() function in Julia, C, and Rust

**URL:** https://discourse.julialang.org/t/performance-comparison-of-a-custom-gcd-function-in-julia-c-and-rust/121665
**Category:** Performance
**Tags:** question, ffi
**Created:** [October 23, 2024, 6:27pm UTC](https://discourse.julialang.org/t/performance-comparison-of-a-custom-gcd-function-in-julia-c-and-rust/121665 "2024-10-23T18:27:00Z")
**Posts on this page:** 1
**Showing post:** 7

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 23, 2024, 10:21pm UTC](https://discourse.julialang.org/t/performance-comparison-of-a-custom-gcd-function-in-julia-c-and-rust/121665/7 "2024-10-23T22:21:22Z")

</div>

> [@jbytecode](#):
>
> In short, it’s better to use loops, and recursion & repeated function self-calls should be avoided as much as possible

No. Recursion is often a great way to code, if it is used appropriately. [Recursion in Julia — bad idea? - #5 by stevengj](https://discourse.julialang.org/t/recursion-in-julia-bad-idea/99304/5)

But the interesting and useful cases for recursion (in an imperative language) are mostly not tail recursive, so they won’t be optimized away by _any_ compiler in _any_ language. Instead, you often have to make them fast by coarsening the base case (which no production compiler will do for you, AFAIK).

“Yes” in a more narrow sense: use loops instead of self tail calls, which are trivially rewritten as loops in an imperative language like Julia. Recursion adds no value in this case.

---

_[View the full topic](https://discourse.julialang.org/t/performance-comparison-of-a-custom-gcd-function-in-julia-c-and-rust/121665)._
