# How many flops does it take to compute a square root?

**URL:** <https://discourse.julialang.org/t/how-many-flops-does-it-take-to-compute-a-square-root/89027>\
**Category:** Numerics\
**Created:** [October 20, 2022, 5:42pm UTC](https://discourse.julialang.org/t/how-many-flops-does-it-take-to-compute-a-square-root/89027 "2022-10-20T17:42:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nvenkov1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nvenkov1/32/43679_2.png) [@nvenkov1](https://discourse.julialang.org/u/nvenkov1)\
**Post date:** [October 20, 2022, 5:42pm UTC](https://discourse.julialang.org/t/how-many-flops-does-it-take-to-compute-a-square-root/89027/1 "2022-10-20T17:42:37Z")

</div>

How many flops does it take to compute a square root?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 20, 2022, 5:53pm UTC](https://discourse.julialang.org/t/how-many-flops-does-it-take-to-compute-a-square-root/89027/2 "2022-10-20T17:53:36Z")

</div>

This (perhaps surprisingly) isn’t a well defined question. it clearly is 1 flop (sqrt), and even is only 1 cpu instruction on most cpus. However, it is generally going to be slower than an add or multiply. You should expect it to be roughly half as fast as a div.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [October 20, 2022, 5:57pm UTC](https://discourse.julialang.org/t/how-many-flops-does-it-take-to-compute-a-square-root/89027/3 "2022-10-20T17:57:50Z")

</div>

More generally, you can answer these questions with tools like GFlops.jl or, on a hardware-level, with LIKWID.jl.

**GFlops.jl** (counting FLOP operations in a piece of code via Cassette.jl)

```julia
using GFlops
x = rand(1000)
@count_ops sqrt.(x) # gives 1000, i.e. 1 FLOP per element

```

(Note that GFlops.jl isn’t really reliable. For example, it doesn’t count FLOPs outside of Julia, i.e. due to LAPACK/BLAS)

**LIKWID.jl** (counts the FLOPS on a hardware level, i.e. by utilizing counters inside of a CPU core, only works on Linux)

```julia
julia> using LIKWID

julia> x = rand(1000);

julia> function count_FLOPs(f)
           metrics, _ = perfmon(f, "FLOPS_DP"; print=false)
           flops_per_second = first(metrics["FLOPS_DP"])["DP [MFLOP/s]"] * 1e6
           runtime = first(metrics["FLOPS_DP"])["Runtime (RDTSC) [s]"]
           return round(Int, flops_per_second * runtime)
       end
count_FLOPs (generic function with 1 method)

julia> count_FLOPs(() -> sqrt.(x))
1000 # again 1 per element

```

Compare this to e.g. the exponential function for which I find

```julia
julia> count_FLOPs(() -> exp.(x))
17000 # 17 FLOPs per element

```

See [Counting FLOPs · LIKWID.jl](https://juliaperf.github.io/LIKWID.jl/dev/tutorials/counting_flops/) for more.

---

<div class="post-metadata">

**Author:** ![nvenkov1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nvenkov1/32/43679_2.png) [@nvenkov1](https://discourse.julialang.org/u/nvenkov1)\
**Post date:** [October 20, 2022, 5:57pm UTC](https://discourse.julialang.org/t/how-many-flops-does-it-take-to-compute-a-square-root/89027/4 "2022-10-20T17:57:54Z")

</div>

So would you count it as two flops in a cost analysis, or simply leave it as one. What’s the norm?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [October 20, 2022, 5:58pm UTC](https://discourse.julialang.org/t/how-many-flops-does-it-take-to-compute-a-square-root/89027/5 "2022-10-20T17:58:50Z")

</div>

Don’t count flops. what matters is time.

---

<div class="post-metadata">

**Author:** ![nvenkov1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nvenkov1/32/43679_2.png) [@nvenkov1](https://discourse.julialang.org/u/nvenkov1)\
**Post date:** [October 20, 2022, 5:59pm UTC](https://discourse.julialang.org/t/how-many-flops-does-it-take-to-compute-a-square-root/89027/6 "2022-10-20T17:59:47Z")

</div>

That’s interesting. Thank you.

---

<div class="post-metadata">

**Author:** ![samo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samo/32/35398_2.png) [@samo](https://discourse.julialang.org/u/samo)\
**Post date:** [October 21, 2022, 5:20pm UTC](https://discourse.julialang.org/t/how-many-flops-does-it-take-to-compute-a-square-root/89027/7 "2022-10-21T17:20:27Z")

</div>

… or use a metric that is directly proportional to time, as for example the [effective throughput metric](https://github.com/omlins/ParallelStencil.jl#performance-metric)!
