### How much does a square root cost?

Labels:
compilers,
computing,
math,
numerical methods

An interesting diversion: how much (time) does calling the square root function sqrt() cost, and is it even worth trying alternative less-accurate implementations?

Some Assembly Required provides some answers:

__SQUARE ROOT__Method | Total time | Time per float | Avg Error |
---|---|---|---|

Compiler `sqrt(x)` /x87 FPU `FSQRT` |
404.029ms | 24ns | 0.0000% |

SSE intrinsic `ssqrts` |
200.395ms | 11.9ns | 0.0000% |

Carmack’s Magic Number rsqrt * x | 72.682ms | 4.33ns | 0.0990% |

SSE `rsqrtss` * x |
20.495ms | 1.22ns | 0.0094% |

SSE `rsqrtss` * xwith one NR step |
53.401ms | 3.18ns | 0.0000% |

SSE `rsqrtss` * xwith one NR step, unrolled by four |
48.701ms | 2.90ns | 0.0000% |

__RECIPROCAL SQRT__Method | Total time | Time per float | Avg Error |
---|---|---|---|

Carmack’s Magic Number rsqrt | 59.378ms | 3.54ns | 0.0990% |

SSE `rsqrtss` |
14.202ms | 0.85ns | 0.0094% |

SSE `rsqrtss` with one NR step |
45.952ms | 2.74ns | 0.0000% |