15,878,231 members
Articles / Programming Languages / C#
Tip/Trick

# No Code is the Fastest Code

Rate me:
8 Jan 2020CPOL2 min read 9.6K   44   2   15
Optimization of finding a point with shortest distance w.r.t. a point of interest

## Introduction

When it comes to optimization, majority of developers turn to parallelism but per CPU core isn't cheap. We could look to eliminate some operations to increase the per-thread performance first. That doesn't mean accuracy has to be sacrificed. With all other things being equal, the code with lesser operations shall be more performant, hence the tip title: no code is fastest code since no code needs to be executed.

## Shortest Distance

We'll use the task of finding the shortest distance as an example. The formula of the length of a line is used to find the distance between 2 points, as shown below:

We have to compare and store the shorter distance. The interesting property of relationship of 2 numbers and their square roots is shown below: Given square root of D1 is lesser than that of D2, it should also be true that D1 is lesser than D2. We will make use of that property to forgo square root calculation.

Below is the C# code using length of line formula, meaning with `Math.Sqrt()` to find the nearest distance with respect to `dest`. The C++ code is identical, except for the `sqrt()` call, so it is not shown here to avoid repetition.

C#
```double shortest = 10000000.0;
int shortest_index = 0;

for (int j = 0; j < list.Count; ++j)
{
Point pt = list[j];
double x = (pt.x - dest.x);
double y = (pt.y - dest.y);
x *= x;
y *= y;
double distance = Math.Sqrt(x + y); // calling sqrt
if (distance < shortest)
{
shortest = distance;
shortest_index = j;
}
}```

This is the C# code to find the shortest distance without using `Math.Sqrt()` in the tight loop but `Math.Sqrt()` is still used to compute the distance outside the loop at the end when the shortest distance is found.

C#
```double shortest2 = 10000000.0;
int shortest_index2 = 0;

for (int j = 0; j < list.Count; ++j)
{
Point pt = list[j];
double x = (pt.x - dest.x);
double y = (pt.y - dest.y);
x *= x;
y *= y;
double distance = x + y; // no calling sqrt
if (distance < shortest2)
{
shortest2 = distance;
shortest_index2 = j;
}
}
shortest2 = Math.Sqrt(shortest2);```

## Benchmark Results

The benchmark is done on the computer with these specs: Intel(R) Core i7-8700 @ 3.2GHz CPU with 16GB RAM. The executable is built in Release x64 mode. Only one thread is utilized. The outer loop is done 1 million times while the inner loop is done on the `List` (or `vector` in C++) with one thousand elements.

C++
```VC++ 2019 16.4 update, /Ox
============================
With sqrt timing: 4855ms
Without sqrt timing: 1264ms```

In Visual C++, we get a 74% speedup over the sqrt version. G++ and Clang++ benchmark are built and run in Windows Subsystem for Linux (Ubuntu 1804). It does seem like G++ and Clang++ has a more optimized `sqrt()` implementation over VC++'s. Both compilers generate faster code than VC++, despite having WSL overhead running on Windows.

C++
```Clang++ 6.0.0, -O3
============================
With sqrt timing: 1830ms
Without sqrt timing: 1047ms```

With Clang++, we get 42% speedup.

C++
```G++ 7.4.0, -O3
============================
With sqrt timing: 2388ms
Without sqrt timing: 1211ms```

With G++, we get almost 50% speedup, but its timing isn't as good as Clang++'s.

C++
```C# 7, .NET Framework 4.6.1
============================
With sqrt timing: 1938ms
Without sqrt timing: 1840ms```

The result is surprising with C# 7. Only about 100 milliseconds shaved off the total time. So don't bother with this optimization in C# since the speed improvement is negligible (only 5% better).

## History

• 18th January, 2020: Add bailout before squaring code suggested by scjurgen to C++ and C# benchmark
• 9th January, 2020: Initial release

Written By
Software Developer (Senior)
Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

 First Prev Next
 Bailout before calculating the square scjurgen13-Jan-20 3:06 scjurgen 13-Jan-20 3:06
 Re: Bailout before calculating the square YvesDaoust13-Jan-20 23:36 YvesDaoust 13-Jan-20 23:36
 Re: Bailout before calculating the square scjurgen13-Jan-20 23:51 scjurgen 13-Jan-20 23:51
 Re: Bailout before calculating the square YvesDaoust14-Jan-20 0:04 YvesDaoust 14-Jan-20 0:04
 Re: Bailout before calculating the square Shao Voon Wong17-Jan-20 21:27 Shao Voon Wong 17-Jan-20 21:27
 Re: Bailout before calculating the square Shao Voon Wong17-Jan-20 23:18 Shao Voon Wong 17-Jan-20 23:18
 Re: Bailout before calculating the square Shao Voon Wong28-Jan-20 2:08 Shao Voon Wong 28-Jan-20 2:08
 Re: Bailout before calculating the square scjurgen28-Jan-20 3:40 scjurgen 28-Jan-20 3:40
 ICC E. Papulovskiy10-Jan-20 1:23 E. Papulovskiy 10-Jan-20 1:23
 ICC on an old good Intel 4770 gives: ``` With sqrt timing: 1024ms Without sqrt timing: 501ms shortest: 9.25283, 9.25283 shortest index: 483, 483 Done!``` MSVC was designed to produce GUI applications, it was never meant for speed.
 For VC++ use "/fp:fast" logica9-Jan-20 4:40 logica 9-Jan-20 4:40
 Makefile Member 41335848-Jan-20 23:58 Member 4133584 8-Jan-20 23:58
 Re: Makefile Shao Voon Wong9-Jan-20 0:07 Shao Voon Wong 9-Jan-20 0:07
 Debate Rick York8-Jan-20 19:48 Rick York 8-Jan-20 19:48
 Re: Debate Shao Voon Wong9-Jan-20 13:52 Shao Voon Wong 9-Jan-20 13:52
 Re: Debate Rick York9-Jan-20 18:01 Rick York 9-Jan-20 18:01
 Last Visit: 31-Dec-99 18:00     Last Update: 14-Apr-24 23:47 Refresh 1