How Do Basic Math Operations Compare in Speed?

0
3
Asked By CuriousCoder42 On

I've been working on some algorithms and realized I made certain assumptions about the speed of different mathematical operations. For instance, when determining distance, if accuracy isn't a primary concern, I thought about squaring the X and Y values and skipping the square root for comparison purposes. This would mean saving time by only performing two squares, an addition, and a comparison instead of including the square root.

But what if I need to square the base distance for comparison? That could potentially turn into three squares, one addition, and one comparison.

I also explored other algorithms, including one using division for non-linear reductions based on distance. However, I started questioning whether division would be faster than other operations.

So, I'm looking for insights or resources that outline the relative speeds of basic math functions, particularly multiplication, division, exponents, and n-roots. I want to avoid vague suggestions that speed doesn't matter because computers are fast - I believe speed and memory optimization are still crucial in programming.

1 Answer

Answered By TechieTinker On

To really grasp the performance of math operations, especially on x86 or similar architectures, you’ll want to look at the documentation on CPU instructions. Websites like uops.info are great for understanding the latency and throughput of these operations. Basically, latency tells you how many CPU cycles an operation takes to finish, while throughput gives you insights into how many operations can be executed per cycle.

You might also try using an x86 CPU simulator to see how sequences of instructions behave when run repeatedly. This can clarify how different operations impact the overall execution time, taking dependencies into account. If you're not familiar with assembly, use Godbolt to generate it from your preferred programming language!

AlgoExplorer1 -

Got it, I’m focusing on x86 and going to explore ARM processors too for app development. Thanks for the tips!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.